aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPali Rohár <pali.rohar@gmail.com>2019-04-25 19:36:57 +0200
committerPali Rohár <pali.rohar@gmail.com>2019-04-25 19:36:57 +0200
commit83add39260d44a35e9acd2fc40389e7a9a27111f (patch)
treeadb297ee7e67d5098bfc56ad33d926dfb017b00e
parentd9d49f2dfc4eb4c22c8d9185348e67352e488f8b (diff)
downloaddlfcn-win32-83add39260d44a35e9acd2fc40389e7a9a27111f.tar.gz
dlfcn-win32-83add39260d44a35e9acd2fc40389e7a9a27111f.tar.bz2
dlfcn-win32-83add39260d44a35e9acd2fc40389e7a9a27111f.zip
Simplify implementation of save_err_str()
Check return value of FormatMessageA() function and remove copy_string() function as it is not needed.
-rw-r--r--dlfcn.c48
1 files changed, 22 insertions, 26 deletions
diff --git a/dlfcn.c b/dlfcn.c
index 475b069..8687c51 100644
--- a/dlfcn.c
+++ b/dlfcn.c
@@ -133,45 +133,41 @@ static char error_buffer[65535];
133static char *current_error; 133static char *current_error;
134static char dlerror_buffer[65536]; 134static char dlerror_buffer[65536];
135 135
136static int copy_string( char *dest, int dest_size, const char *src )
137{
138 int i = 0;
139
140 /* gcc should optimize this out */
141 if( !src || !dest )
142 return 0;
143
144 for( i = 0 ; i < dest_size-1 ; i++ )
145 {
146 if( !src[i] )
147 break;
148 else
149 dest[i] = src[i];
150 }
151 dest[i] = '\0';
152
153 return i;
154}
155
156static void save_err_str( const char *str ) 136static void save_err_str( const char *str )
157{ 137{
158 DWORD dwMessageId; 138 DWORD dwMessageId;
159 DWORD pos; 139 DWORD ret;
140 size_t pos, len;
160 141
161 dwMessageId = GetLastError( ); 142 dwMessageId = GetLastError( );
162 143
163 if( dwMessageId == 0 ) 144 if( dwMessageId == 0 )
164 return; 145 return;
165 146
147 len = strlen( str );
148 if( len > sizeof( error_buffer ) - 5 )
149 len = sizeof( error_buffer ) - 5;
150
166 /* Format error message to: 151 /* Format error message to:
167 * "<argument to function that failed>": <Windows localized error message> 152 * "<argument to function that failed>": <Windows localized error message>
168 */ 153 */
169 pos = copy_string( error_buffer, sizeof(error_buffer), "\"" ); 154 pos = 0;
170 pos += copy_string( error_buffer+pos, sizeof(error_buffer)-pos, str ); 155 error_buffer[pos++] = '"';
171 pos += copy_string( error_buffer+pos, sizeof(error_buffer)-pos, "\": " ); 156 memcpy( error_buffer+pos, str, len );
172 pos += FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwMessageId, 157 pos += len;
158 error_buffer[pos++] = '"';
159 error_buffer[pos++] = ':';
160 error_buffer[pos++] = ' ';
161
162 ret = FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwMessageId,
173 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), 163 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
174 error_buffer+pos, sizeof(error_buffer)-pos, NULL ); 164 error_buffer+pos, (DWORD) (sizeof(error_buffer)-pos), NULL );
165 pos += ret;
166
167 /* When FormatMessageA() fails it returns zero and does not touch buffer
168 * so add trailing null byte */
169 if( ret == 0 )
170 error_buffer[pos] = '\0';
175 171
176 if( pos > 1 ) 172 if( pos > 1 )
177 { 173 {