[KERNEL32] Improve path name handling
[reactos.git] / 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);