diff options
author | Pali Rohár <pali.rohar@gmail.com> | 2021-02-03 20:58:39 +0100 |
---|---|---|
committer | Pali Rohár <pali.rohar@gmail.com> | 2021-02-03 20:58:39 +0100 |
commit | c172ee216cd5d46c1999018e1a3e905eebe3c10c (patch) | |
tree | e1f7da079d4cbc5907b23b8fc3a6a678dbdeb858 /src | |
parent | b89f89323c13a9804d9d521ea9800983f955560a (diff) | |
download | dlfcn-win32-c172ee216cd5d46c1999018e1a3e905eebe3c10c.tar.gz dlfcn-win32-c172ee216cd5d46c1999018e1a3e905eebe3c10c.tar.bz2 dlfcn-win32-c172ee216cd5d46c1999018e1a3e905eebe3c10c.zip |
Avoid calling SetLastError() and GetLastError() internally
There is no need for propagating internal errors via SetLastError() and
GetLastError() calls. Just use additional argument for save_err_str()
function. Also dlfcn API is POSIX and does not use GetLastError().
Diffstat (limited to 'src')
-rw-r--r-- | src/dlfcn.c | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/src/dlfcn.c b/src/dlfcn.c index 8cd2fc9..33fa325 100644 --- a/src/dlfcn.c +++ b/src/dlfcn.c | |||
@@ -98,10 +98,7 @@ static BOOL local_add( HMODULE hModule ) | |||
98 | nobject = (local_object *) malloc( sizeof( local_object ) ); | 98 | nobject = (local_object *) malloc( sizeof( local_object ) ); |
99 | 99 | ||
100 | if( !nobject ) | 100 | if( !nobject ) |
101 | { | ||
102 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); | ||
103 | return FALSE; | 101 | return FALSE; |
104 | } | ||
105 | 102 | ||
106 | pobject->next = nobject; | 103 | pobject->next = nobject; |
107 | nobject->next = NULL; | 104 | nobject->next = NULL; |
@@ -139,17 +136,11 @@ static void local_rem( HMODULE hModule ) | |||
139 | static char error_buffer[65535]; | 136 | static char error_buffer[65535]; |
140 | static BOOL error_occurred; | 137 | static BOOL error_occurred; |
141 | 138 | ||
142 | static void save_err_str( const char *str ) | 139 | static void save_err_str( const char *str, DWORD dwMessageId ) |
143 | { | 140 | { |
144 | DWORD dwMessageId; | ||
145 | DWORD ret; | 141 | DWORD ret; |
146 | size_t pos, len; | 142 | size_t pos, len; |
147 | 143 | ||
148 | dwMessageId = GetLastError( ); | ||
149 | |||
150 | if( dwMessageId == 0 ) | ||
151 | return; | ||
152 | |||
153 | len = strlen( str ); | 144 | len = strlen( str ); |
154 | if( len > sizeof( error_buffer ) - 5 ) | 145 | if( len > sizeof( error_buffer ) - 5 ) |
155 | len = sizeof( error_buffer ) - 5; | 146 | len = sizeof( error_buffer ) - 5; |
@@ -185,7 +176,7 @@ static void save_err_str( const char *str ) | |||
185 | error_occurred = TRUE; | 176 | error_occurred = TRUE; |
186 | } | 177 | } |
187 | 178 | ||
188 | static void save_err_ptr_str( const void *ptr ) | 179 | static void save_err_ptr_str( const void *ptr, DWORD dwMessageId ) |
189 | { | 180 | { |
190 | char ptr_buf[2 + 2 * sizeof( ptr ) + 1]; | 181 | char ptr_buf[2 + 2 * sizeof( ptr ) + 1]; |
191 | char num; | 182 | char num; |
@@ -202,7 +193,7 @@ static void save_err_ptr_str( const void *ptr ) | |||
202 | 193 | ||
203 | ptr_buf[2 + 2 * sizeof( ptr )] = 0; | 194 | ptr_buf[2 + 2 * sizeof( ptr )] = 0; |
204 | 195 | ||
205 | save_err_str( ptr_buf ); | 196 | save_err_str( ptr_buf, dwMessageId ); |
206 | } | 197 | } |
207 | 198 | ||
208 | /* Load Psapi.dll at runtime, this avoids linking caveat */ | 199 | /* Load Psapi.dll at runtime, this avoids linking caveat */ |
@@ -249,7 +240,7 @@ void *dlopen( const char *file, int mode ) | |||
249 | hModule = GetModuleHandle( NULL ); | 240 | hModule = GetModuleHandle( NULL ); |
250 | 241 | ||
251 | if( !hModule ) | 242 | if( !hModule ) |
252 | save_err_str( "(null)" ); | 243 | save_err_str( "(null)", GetLastError( ) ); |
253 | } | 244 | } |
254 | else | 245 | else |
255 | { | 246 | { |
@@ -262,8 +253,7 @@ void *dlopen( const char *file, int mode ) | |||
262 | 253 | ||
263 | if( len >= sizeof( lpFileName ) ) | 254 | if( len >= sizeof( lpFileName ) ) |
264 | { | 255 | { |
265 | SetLastError( ERROR_FILENAME_EXCED_RANGE ); | 256 | save_err_str( file, ERROR_FILENAME_EXCED_RANGE ); |
266 | save_err_str( file ); | ||
267 | hModule = NULL; | 257 | hModule = NULL; |
268 | } | 258 | } |
269 | else | 259 | else |
@@ -292,7 +282,7 @@ void *dlopen( const char *file, int mode ) | |||
292 | 282 | ||
293 | if( !hModule ) | 283 | if( !hModule ) |
294 | { | 284 | { |
295 | save_err_str( lpFileName ); | 285 | save_err_str( lpFileName, GetLastError( ) ); |
296 | } | 286 | } |
297 | else | 287 | else |
298 | { | 288 | { |
@@ -312,7 +302,7 @@ void *dlopen( const char *file, int mode ) | |||
312 | { | 302 | { |
313 | if( !local_add( hModule ) ) | 303 | if( !local_add( hModule ) ) |
314 | { | 304 | { |
315 | save_err_str( lpFileName ); | 305 | save_err_str( lpFileName, ERROR_NOT_ENOUGH_MEMORY ); |
316 | FreeLibrary( hModule ); | 306 | FreeLibrary( hModule ); |
317 | hModule = NULL; | 307 | hModule = NULL; |
318 | } | 308 | } |
@@ -347,7 +337,7 @@ int dlclose( void *handle ) | |||
347 | if( ret ) | 337 | if( ret ) |
348 | local_rem( hModule ); | 338 | local_rem( hModule ); |
349 | else | 339 | else |
350 | save_err_ptr_str( handle ); | 340 | save_err_ptr_str( handle, GetLastError( ) ); |
351 | 341 | ||
352 | /* dlclose's return value in inverted in relation to FreeLibrary's. */ | 342 | /* dlclose's return value in inverted in relation to FreeLibrary's. */ |
353 | ret = !ret; | 343 | ret = !ret; |
@@ -362,12 +352,14 @@ void *dlsym( void *handle, const char *name ) | |||
362 | FARPROC symbol; | 352 | FARPROC symbol; |
363 | HMODULE hCaller; | 353 | HMODULE hCaller; |
364 | HMODULE hModule; | 354 | HMODULE hModule; |
355 | DWORD dwMessageId; | ||
365 | 356 | ||
366 | error_occurred = FALSE; | 357 | error_occurred = FALSE; |
367 | 358 | ||
368 | symbol = NULL; | 359 | symbol = NULL; |
369 | hCaller = NULL; | 360 | hCaller = NULL; |
370 | hModule = GetModuleHandle( NULL ); | 361 | hModule = GetModuleHandle( NULL ); |
362 | dwMessageId = 0; | ||
371 | 363 | ||
372 | if( handle == RTLD_DEFAULT ) | 364 | if( handle == RTLD_DEFAULT ) |
373 | { | 365 | { |
@@ -388,7 +380,10 @@ void *dlsym( void *handle, const char *name ) | |||
388 | * use standard GetModuleHandleExA() function. | 380 | * use standard GetModuleHandleExA() function. |
389 | */ | 381 | */ |
390 | if( !GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) _ReturnAddress( ), &hCaller ) ) | 382 | if( !GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) _ReturnAddress( ), &hCaller ) ) |
383 | { | ||
384 | dwMessageId = ERROR_INVALID_PARAMETER; | ||
391 | goto end; | 385 | goto end; |
386 | } | ||
392 | } | 387 | } |
393 | 388 | ||
394 | if( handle != RTLD_NEXT ) | 389 | if( handle != RTLD_NEXT ) |
@@ -448,7 +443,7 @@ void *dlsym( void *handle, const char *name ) | |||
448 | } | 443 | } |
449 | else | 444 | else |
450 | { | 445 | { |
451 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); | 446 | dwMessageId = ERROR_NOT_ENOUGH_MEMORY; |
452 | goto end; | 447 | goto end; |
453 | } | 448 | } |
454 | } | 449 | } |
@@ -457,9 +452,9 @@ void *dlsym( void *handle, const char *name ) | |||
457 | end: | 452 | end: |
458 | if( symbol == NULL ) | 453 | if( symbol == NULL ) |
459 | { | 454 | { |
460 | if( GetLastError() == 0 ) | 455 | if( !dwMessageId ) |
461 | SetLastError( ERROR_PROC_NOT_FOUND ); | 456 | dwMessageId = ERROR_PROC_NOT_FOUND; |
462 | save_err_str( name ); | 457 | save_err_str( name, dwMessageId ); |
463 | } | 458 | } |
464 | 459 | ||
465 | return *(void **) (&symbol); | 460 | return *(void **) (&symbol); |