Correctly process and indicate error when file name is too long
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
diff --git a/dlfcn.c b/dlfcn.c
index 7fe4701..ec60423 100644
--- a/dlfcn.c
+++ b/dlfcn.c
@@ -242,63 +242,71 @@ void *dlopen( const char *file, int mode )
HANDLE hCurrentProc;
DWORD dwProcModsBefore, dwProcModsAfter;
char lpFileName[MAX_PATH];
- size_t i;
-
- /* MSDN says backslashes *must* be used instead of forward slashes. */
- for( i = 0 ; i < sizeof(lpFileName) - 1 ; i ++ )
- {
- if( !file[i] )
- break;
- else if( file[i] == '/' )
- lpFileName[i] = '\\';
- else
- lpFileName[i] = file[i];
- }
- lpFileName[i] = '\0';
-
- hCurrentProc = GetCurrentProcess( );
+ size_t i, len;
- if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsBefore ) == 0 )
- dwProcModsBefore = 0;
+ len = strlen( file );
- /* POSIX says the search path is implementation-defined.
- * LOAD_WITH_ALTERED_SEARCH_PATH is used to make it behave more closely
- * to UNIX's search paths (start with system folders instead of current
- * folder).
- */
- hModule = LoadLibraryExA(lpFileName, NULL,
- LOAD_WITH_ALTERED_SEARCH_PATH );
-
- if( !hModule )
+ if( len >= sizeof( lpFileName ) )
{
- save_err_str( lpFileName );
+ SetLastError( ERROR_FILENAME_EXCED_RANGE );
+ save_err_str( file );
+ hModule = NULL;
}
else
{
- if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 )
- dwProcModsAfter = 0;
-
- /* If the object was loaded with RTLD_LOCAL, add it to list of local
- * objects, so that its symbols cannot be retrieved even if the handle for
- * the original program file is passed. POSIX says that if the same
- * file is specified in multiple invocations, and any of them are
- * RTLD_GLOBAL, even if any further invocations use RTLD_LOCAL, the
- * symbols will remain global. If number of loaded modules was not
- * changed after calling LoadLibraryEx(), it means that library was
- * already loaded.
+ /* MSDN says backslashes *must* be used instead of forward slashes. */
+ for( i = 0; i < len; i++ )
+ {
+ if( file[i] == '/' )
+ lpFileName[i] = '\\';
+ else
+ lpFileName[i] = file[i];
+ }
+ lpFileName[len] = '\0';
+
+ hCurrentProc = GetCurrentProcess( );
+
+ if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsBefore ) == 0 )
+ dwProcModsBefore = 0;
+
+ /* POSIX says the search path is implementation-defined.
+ * LOAD_WITH_ALTERED_SEARCH_PATH is used to make it behave more closely
+ * to UNIX's search paths (start with system folders instead of current
+ * folder).
*/
- if( (mode & RTLD_LOCAL) && dwProcModsBefore != dwProcModsAfter )
+ hModule = LoadLibraryExA( lpFileName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
+
+ if( !hModule )
{
- if( !local_add( hModule ) )
- {
- save_err_str( lpFileName );
- FreeLibrary( hModule );
- hModule = NULL;
- }
+ save_err_str( lpFileName );
}
- else if( !(mode & RTLD_LOCAL) && dwProcModsBefore == dwProcModsAfter )
+ else
{
- local_rem( hModule );
+ if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 )
+ dwProcModsAfter = 0;
+
+ /* If the object was loaded with RTLD_LOCAL, add it to list of local
+ * objects, so that its symbols cannot be retrieved even if the handle for
+ * the original program file is passed. POSIX says that if the same
+ * file is specified in multiple invocations, and any of them are
+ * RTLD_GLOBAL, even if any further invocations use RTLD_LOCAL, the
+ * symbols will remain global. If number of loaded modules was not
+ * changed after calling LoadLibraryEx(), it means that library was
+ * already loaded.
+ */
+ if( (mode & RTLD_LOCAL) && dwProcModsBefore != dwProcModsAfter )
+ {
+ if( !local_add( hModule ) )
+ {
+ save_err_str( lpFileName );
+ FreeLibrary( hModule );
+ hModule = NULL;
+ }
+ }
+ else if( !(mode & RTLD_LOCAL) && dwProcModsBefore == dwProcModsAfter )
+ {
+ local_rem( hModule );
+ }
}
}
}