[KERNEL32] Improve path name handling
authorTimo Kreuzer <timo.kreuzer@reactos.org>
Sat, 27 Apr 2019 16:58:08 +0000 (18:58 +0200)
committerTimo Kreuzer <timo.kreuzer@reactos.org>
Sat, 20 Jul 2019 11:56:18 +0000 (13:56 +0200)
Fixes GCC 8 warning:
dll/win32/kernel32/client/loader.c: In function 'LoadLibraryA':
dll/win32/kernel32/client/loader.c:129:17: error: 'strncat' specified bound 13 equals source length [-Werror=stringop-overflow=]
                 strncat(PathBuffer, "\\twain_32.dll", 13);
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

dll/win32/kernel32/client/loader.c

index 905f6ae..ec41114 100644 (file)
@@ -110,23 +110,25 @@ WINAPI
 DECLSPEC_HOTPATCH
 LoadLibraryA(LPCSTR lpLibFileName)
 {
+    static const CHAR TwainDllName[] = "twain_32.dll";
     LPSTR PathBuffer;
     UINT Len;
     HINSTANCE Result;
 
     /* Treat twain_32.dll in a special way (what a surprise...) */
-    if (lpLibFileName && !_strcmpi(lpLibFileName, "twain_32.dll"))
+    if (lpLibFileName && !_strcmpi(lpLibFileName, TwainDllName))
     {
         /* Allocate space for the buffer */
-        PathBuffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, MAX_PATH);
+        PathBuffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, MAX_PATH + sizeof(ANSI_NULL));
         if (PathBuffer)
         {
             /* Get windows dir in this buffer */
-            Len = GetWindowsDirectoryA(PathBuffer, MAX_PATH - 13); /* 13 is sizeof of '\\twain_32.dll' */
-            if (Len && Len < (MAX_PATH - 13))
+            Len = GetWindowsDirectoryA(PathBuffer, MAX_PATH);
+            if ((Len != 0) && (Len < (MAX_PATH - sizeof(TwainDllName) - sizeof('\\'))))
             {
                 /* We successfully got windows directory. Concatenate twain_32.dll to it */
-                strncat(PathBuffer, "\\twain_32.dll", 13);
+                PathBuffer[Len] = '\\';
+                strcpy(&PathBuffer[Len + 1], TwainDllName);
 
                 /* And recursively call ourselves with a new string */
                 Result = LoadLibraryA(PathBuffer);