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().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
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 )
nobject = (local_object *) malloc( sizeof( local_object ) );
if( !nobject )
- {
- SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return FALSE;
- }
pobject->next = nobject;
nobject->next = NULL;
@@ -139,17 +136,11 @@ static void local_rem( HMODULE hModule )
static char error_buffer[65535];
static BOOL error_occurred;
-static void save_err_str( const char *str )
+static void save_err_str( const char *str, DWORD dwMessageId )
{
- DWORD dwMessageId;
DWORD ret;
size_t pos, len;
- dwMessageId = GetLastError( );
-
- if( dwMessageId == 0 )
- return;
-
len = strlen( str );
if( len > sizeof( error_buffer ) - 5 )
len = sizeof( error_buffer ) - 5;
@@ -185,7 +176,7 @@ static void save_err_str( const char *str )
error_occurred = TRUE;
}
-static void save_err_ptr_str( const void *ptr )
+static void save_err_ptr_str( const void *ptr, DWORD dwMessageId )
{
char ptr_buf[2 + 2 * sizeof( ptr ) + 1];
char num;
@@ -202,7 +193,7 @@ static void save_err_ptr_str( const void *ptr )
ptr_buf[2 + 2 * sizeof( ptr )] = 0;
- save_err_str( ptr_buf );
+ save_err_str( ptr_buf, dwMessageId );
}
/* Load Psapi.dll at runtime, this avoids linking caveat */
@@ -249,7 +240,7 @@ void *dlopen( const char *file, int mode )
hModule = GetModuleHandle( NULL );
if( !hModule )
- save_err_str( "(null)" );
+ save_err_str( "(null)", GetLastError( ) );
}
else
{
@@ -262,8 +253,7 @@ void *dlopen( const char *file, int mode )
if( len >= sizeof( lpFileName ) )
{
- SetLastError( ERROR_FILENAME_EXCED_RANGE );
- save_err_str( file );
+ save_err_str( file, ERROR_FILENAME_EXCED_RANGE );
hModule = NULL;
}
else
@@ -292,7 +282,7 @@ void *dlopen( const char *file, int mode )
if( !hModule )
{
- save_err_str( lpFileName );
+ save_err_str( lpFileName, GetLastError( ) );
}
else
{
@@ -312,7 +302,7 @@ void *dlopen( const char *file, int mode )
{
if( !local_add( hModule ) )
{
- save_err_str( lpFileName );
+ save_err_str( lpFileName, ERROR_NOT_ENOUGH_MEMORY );
FreeLibrary( hModule );
hModule = NULL;
}
@@ -347,7 +337,7 @@ int dlclose( void *handle )
if( ret )
local_rem( hModule );
else
- save_err_ptr_str( handle );
+ save_err_ptr_str( handle, GetLastError( ) );
/* dlclose's return value in inverted in relation to FreeLibrary's. */
ret = !ret;
@@ -362,12 +352,14 @@ void *dlsym( void *handle, const char *name )
FARPROC symbol;
HMODULE hCaller;
HMODULE hModule;
+ DWORD dwMessageId;
error_occurred = FALSE;
symbol = NULL;
hCaller = NULL;
hModule = GetModuleHandle( NULL );
+ dwMessageId = 0;
if( handle == RTLD_DEFAULT )
{
@@ -388,7 +380,10 @@ void *dlsym( void *handle, const char *name )
* use standard GetModuleHandleExA() function.
*/
if( !GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) _ReturnAddress( ), &hCaller ) )
+ {
+ dwMessageId = ERROR_INVALID_PARAMETER;
goto end;
+ }
}
if( handle != RTLD_NEXT )
@@ -448,7 +443,7 @@ void *dlsym( void *handle, const char *name )
}
else
{
- SetLastError( ERROR_NOT_ENOUGH_MEMORY );
+ dwMessageId = ERROR_NOT_ENOUGH_MEMORY;
goto end;
}
}
@@ -457,9 +452,9 @@ void *dlsym( void *handle, const char *name )
end:
if( symbol == NULL )
{
- if( GetLastError() == 0 )
- SetLastError( ERROR_PROC_NOT_FOUND );
- save_err_str( name );
+ if( !dwMessageId )
+ dwMessageId = ERROR_PROC_NOT_FOUND;
+ save_err_str( name, dwMessageId );
}
return *(void **) (&symbol);