diff options
author | Pali Rohár <pali.rohar@gmail.com> | 2019-04-25 19:36:57 +0200 |
---|---|---|
committer | Pali Rohár <pali.rohar@gmail.com> | 2019-04-25 19:36:57 +0200 |
commit | 83add39260d44a35e9acd2fc40389e7a9a27111f (patch) | |
tree | adb297ee7e67d5098bfc56ad33d926dfb017b00e | |
parent | d9d49f2dfc4eb4c22c8d9185348e67352e488f8b (diff) | |
download | dlfcn-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.c | 48 |
1 files changed, 22 insertions, 26 deletions
@@ -133,45 +133,41 @@ static char error_buffer[65535]; | |||
133 | static char *current_error; | 133 | static char *current_error; |
134 | static char dlerror_buffer[65536]; | 134 | static char dlerror_buffer[65536]; |
135 | 135 | ||
136 | static 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 | |||
156 | static void save_err_str( const char *str ) | 136 | static 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 | { |