Use StackString in PAL file apis.
authorLakshmi Priya Sekar <lasekar@microsoft.com>
Wed, 7 Oct 2015 01:19:32 +0000 (18:19 -0700)
committerLakshmi Priya Sekar <lasekar@microsoft.com>
Mon, 19 Oct 2015 06:53:19 +0000 (23:53 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/01a7eb3df39e828c0c83980ff2d32bc49b3db73b

14 files changed:
src/coreclr/src/pal/src/file/directory.cpp
src/coreclr/src/pal/src/file/disk.cpp
src/coreclr/src/pal/src/file/file.cpp
src/coreclr/src/pal/src/file/find.cpp
src/coreclr/src/pal/src/file/path.cpp
src/coreclr/src/pal/src/include/pal/map.hpp
src/coreclr/src/pal/src/include/pal/stackstring.hpp
src/coreclr/src/pal/src/loader/module.cpp
src/coreclr/src/pal/src/locale/unicode.cpp
src/coreclr/src/pal/src/misc/perftrace.cpp
src/coreclr/src/pal/src/shmemory/shmemory.cpp
src/coreclr/src/pal/src/synchmgr/synchmanager.cpp
src/coreclr/src/pal/src/thread/process.cpp
src/coreclr/src/pal/tests/palsuite/file_io/SearchPathA/test1/SearchPathA.c

index bf4780f..3a1747c 100644 (file)
@@ -24,6 +24,7 @@ Revision History:
 #include "pal/palinternal.h"
 #include "pal/dbgmsg.h"
 #include "pal/file.h"
+#include "pal/stackstring.hpp"
 
 #if HAVE_ALLOCA_H
 #include <alloca.h>
@@ -195,8 +196,10 @@ RemoveDirectoryA(
 {
     DWORD dwLastError = 0;
     BOOL  bRet = FALSE;
-    char  mb_dir[MAX_LONGPATH];
-
+    PathCharString mb_dirPathString;
+    size_t length;
+    char * mb_dir;
+    
     PERF_ENTRY(RemoveDirectoryA);
     ENTRY("RemoveDirectoryA(lpPathName=%p (%s))\n",
           lpPathName,
@@ -208,14 +211,17 @@ RemoveDirectoryA(
         goto done;
     }
 
-    mb_dir[MAX_LONGPATH - 1] = '\0';
-    if (strncpy_s (mb_dir, sizeof(mb_dir), lpPathName, MAX_LONGPATH) != SAFECRT_SUCCESS)
+    length = strlen(lpPathName);
+    mb_dir = mb_dirPathString.OpenStringBuffer(length);
+    if (strncpy_s (mb_dir, sizeof(char) * (length+1), lpPathName, MAX_LONGPATH) != SAFECRT_SUCCESS)
     {
+        mb_dirPathString.CloseBuffer(length);
         WARN("mb_dir is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH);
         dwLastError = ERROR_FILENAME_EXCED_RANGE;
         goto done;
     }
 
+    mb_dirPathString.CloseBuffer(length);
     bRet = RemoveDirectoryHelper (mb_dir, &dwLastError);
 
 done:
@@ -240,10 +246,12 @@ PALAPI
 RemoveDirectoryW(
          IN LPCWSTR lpPathName)
 {
-    char  mb_dir[MAX_LONGPATH];
+    PathCharString mb_dirPathString;
     int   mb_size;
     DWORD dwLastError = 0;
     BOOL  bRet = FALSE;
+    size_t length;
+    char * mb_dir;
 
     PERF_ENTRY(RemoveDirectoryW);
     ENTRY("RemoveDirectoryW(lpPathName=%p (%S))\n",
@@ -255,9 +263,13 @@ RemoveDirectoryW(
         dwLastError = ERROR_PATH_NOT_FOUND;
         goto done;
     }
-    
-    mb_size = WideCharToMultiByte( CP_ACP, 0, lpPathName, -1, mb_dir, MAX_LONGPATH,
+
+    length = (PAL_wcslen(lpPathName)+1) * 3;
+    mb_dir = mb_dirPathString.OpenStringBuffer(length);
+    mb_size = WideCharToMultiByte( CP_ACP, 0, lpPathName, -1, mb_dir, length,
                                    NULL, NULL );
+    mb_dirPathString.CloseBuffer(mb_size);
+
     if( mb_size == 0 )
     {
         dwLastError = GetLastError();
@@ -429,9 +441,11 @@ SetCurrentDirectoryW(
 {
     BOOL bRet;
     DWORD dwLastError = 0;
-    char dir[MAX_LONGPATH];
+    PathCharString dirPathString;
     int  size;
-
+    size_t length;
+    char * dir;
+    
     PERF_ENTRY(SetCurrentDirectoryW);
     ENTRY("SetCurrentDirectoryW(lpPathName=%p (%S))\n",
           lpPathName?lpPathName:W16_NULLSTRING,
@@ -447,8 +461,12 @@ SetCurrentDirectoryW(
         goto done;
     }
 
-    size = WideCharToMultiByte( CP_ACP, 0, lpPathName, -1, dir, MAX_LONGPATH,
+    length = (PAL_wcslen(lpPathName)+1) * 3;
+    dir = dirPathString.OpenStringBuffer(length);
+    size = WideCharToMultiByte( CP_ACP, 0, lpPathName, -1, dir, length,
                                 NULL, NULL );
+    dirPathString.CloseBuffer(size);
+    
     if( size == 0 )
     {
         dwLastError = GetLastError();
index 4bf57da..2c43fa7 100644 (file)
@@ -24,6 +24,7 @@ Revision History:
 #include "pal/palinternal.h"
 #include "pal/dbgmsg.h"
 #include "pal/file.h"
+#include "pal/stackstring.hpp"
 
 #include <sys/param.h>
 #if !defined(_AIX)
@@ -68,7 +69,10 @@ GetDiskFreeSpaceW(
     pal_statfs fsInfoBuffer;
     INT  statfsRetVal = 0;
     DWORD dwLastError = NO_ERROR;
-    CHAR DirNameBuffer[ MAX_LONGPATH ];
+    PathCharString dirNameBufferPathString;
+    size_t length;
+    char * dirNameBuffer;
+    int size;
 
     PERF_ENTRY(GetDiskFreeSpaceW);
     ENTRY( "GetDiskFreeSpaceW( lpDirectoryName=%p (%S), lpSectorsPerCluster=%p,"
@@ -111,11 +115,15 @@ GetDiskFreeSpaceW(
 
     if ( lpDirectoryName )
     {
-        if ( WideCharToMultiByte( CP_ACP, 0, lpDirectoryName, -1,
-                                  DirNameBuffer,MAX_LONGPATH, 0, 0 ) != 0 )
+        length = (PAL_wcslen(lpDirectoryName)+1) * 3;
+        dirNameBuffer = dirNameBufferPathString.OpenStringBuffer(length);
+        size = WideCharToMultiByte( CP_ACP, 0, lpDirectoryName, -1,
+                                  dirNameBuffer,length, 0, 0 );
+        dirNameBufferPathString.CloseBuffer(size);
+        if ( size != 0 )
         {
-            FILEDosToUnixPathA( DirNameBuffer );
-            statfsRetVal = statfs( DirNameBuffer, &fsInfoBuffer );
+            FILEDosToUnixPathA( dirNameBuffer );
+            statfsRetVal = statfs( dirNameBuffer, &fsInfoBuffer );
         }
         else
         {
@@ -139,7 +147,7 @@ GetDiskFreeSpaceW(
     {
         if ( errno == ENOTDIR || errno == ENOENT )
         {
-            FILEGetProperNotFoundError( DirNameBuffer, &dwLastError );
+            FILEGetProperNotFoundError( dirNameBuffer, &dwLastError );
             goto exit;
         }
         dwLastError = FILEGetLastErrorFromErrno();
index c4ac214..33210d4 100644 (file)
@@ -23,6 +23,7 @@ Abstract:
 #include "pal/file.hpp"
 #include "shmfilelockmgr.hpp"
 #include "pal/malloc.hpp"
+#include "pal/stackstring.hpp"
 
 #include "pal/palinternal.h"
 #include "pal/dbgmsg.h"
@@ -44,6 +45,8 @@ using namespace CorUnix;
 
 SET_DEFAULT_DEBUG_CHANNEL(FILE);
 
+int MaxWCharToAcpLengthRatio = 3;
+
 PAL_ERROR
 InternalSetFilePointerForUnixFd(
     int iUnixFd,
@@ -974,8 +977,10 @@ CreateFileW(
 {
     CPalThread *pThread;
     PAL_ERROR palError = NO_ERROR;
-    char    name[MAX_LONGPATH];
-    int     size;
+    PathCharString namePathString;
+    char * name;
+    int size;
+    int length = 0;
     HANDLE  hRet = INVALID_HANDLE_VALUE;
 
     PERF_ENTRY(CreateFileW);
@@ -988,8 +993,16 @@ CreateFileW(
 
     pThread = InternalGetCurrentThread();
 
-    size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, name, MAX_LONGPATH,
+    if (lpFileName != NULL)
+    {
+        length = (PAL_wcslen(lpFileName)+1) * MaxWCharToAcpLengthRatio;
+    }
+    
+    name = namePathString.OpenStringBuffer(length);
+    size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, name, length,
                                 NULL, NULL );
+    namePathString.CloseBuffer(size);    
+
     if( size == 0 )
     {
         DWORD dwLastError = GetLastError();
@@ -1053,10 +1066,12 @@ CopyFileW(
       IN BOOL bFailIfExists)
 {
     CPalThread *pThread;
-    char    source[MAX_LONGPATH];
-    char    dest[MAX_LONGPATH];
-    int     src_size,dest_size;
-    BOOL        bRet = FALSE;
+    PathCharString sourcePathString;
+    PathCharString destPathString;
+    char * source;
+    char * dest;
+    int src_size, dest_size, length = 0;
+    BOOL bRet = FALSE;
 
     PERF_ENTRY(CopyFileW);
     ENTRY("CopyFileW(lpExistingFileName=%p (%S), lpNewFileName=%p (%S), bFailIfExists=%d)\n",
@@ -1066,8 +1081,16 @@ CopyFileW(
           lpNewFileName?lpNewFileName:W16_NULLSTRING, bFailIfExists);
 
     pThread = InternalGetCurrentThread();
-    src_size = WideCharToMultiByte( CP_ACP, 0, lpExistingFileName, -1, source, MAX_LONGPATH,
+    if (lpExistingFileName != NULL)
+    {
+        length = (PAL_wcslen(lpExistingFileName)+1) * MaxWCharToAcpLengthRatio;
+    }
+    
+    source = sourcePathString.OpenStringBuffer(length);
+    src_size = WideCharToMultiByte( CP_ACP, 0, lpExistingFileName, -1, source, length,
                                 NULL, NULL );
+    sourcePathString.CloseBuffer(src_size);
+    
     if( src_size == 0 )
     {
         DWORD dwLastError = GetLastError();
@@ -1084,8 +1107,17 @@ CopyFileW(
         goto done;
     }
 
-    dest_size = WideCharToMultiByte( CP_ACP, 0, lpNewFileName, -1, dest, MAX_LONGPATH,
+    length = 0;
+    if (lpNewFileName != NULL)
+    {
+        length = (PAL_wcslen(lpNewFileName)+1) * MaxWCharToAcpLengthRatio;
+    }
+    
+    dest = destPathString.OpenStringBuffer(length);
+    dest_size = WideCharToMultiByte( CP_ACP, 0, lpNewFileName, -1, dest, length,
                                 NULL, NULL );
+    destPathString.CloseBuffer(dest_size);
+    
     if( dest_size == 0 )
     {
         DWORD dwLastError = GetLastError();
@@ -1128,7 +1160,9 @@ DeleteFileA(
     int     result;
     BOOL    bRet = FALSE;
     DWORD   dwLastError = 0;
-    char    lpUnixFileName[MAX_LONGPATH];
+    char * lpUnixFileName;
+    int length;
+    PathCharString lpUnixFileNamePS;
     LPSTR lpFullUnixFileName = NULL;
     DWORD cchFullUnixFileName = MAX_LONGPATH+1;// InternalCanonicalizeRealPath requires this to be atleast PATH_MAX
 
@@ -1136,14 +1170,17 @@ DeleteFileA(
     ENTRY("DeleteFileA(lpFileName=%p (%s))\n", lpFileName?lpFileName:"NULL", lpFileName?lpFileName:"NULL");
 
     pThread = InternalGetCurrentThread();
-    if (strlen(lpFileName) >= MAX_LONGPATH)
+    length = strlen(lpFileName);
+    if (length >= MAX_LONGPATH)
     {
         WARN("lpFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH);
         pThread->SetLastError(ERROR_FILENAME_EXCED_RANGE);
         goto done;
     }
 
-    strcpy_s( lpUnixFileName, sizeof(lpUnixFileName), lpFileName);
+    lpUnixFileName = lpUnixFileNamePS.OpenStringBuffer(length);
+    strcpy_s( lpUnixFileName, lpUnixFileNamePS.GetSizeOf(), lpFileName);
+    lpUnixFileNamePS.CloseBuffer(length);
     
     FILEDosToUnixPathA( lpUnixFileName );
     
@@ -1241,7 +1278,9 @@ DeleteFileW(
 {
     CPalThread *pThread;
     int  size;
-    char name[MAX_LONGPATH];
+    PathCharString namePS;
+    char * name;
+    int length = 0;
     BOOL bRet = FALSE;
 
     PERF_ENTRY(DeleteFileW);
@@ -1250,8 +1289,17 @@ DeleteFileW(
       lpFileName?lpFileName:W16_NULLSTRING);
 
     pThread = InternalGetCurrentThread();
-    size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, name, MAX_LONGPATH,
+    
+    if (lpFileName != NULL)
+    {
+        length = (PAL_wcslen(lpFileName)+1) * MaxWCharToAcpLengthRatio;
+    }
+    
+    name = namePS.OpenStringBuffer(length);
+    size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, name, length,
                                 NULL, NULL );
+    namePS.CloseBuffer(size);
+    
     if( size == 0 )
     {
         DWORD dwLastError = GetLastError();
@@ -1354,8 +1402,11 @@ MoveFileExA(
 {
     CPalThread *pThread;
     int   result;
-    char  source[MAX_LONGPATH];
-    char  dest[MAX_LONGPATH];
+    int length = 0;
+    PathCharString sourcePS;
+    PathCharString destPS;
+    char * source;
+    char * dest;
     BOOL  bRet = TRUE;
     DWORD dwLastError = 0;
 
@@ -1376,26 +1427,32 @@ MoveFileExA(
         goto done;
     }
 
-    if (strlen(lpExistingFileName) >= MAX_LONGPATH)
+    length = strlen(lpExistingFileName);
+    if (length >= MAX_LONGPATH)
     {
         WARN("lpExistingFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH);
         pThread->SetLastError(ERROR_FILENAME_EXCED_RANGE);
         goto done;
     }
     
-    strcpy_s( source, sizeof(source), lpExistingFileName);
-
+    source = sourcePS.OpenStringBuffer(length);
+    strcpy_s( source, sourcePS.GetSizeOf(), lpExistingFileName);
+    sourcePS.CloseBuffer(length);
+    
     FILEDosToUnixPathA( source );
 
-    if (strlen(lpNewFileName) >= MAX_LONGPATH)
+    length = strlen(lpNewFileName);
+    if (length >= MAX_LONGPATH)
     {
         WARN("lpNewFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH);
         pThread->SetLastError(ERROR_FILENAME_EXCED_RANGE);
         goto done;
     }
     
-    strcpy_s( dest, sizeof(dest), lpNewFileName);
-
+    dest = destPS.OpenStringBuffer(length);
+    strcpy_s( dest, destPS.GetSizeOf(), lpNewFileName);
+    destPS.CloseBuffer(length);
+    
     FILEDosToUnixPathA( dest );
 
     if ( !FILEGetFileNameFromSymLink(source))
@@ -1527,8 +1584,11 @@ MoveFileExW(
         IN DWORD dwFlags)
 {
     CPalThread *pThread;
-    char    source[MAX_LONGPATH];
-    char    dest[MAX_LONGPATH];
+    PathCharString sourcePS;
+    PathCharString destPS;
+    char * source;
+    char * dest;
+    int length = 0;
     int     src_size,dest_size;
     BOOL        bRet = FALSE;
 
@@ -1540,8 +1600,16 @@ MoveFileExW(
           lpNewFileName?lpNewFileName:W16_NULLSTRING, dwFlags);
 
     pThread = InternalGetCurrentThread();
-    src_size = WideCharToMultiByte( CP_ACP, 0, lpExistingFileName, -1, source, MAX_LONGPATH,
+    
+    if (lpExistingFileName != NULL)
+    {
+        length = (PAL_wcslen(lpExistingFileName)+1) * MaxWCharToAcpLengthRatio;
+    }
+    
+    source = sourcePS.OpenStringBuffer(length);
+    src_size = WideCharToMultiByte( CP_ACP, 0, lpExistingFileName, -1, source, length,
                                 NULL, NULL );
+    sourcePS.CloseBuffer(src_size);
     if( src_size == 0 )
     {
         DWORD dwLastError = GetLastError();
@@ -1558,8 +1626,17 @@ MoveFileExW(
         goto done;
     }
 
-    dest_size = WideCharToMultiByte( CP_ACP, 0, lpNewFileName, -1, dest, MAX_LONGPATH,
+    length = 0;
+    if (lpNewFileName != NULL)
+    {
+        length = (PAL_wcslen(lpNewFileName)+1) * MaxWCharToAcpLengthRatio;
+    }
+    
+    dest = destPS.OpenStringBuffer(length);
+    dest_size = WideCharToMultiByte( CP_ACP, 0, lpNewFileName, -1, dest, length,
                                 NULL, NULL );
+    destPS.CloseBuffer(dest_size);
+    
     if( dest_size == 0 )
     {
         DWORD dwLastError = GetLastError();
@@ -1618,7 +1695,9 @@ GetFileAttributesA(
     struct stat stat_data;
     DWORD dwAttr = 0;
     DWORD dwLastError = 0;
-    CHAR UnixFileName[MAX_LONGPATH + 1];
+    CHAR * UnixFileName;
+    int length = 0;
+    PathCharString UnixFileNamePS;
 
     PERF_ENTRY(GetFileAttributesA);
     ENTRY("GetFileAttributesA(lpFileName=%p (%s))\n", lpFileName?lpFileName:"NULL", lpFileName?lpFileName:"NULL");
@@ -1630,15 +1709,18 @@ GetFileAttributesA(
         goto done;
     }
 
-    if (strlen(lpFileName) >= MAX_LONGPATH) 
+    length = strlen(lpFileName);
+    if (length >= MAX_LONGPATH) 
     {
         WARN("lpFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH);
         dwLastError = ERROR_FILENAME_EXCED_RANGE;
         goto done;
     }
     
-    strcpy_s( UnixFileName, sizeof(UnixFileName), lpFileName );
-
+    UnixFileName = UnixFileNamePS.OpenStringBuffer(length);
+    strcpy_s( UnixFileName, UnixFileNamePS.GetSizeOf(), lpFileName );
+    UnixFileNamePS.CloseBuffer(length);
+    
     FILEDosToUnixPathA( UnixFileName );
 
     if ( stat(UnixFileName, &stat_data) != 0 )
@@ -1701,7 +1783,9 @@ GetFileAttributesW(
 {
     CPalThread *pThread;
     int   size;
-    char  filename[MAX_LONGPATH];
+    PathCharString filenamePS;
+    int length = 0;
+    char * filename;
     DWORD dwRet = (DWORD) -1;
 
     PERF_ENTRY(GetFileAttributesW);
@@ -1716,8 +1800,12 @@ GetFileAttributesW(
         goto done;
     }
     
-    size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, filename, MAX_LONGPATH,
+    length = (PAL_wcslen(lpFileName)+1) * MaxWCharToAcpLengthRatio;
+    filename = filenamePS.OpenStringBuffer(length);
+    size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, filename, length,
                                 NULL, NULL );
+    filenamePS.CloseBuffer(size);
+    
     if( size == 0 )
     {
         DWORD dwLastError = GetLastError();
@@ -1761,7 +1849,9 @@ GetFileAttributesExW(
 
     struct stat stat_data;
 
-    char name[MAX_LONGPATH];
+    char * name;
+    PathCharString namePS;
+    int length = 0;
     int  size;
 
     PERF_ENTRY(GetFileAttributesExW);
@@ -1790,8 +1880,12 @@ GetFileAttributesExW(
         goto done;
     }
     
-    size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, name, MAX_LONGPATH,
+    length = (PAL_wcslen(lpFileName)+1) * MaxWCharToAcpLengthRatio;
+    name = namePS.OpenStringBuffer(length);
+    size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, name, length,
                                 NULL, NULL );
+    namePS.CloseBuffer(size);
+    
     if( size == 0 )
     {
         dwLastError = GetLastError();
@@ -1871,7 +1965,9 @@ SetFileAttributesW(
            IN DWORD dwFileAttributes)
 {
     CPalThread *pThread;
-    char name[MAX_LONGPATH];
+    char * name;
+    PathCharString namePS;
+    int length = 0;
     int  size;
 
     DWORD dwLastError = 0;
@@ -1889,8 +1985,12 @@ SetFileAttributesW(
         goto done;
     }
     
-    size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, name, MAX_LONGPATH,
+    length = (PAL_wcslen(lpFileName)+1) * MaxWCharToAcpLengthRatio;
+    name = namePS.OpenStringBuffer(length);
+    size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, name, length,
                                 NULL, NULL );
+    namePS.CloseBuffer(size);
+    
     if( size == 0 )
     {
         dwLastError = GetLastError();
@@ -3367,8 +3467,11 @@ GetTempFileNameA(
                  OUT LPSTR lpTempFileName)
 {
     CPalThread *pThread;
-    CHAR    full_name[ MAX_LONGPATH + 1 ];
-    CHAR    file_template[ MAX_LONGPATH + 1 ];
+    CHAR * full_name;
+    PathCharString full_namePS;
+    int length;
+    CHAR * file_template;
+    PathCharString file_templatePS;
     CHAR    chLastPathNameChar;
  
     HANDLE  hTempFile;
@@ -3414,27 +3517,33 @@ GetTempFileNameA(
         goto done;
     }
 
+    length = strlen(lpPathName) + MAX_SEEDSIZE + MAX_PREFIX + 10;
+    file_template = file_templatePS.OpenStringBuffer(length);
     *file_template = '\0';
-    strcat_s( file_template, sizeof(file_template), lpPathName );
+    strcat_s( file_template, file_templatePS.GetSizeOf(), lpPathName );
+    file_templatePS.CloseBuffer(length);
 
     chLastPathNameChar = file_template[strlen(file_template)-1];
     if (chLastPathNameChar != '\\' && chLastPathNameChar != '/')
     {
-        strcat_s( file_template, sizeof(file_template), "\\" );
+        strcat_s( file_template, file_templatePS.GetSizeOf(), "\\" );
     }
     
     if ( lpPrefixString )
     {
-        strncat_s( file_template, sizeof(file_template), lpPrefixString, MAX_PREFIX );
+        strncat_s( file_template, file_templatePS.GetSizeOf(), lpPrefixString, MAX_PREFIX );
     }
     FILEDosToUnixPathA( file_template );
-    strncat_s( file_template, sizeof(file_template), "%.4x.TMP", MAX_SEEDSIZE );
+    strncat_s( file_template, file_templatePS.GetSizeOf(), "%.4x.TMP", MAX_SEEDSIZE );
 
     /* Create the file. */
     dwError = GetLastError();
     pThread->SetLastError( NOERROR );
 
-    sprintf_s( full_name, sizeof(full_name), file_template, (0 == uUnique) ? uUniqueSeed : uUnique);
+    length = strlen(file_template) + MAX_SEEDSIZE + MAX_PREFIX;
+    full_name = full_namePS.OpenStringBuffer(length);
+    sprintf_s( full_name, full_namePS.GetSizeOf(), file_template, (0 == uUnique) ? uUniqueSeed : uUnique);
+    full_namePS.CloseBuffer(length);
     
     hTempFile = CreateFileA( full_name, GENERIC_WRITE, 
                              FILE_SHARE_READ, NULL, CREATE_NEW, 0, NULL );
@@ -3453,7 +3562,7 @@ GetTempFileNameA(
             ENSURE_UNIQUE_NOT_ZERO;
 
             pThread->SetLastError( NOERROR );
-            sprintf_s( full_name, sizeof(full_name), file_template, uUniqueSeed );
+            sprintf_s( full_name, full_namePS.GetSizeOf(), file_template, uUniqueSeed );
             hTempFile = CreateFileA( full_name, GENERIC_WRITE, 
                                     FILE_SHARE_READ, NULL, CREATE_NEW, 0, NULL );
             uLoopCounter++;
@@ -3543,9 +3652,11 @@ GetTempFileNameW(
     CPalThread *pThread;
     INT path_size = 0;
     INT prefix_size = 0;
-    CHAR full_name[ MAX_LONGPATH + 1 ];
-    CHAR prefix_string[ MAX_LONGPATH + 1 ];
-    CHAR tempfile_name[ MAX_PATH_FNAME + 1 ];
+    CHAR * full_name;
+    CHAR * prefix_string;
+    CHAR * tempfile_name;
+    PathCharString full_namePS, prefix_stringPS;
+    INT length = 0;
     UINT   uRet;
 
     PERF_ENTRY(GetTempFileNameW);
@@ -3563,8 +3674,12 @@ GetTempFileNameW(
         goto done;
     }
 
+    length = (PAL_wcslen(lpPathName)+1) * MaxWCharToAcpLengthRatio;
+    full_name = full_namePS.OpenStringBuffer(length);
     path_size = WideCharToMultiByte( CP_ACP, 0, lpPathName, -1, full_name,
-                                     MAX_LONGPATH, NULL, NULL );
+                                     length, NULL, NULL );
+    full_namePS.CloseBuffer(path_size);
+                                     
     if( path_size == 0 )
     {
         DWORD dwLastError = GetLastError();
@@ -3584,10 +3699,14 @@ GetTempFileNameW(
     
     if (lpPrefixString != NULL) 
     {
+        length = (PAL_wcslen(lpPrefixString)+1) * MaxWCharToAcpLengthRatio;
+        prefix_string = prefix_stringPS.OpenStringBuffer(length);
         prefix_size = WideCharToMultiByte( CP_ACP, 0, lpPrefixString, -1, 
                                            prefix_string,
                                            MAX_LONGPATH - path_size - MAX_SEEDSIZE, 
                                            NULL, NULL );
+        prefix_stringPS.CloseBuffer(prefix_size);
+        
         if( prefix_size == 0 )
         {
             DWORD dwLastError = GetLastError();
@@ -3605,18 +3724,30 @@ GetTempFileNameW(
             goto done;
         }
     }
-       
+    
+    tempfile_name = new char[MAX_LONGPATH+1];
+    if (tempfile_name == NULL)
+    {
+        pThread->SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+        uRet = 0;
+        goto done;
+    }
+    
     uRet = GetTempFileNameA(full_name, 
                             (lpPrefixString == NULL) ? NULL : prefix_string,
                             0, tempfile_name);
-        
-    if ( uRet && !MultiByteToWideChar( CP_ACP, 0, tempfile_name, -1, 
-                                       lpTempFileName, MAX_PATH_FNAME ))
+                        
+    path_size = MultiByteToWideChar( CP_ACP, 0, tempfile_name, -1, 
+                                       lpTempFileName, MAX_LONGPATH );
+
+    delete [] tempfile_name;
+    tempfile_name = NULL;
+    if ( uRet && !path_size)
     {
         DWORD dwLastError = GetLastError();
         if (dwLastError == ERROR_INSUFFICIENT_BUFFER)
         {
-            WARN("File names larger than MAX_PATH_FNAME (%d)! \n", MAX_PATH_FNAME);
+            WARN("File names larger than MAX_PATH_FNAME (%d)! \n", MAX_LONGPATH);
             dwLastError = ERROR_FILENAME_EXCED_RANGE;
         }
         else
@@ -3628,6 +3759,7 @@ GetTempFileNameW(
         uRet = 0;
         goto done;
     }
+    
 
 done:
     LOGEXIT("GetTempFileNameW returns UINT %u\n", uRet);
@@ -4773,7 +4905,7 @@ Return value:
 BOOL FILEGetFileNameFromSymLink(char *source)
 {
     int ret;
-    char sLinkData[MAX_LONGPATH];
+    char * sLinkData = new char[MAX_LONGPATH+1];
 
     do
     {
@@ -4781,10 +4913,11 @@ BOOL FILEGetFileNameFromSymLink(char *source)
         if (ret>0)
         {
             sLinkData[ret] = '\0';
-            strcpy_s(source, sizeof(sLinkData), sLinkData);
+            strcpy_s(source, sizeof(char)*(MAX_LONGPATH+1), sLinkData);
         }
     } while (ret > 0);
 
+    delete [] sLinkData;
     return (errno == EINVAL);
 }
 
index c24962f..75d7bfd 100644 (file)
@@ -24,6 +24,7 @@ Revision History:
 #include "pal/thread.hpp"
 #include "pal/malloc.hpp"
 #include "pal/file.hpp"
+#include "pal/stackstring.hpp"
 
 #include "pal/palinternal.h"
 #include "pal/dbgmsg.h"
@@ -777,18 +778,29 @@ static int FILEGlobFromSplitPath( CPalThread *pthrCurrent,
                                   glob_t *pgGlob )
 {
     int  Ret;
-    char Pattern[MAX_LONGPATH];
-    char EscapedPattern[2*MAX_LONGPATH];
+    PathCharString PatternPS;
+    PathCharString EscapedPatternPS;
+    char * Pattern;
+    int length = 0;
+    char * EscapedPattern;
 
     TRACE("We shall attempt to glob from components [%s][%s][%s]\n",
           dir?dir:"NULL", fname?fname:"NULL", ext?ext:"NULL");
 
-    FILEMakePathA( Pattern, MAX_LONGPATH, dir, fname, ext );
+    if (dir) length = strlen(dir);
+    if (fname) length += strlen(fname);
+    if (ext) length += strlen(ext);
+    
+    Pattern = PatternPS.OpenStringBuffer(length);
+    FILEMakePathA( Pattern, length+1, dir, fname, ext );
+    PatternPS.CloseBuffer(length);
     TRACE("Assembled Pattern = [%s]\n", Pattern);
 
     /* special handling is needed to handle the case where
         filename contains '[' and ']' */
+    EscapedPattern = EscapedPatternPS.OpenStringBuffer(length*2);
     FILEEscapeSquareBrackets( Pattern, EscapedPattern);
+    EscapedPatternPS.CloseBuffer(strlen(EscapedPattern));
 #ifdef GLOB_QUOTE
     flags |= GLOB_QUOTE;
 #endif  // GLOB_QUOTE
index e54f243..a8fe5b3 100644 (file)
@@ -26,6 +26,7 @@ Revision History:
 #include "pal/dbgmsg.h"
 #include "pal/file.h"
 #include "pal/malloc.hpp"
+#include "pal/stackstring.hpp"
 
 #include <errno.h>
 
@@ -194,7 +195,9 @@ GetFullPathNameW(
     LPSTR fileNameA;
     /* bufferA needs to be able to hold a path that's potentially as
        large as MAX_LONGPATH WCHARs. */
-    CHAR  bufferA[MAX_LONGPATH * sizeof(WCHAR)];
+    CHAR * bufferA;
+    size_t bufferASize = 0;
+    PathCharString bufferAPS;
     LPSTR lpFilePartA;
     int   fileNameLength;
     int   srcSize;
@@ -212,6 +215,7 @@ GetFullPathNameW(
        to ANSI. This may be more than MAX_LONGPATH. We try to
        handle that case, since it may be less than MAX_LONGPATH
        WCHARs. */
+    
     fileNameLength = WideCharToMultiByte(CP_ACP, 0, lpFileName,
                                          -1, NULL, 0, NULL, NULL);
     if (fileNameLength == 0)
@@ -244,9 +248,13 @@ GetFullPathNameW(
         goto done;
     }
     
-    length = GetFullPathNameA(fileNameA, sizeof(bufferA), bufferA, &lpFilePartA);
+    bufferASize = MAX_LONGPATH * sizeof(WCHAR);
+    bufferA = bufferAPS.OpenStringBuffer(bufferASize);
+    
+    length = GetFullPathNameA(fileNameA, bufferASize, bufferA, &lpFilePartA);
+    bufferAPS.CloseBuffer(length);
     
-    if (length == 0 || length > sizeof(bufferA))
+    if (length == 0 || length > bufferASize)
     {
         /* Last error is set by GetFullPathNameA */
         nRet = length;
@@ -1087,8 +1095,11 @@ SearchPathA(
     )
 {
     DWORD nRet = 0;
-    CHAR FullPath[MAX_LONGPATH];
-    CHAR CanonicalFullPath[MAX_LONGPATH];
+    CHAR * FullPath;
+    size_t FullPathLength = 0;
+    PathCharString FullPathPS;
+    PathCharString CanonicalFullPathPS;
+    CHAR * CanonicalFullPath;
     LPCSTR pPathStart;
     LPCSTR pPathEnd;
     size_t PathLength;
@@ -1136,11 +1147,22 @@ SearchPathA(
             goto done;
         }
         /* Canonicalize the path to deal with back-to-back '/', etc. */
+        CanonicalFullPath = CanonicalFullPathPS.OpenStringBuffer(MAX_LONGPATH);
         dw = GetFullPathNameA(lpFileName, MAX_LONGPATH, CanonicalFullPath, NULL);
-        if (dw == 0 || dw >= MAX_LONGPATH) 
+        CanonicalFullPathPS.CloseBuffer(dw);
+        
+        if (dw > MAX_LONGPATH+1)
+        {
+            CanonicalFullPath = CanonicalFullPathPS.OpenStringBuffer(dw-1);
+            dw = GetFullPathNameA(lpFileName, dw,
+                                  CanonicalFullPath, NULL);
+            CanonicalFullPathPS.CloseBuffer(dw);
+        }
+
+        if (dw == 0) 
         {
             WARN("couldn't canonicalize path <%s>, error is %#x. failing.\n",
-                 FullPath, GetLastError());
+                 lpFileName, GetLastError());
             SetLastError(ERROR_INVALID_PARAMETER);
             goto done;
         }
@@ -1194,20 +1216,35 @@ SearchPathA(
     
             /* Construct a pathname by concatenating one path from lpPath, '/' 
                and lpFileName */
+            FullPathLength = PathLength + FileNameLength;
+            FullPath = FullPathPS.OpenStringBuffer(FullPathLength+1);
             memcpy(FullPath, pPathStart, PathLength);
             FullPath[PathLength] = '/';
             if (strcpy_s(&FullPath[PathLength+1], MAX_LONGPATH-PathLength, lpFileName) != SAFECRT_SUCCESS)
             {
+                FullPathPS.CloseBuffer(0);
                 ERROR("strcpy_s failed!\n");
                 SetLastError( ERROR_FILENAME_EXCED_RANGE );
                 nRet = 0;
                 goto done;
             }
 
+            FullPathPS.CloseBuffer(FullPathLength+1);            
             /* Canonicalize the path to deal with back-to-back '/', etc. */
+            CanonicalFullPath = CanonicalFullPathPS.OpenStringBuffer(MAX_LONGPATH);
             dw = GetFullPathNameA(FullPath, MAX_LONGPATH,
                                   CanonicalFullPath, NULL);
-            if (dw == 0 || dw >= MAX_LONGPATH) 
+            CanonicalFullPathPS.CloseBuffer(dw);
+            
+            if (dw > MAX_LONGPATH+1)
+            {
+                CanonicalFullPath = CanonicalFullPathPS.OpenStringBuffer(dw-1);
+                dw = GetFullPathNameA(FullPath, dw,
+                                      CanonicalFullPath, NULL);
+                CanonicalFullPathPS.CloseBuffer(dw);
+            }
+            
+            if (dw == 0) 
             {
                 /* Call failed - possibly low memory.  Skip the path */
                 WARN("couldn't canonicalize path <%s>, error is %#x. "
@@ -1305,14 +1342,20 @@ SearchPathW(
     )
 {
     DWORD nRet = 0;
-    WCHAR FullPath[MAX_LONGPATH];
+    WCHAR * FullPath;
+    size_t FullPathLength = 0;
+    PathWCharString FullPathPS;
     LPCWSTR pPathStart;
     LPCWSTR pPathEnd;
     size_t PathLength;
     size_t FileNameLength;
     DWORD dw;
-    char AnsiPath[MAX_LONGPATH];
-    WCHAR CanonicalPath[MAX_LONGPATH];
+    char * AnsiPath;
+    PathCharString AnsiPathPS;
+    size_t CanonicalPathLength;
+    int canonical_size;
+    WCHAR * CanonicalPath;
+    PathWCharString CanonicalPathPS;
 
     PERF_ENTRY(SearchPathW);
     ENTRY("SearchPathW(lpPath=%p (%S), lpFileName=%p (%S), lpExtension=%p, "
@@ -1347,8 +1390,17 @@ SearchPathW(
     if('\\' == lpFileName[0] || '/' == lpFileName[0])
     {
         /* Canonicalize the path to deal with back-to-back '/', etc. */
+        CanonicalPath = CanonicalPathPS.OpenStringBuffer(MAX_LONGPATH);
         dw = GetFullPathNameW(lpFileName, MAX_LONGPATH, CanonicalPath, NULL);
-        if (dw == 0 || dw >= MAX_LONGPATH) 
+        CanonicalPathPS.CloseBuffer(dw);
+        if (dw > MAX_LONGPATH+1)
+        {
+            CanonicalPath = CanonicalPathPS.OpenStringBuffer(dw-1);
+            dw = GetFullPathNameW(lpFileName, dw, CanonicalPath, NULL);
+            CanonicalPathPS.CloseBuffer(dw);
+        }
+        
+        if (dw == 0) 
         {
             WARN("couldn't canonicalize path <%S>, error is %#x. failing.\n",
                  lpPath, GetLastError());
@@ -1357,8 +1409,12 @@ SearchPathW(
         }
 
         /* see if the file exists */
-       WideCharToMultiByte(CP_ACP, 0, CanonicalPath, -1,
-                           AnsiPath, MAX_LONGPATH, NULL, NULL);
+        CanonicalPathLength = (PAL_wcslen(CanonicalPath)+1)*3;
+        AnsiPath = AnsiPathPS.OpenStringBuffer(CanonicalPathLength);
+           canonical_size = WideCharToMultiByte(CP_ACP, 0, CanonicalPath, -1,
+                           AnsiPath, CanonicalPathLength, NULL, NULL);
+           AnsiPathPS.CloseBuffer(canonical_size);
+           
         if(0 == access(AnsiPath, F_OK))
         {
             /* found it */
@@ -1409,14 +1465,28 @@ SearchPathW(
     
             /* Construct a pathname by concatenating one path from lpPath, '/' 
                and lpFileName */
+            FullPathLength = PathLength + FileNameLength;
+            FullPath = FullPathPS.OpenStringBuffer(FullPathLength+1);
             memcpy(FullPath, pPathStart, PathLength*sizeof(WCHAR));
             FullPath[PathLength] = '/';
             PAL_wcscpy(&FullPath[PathLength+1], lpFileName);
+            
+            FullPathPS.CloseBuffer(FullPathLength+1);
     
             /* Canonicalize the path to deal with back-to-back '/', etc. */
+            CanonicalPath = CanonicalPathPS.OpenStringBuffer(MAX_LONGPATH);
             dw = GetFullPathNameW(FullPath, MAX_LONGPATH,
                                   CanonicalPath, NULL);
-            if (dw == 0 || dw >= MAX_LONGPATH) 
+            CanonicalPathPS.CloseBuffer(dw);
+            
+            if (dw > MAX_LONGPATH+1)
+            {
+                CanonicalPath = CanonicalPathPS.OpenStringBuffer(dw-1);
+                dw = GetFullPathNameW(FullPath, dw, CanonicalPath, NULL);
+                CanonicalPathPS.CloseBuffer(dw);
+            }
+            
+            if (dw == 0) 
             {
                 /* Call failed - possibly low memory.  Skip the path */
                 WARN("couldn't canonicalize path <%S>, error is %#x. "
@@ -1425,8 +1495,12 @@ SearchPathW(
             }
     
             /* see if the file exists */
-            WideCharToMultiByte(CP_ACP, 0, CanonicalPath, -1,
-                                AnsiPath, MAX_LONGPATH, NULL, NULL);
+            CanonicalPathLength = (PAL_wcslen(CanonicalPath)+1) * 3;
+            AnsiPath = AnsiPathPS.OpenStringBuffer(CanonicalPathLength);
+            canonical_size = WideCharToMultiByte(CP_ACP, 0, CanonicalPath, -1,
+                                AnsiPath, CanonicalPathLength, NULL, NULL);
+            AnsiPathPS.CloseBuffer(canonical_size);
+               
             if(0 == access(AnsiPath, F_OK))
             {
                 /* found it */
index 2281203..b3bfe39 100644 (file)
@@ -23,6 +23,7 @@ Abstract:
 #define _PAL_MAP_H_
 
 #include "corunix.hpp"
+#include <sys/param.h>
 
 extern "C"
 {
@@ -144,7 +145,7 @@ namespace CorUnix
     class CFileMappingImmutableData
     {
     public:
-        CHAR szFileName[MAX_LONGPATH + 1];
+        CHAR szFileName[MAXPATHLEN];
         UINT MaxSize;               // The max size of the file mapping object
         DWORD flProtect;            // Protection desired for the file view
         BOOL bPALCreatedTempFile;   // TRUE if it's a PAL created file
index 392c831..3e9a057 100644 (file)
@@ -12,7 +12,8 @@ class StackString
 private:
     T m_innerBuffer[STACKCOUNT + 1];
     T * m_buffer;
-    SIZE_T m_count; // actual allocated count
+    SIZE_T m_size; // actual allocated size
+    SIZE_T m_count; // actual length of string
 
     void NullTerminate()
     {
@@ -45,6 +46,7 @@ private:
         DeleteBuffer();
         m_buffer = newBuffer;
         m_count = count;
+        m_size = count+1;
 
         return;
     }
@@ -59,6 +61,7 @@ private:
             }
             else
             {
+                m_size = STACKCOUNT+1;
                 m_buffer = m_innerBuffer;
                 m_count = count;
             }
@@ -66,9 +69,14 @@ private:
         else if (m_innerBuffer == m_buffer)
         {
             if (count > STACKCOUNT)
+            {
                 ReallocateBuffer(count);
+            }
             else
+            {
                 m_count = count;
+                m_size = STACKCOUNT+1;
+            }
         }
         else
         {
@@ -112,7 +120,7 @@ public:
     
     SIZE_T GetSizeOf() const
     {
-        return (m_count+1) * sizeof(T);
+        return m_size * sizeof(T);
     }
 
     CONST T * GetString() const
index 29d8d19..802b7e4 100644 (file)
@@ -34,6 +34,7 @@ Abstract:
 #include "pal/misc.h"
 #include "pal/virtual.h"
 #include "pal/map.hpp"
+#include "pal/stackstring.hpp"
 
 #include <sys/param.h>
 #include <errno.h>
@@ -92,14 +93,16 @@ CRITICAL_SECTION module_critsec;
 MODSTRUCT exe_module; 
 MODSTRUCT *pal_module = NULL;
 
-char g_szCoreCLRPath[MAX_LONGPATH] = { 0 };
+char * g_szCoreCLRPath = NULL;
+size_t g_cbszCoreCLRPath = (MAX_LONGPATH+1) * sizeof(char);
 
+int MaxWCharToAcpLength = 3;
 /* static function declarations ***********************************************/
 
 template<class TChar> bool LOADVerifyLibraryPath(const TChar *libraryPath);
 bool LOADConvertLibraryPathWideStringToMultibyteString(
     LPCWSTR wideLibraryPath,
-    CHAR (&multibyteLibraryPath)[MAX_LONGPATH],
+    LPSTR multibyteLibraryPath,
     INT *multibyteLibraryPathLengthRef);
 
 static BOOL LOADValidateModule(MODSTRUCT *module);
@@ -220,8 +223,9 @@ LoadLibraryExW(
         return NULL;
     }
     
-    CHAR lpstr[MAX_LONGPATH];
+    CHAR * lpstr;
     INT name_length;
+    PathCharString pathstr;
     HMODULE hModule = NULL;
 
     PERF_ENTRY(LoadLibraryExW);
@@ -234,6 +238,7 @@ LoadLibraryExW(
         goto done;
     }
 
+    lpstr = pathstr.OpenStringBuffer((PAL_wcslen(lpLibFileName)+1) * MaxWCharToAcpLength);
     if (!LOADConvertLibraryPathWideStringToMultibyteString(lpLibFileName, lpstr, &name_length))
     {
         goto done;
@@ -241,6 +246,7 @@ LoadLibraryExW(
 
     /* do the Dos/Unix conversion on our own copy of the name */
     FILEDosToUnixPathA(lpstr);
+    pathstr.CloseBuffer(name_length);
 
     /* let LOADLoadLibrary call SetLastError in case of failure */
     hModule = LOADLoadLibrary(lpstr, TRUE);
@@ -264,7 +270,8 @@ PALAPI
 PAL_LoadLibraryDirect(
     IN LPCWSTR lpLibFileName)
 {
-    CHAR lpstr[MAX_LONGPATH];
+    PathCharString pathstr;
+    CHAR * lpstr = NULL;
     INT name_length;
     HMODULE hModule = NULL;
 
@@ -277,6 +284,8 @@ PAL_LoadLibraryDirect(
     {
         goto done;
     }
+    
+    lpstr = pathstr.OpenStringBuffer((PAL_wcslen(lpLibFileName)+1) * MaxWCharToAcpLength);
 
     if (!LOADConvertLibraryPathWideStringToMultibyteString(lpLibFileName, lpstr, &name_length))
     {
@@ -285,6 +294,7 @@ PAL_LoadLibraryDirect(
 
     /* do the Dos/Unix conversion on our own copy of the name */
     FILEDosToUnixPathA(lpstr);
+    pathstr.CloseBuffer(name_length);
 
     /* let LOADLoadLibraryDirect call SetLastError in case of failure */
     hModule = LOADLoadLibraryDirect(lpstr, true /* setLastError */);
@@ -309,7 +319,8 @@ PAL_RegisterLibraryDirect(
     IN HMODULE dl_handle,
     IN LPCWSTR lpLibFileName)
 {
-    CHAR lpstr[MAX_LONGPATH];
+    PathCharString pathstr;
+    CHAR * lpstr = NULL;
     INT name_length;
     HMODULE hModule = NULL;
 
@@ -323,6 +334,7 @@ PAL_RegisterLibraryDirect(
         goto done;
     }
 
+    lpstr = pathstr.OpenStringBuffer((PAL_wcslen(lpLibFileName)+1) * MaxWCharToAcpLength);
     if (!LOADConvertLibraryPathWideStringToMultibyteString(lpLibFileName, lpstr, &name_length))
     {
         goto done;
@@ -330,6 +342,7 @@ PAL_RegisterLibraryDirect(
 
     /* do the Dos/Unix conversion on our own copy of the name */
     FILEDosToUnixPathA(lpstr);
+    pathstr.CloseBuffer(name_length);
 
     /* let LOADRegisterLibraryDirect call SetLastError in case of failure */
     LockModuleList();
@@ -1237,14 +1250,16 @@ bool LOADVerifyLibraryPath(const TChar *libraryPath)
 // Converts the wide char library path string into a multibyte-char string. On error, calls SetLastError() and returns false.
 bool LOADConvertLibraryPathWideStringToMultibyteString(
     LPCWSTR wideLibraryPath,
-    CHAR(&multibyteLibraryPath)[MAX_LONGPATH],
+    LPSTR multibyteLibraryPath,
     INT *multibyteLibraryPathLengthRef)
 {
-    _ASSERTE(wideLibraryPath != nullptr);
     _ASSERTE(multibyteLibraryPathLengthRef != nullptr);
+    _ASSERTE(wideLibraryPath != nullptr);
 
+    size_t length = (PAL_wcslen(wideLibraryPath)+1) * MaxWCharToAcpLength;
     *multibyteLibraryPathLengthRef = WideCharToMultiByte(CP_ACP, 0, wideLibraryPath, -1, multibyteLibraryPath,
-                                                         MAX_LONGPATH, nullptr, nullptr);
+                                                        length, nullptr, nullptr);
+    
     if (*multibyteLibraryPathLengthRef == 0)
     {
         DWORD dwLastError = GetLastError();
@@ -1596,7 +1611,8 @@ Return value :
 --*/
 static HMODULE LOADLoadLibrary(LPCSTR shortAsciiName, BOOL fDynamic)
 {
-    CHAR fullLibraryName[MAX_LONGPATH];
+    CHAR * fullLibraryName;
+    PathCharString fullLibraryNamePS;
     HMODULE module = NULL;
     HMODULE dl_handle = NULL;
 
@@ -1642,8 +1658,11 @@ static HMODULE LOADLoadLibrary(LPCSTR shortAsciiName, BOOL fDynamic)
                 continue;
 
             _ASSERTE(dl_handle == nullptr);
-            if (snprintf(fullLibraryName, MAX_LONGPATH, formatStrings[i], PAL_SHLIB_PREFIX, shortAsciiName, PAL_SHLIB_SUFFIX) < MAX_LONGPATH)
+            fullLibraryName = fullLibraryNamePS.OpenStringBuffer(strlen(PAL_SHLIB_PREFIX)+strlen(shortAsciiName)+strlen(PAL_SHLIB_SUFFIX));
+            int size = snprintf(fullLibraryName, fullLibraryNamePS.GetSizeOf(), formatStrings[i], PAL_SHLIB_PREFIX, shortAsciiName, PAL_SHLIB_SUFFIX);
+            if (size < fullLibraryNamePS.GetSizeOf())
             {
+                fullLibraryNamePS.CloseBuffer(size);
                 dl_handle = LOADLoadLibraryDirect(fullLibraryName, false /* setLastError */);
                 if (dl_handle != nullptr)
                 {
@@ -1910,7 +1929,12 @@ MODSTRUCT *LOADGetPalLibrary()
         }
         // Stash a copy of the CoreCLR installation path in a global variable.
         // Make sure it's terminated with a slash.
-        if (strcpy_s(g_szCoreCLRPath, sizeof(g_szCoreCLRPath), info.dli_fname) != SAFECRT_SUCCESS)
+        if (g_szCoreCLRPath == NULL)
+        {
+            g_szCoreCLRPath = new char[g_cbszCoreCLRPath/sizeof(char)];
+        }
+        
+        if (strcpy_s(g_szCoreCLRPath, g_cbszCoreCLRPath, info.dli_fname) != SAFECRT_SUCCESS)
         {
             ERROR("LOADGetPalLibrary: strcpy_s failed!");
             goto exit;
index 04d4ec9..898a457 100644 (file)
@@ -30,6 +30,7 @@ Revision History:
 #include "pal/utf8.h"
 #include "pal/locale.h"
 #include "pal/cruntime.h"
+#include "pal/stackstring.hpp"
 
 #if !(HAVE_PTHREAD_RWLOCK_T || HAVE_COREFOUNDATION)
 #error Either pthread rwlocks or Core Foundation are required for Unicode support
@@ -953,7 +954,7 @@ EXIT:
     return retval;
 }
 
-extern char g_szCoreCLRPath[MAX_LONGPATH];
+extern char * g_szCoreCLRPath;
 
 /*++
 Function :
@@ -967,10 +968,13 @@ PALAPI
 PAL_BindResources(IN LPCSTR lpDomain)
 {
 #ifndef __APPLE__
-    char coreCLRDirectoryPath[MAX_LONGPATH];
-
-    INDEBUG(DWORD size = )
-    FILEGetDirectoryFromFullPathA(g_szCoreCLRPath, MAX_LONGPATH, coreCLRDirectoryPath);
+    _ASSERTE(g_szCoreCLRPath != NULL);
+    char * coreCLRDirectoryPath;
+    PathCharString coreCLRDirectoryPathPS;
+    int len = strlen(g_szCoreCLRPath);
+    coreCLRDirectoryPath = coreCLRDirectoryPathPS.OpenStringBuffer(len);
+    DWORD size = FILEGetDirectoryFromFullPathA(g_szCoreCLRPath, len, coreCLRDirectoryPath);
+    coreCLRDirectoryPathPS.CloseBuffer(size);
     _ASSERTE(size <= MAX_LONGPATH);
 
     LPCSTR boundPath = bindtextdomain(lpDomain, coreCLRDirectoryPath);
index d4223a6..f2dd1fb 100644 (file)
@@ -132,7 +132,7 @@ static double PERFComputeStandardDeviation(pal_perf_api_info *api);
 static void PERFPrintProgramHeaderInfo(PERF_FILE * hFile, BOOL completedExecution);
 static BOOL PERFInitProgramInfo(LPWSTR command_line, LPWSTR exe_path);
 static BOOL PERFReadSetting( );
-static void PERFLogFileName(char *destFileName, const char *fileName, const char *suffix, int max_length);
+static void PERFLogFileName(PathCharString destFileString, const char *fileName, const char *suffix, int max_length);
 static void PERFlushAllLogs();
 static int PERFWriteCounters(pal_perf_api_info * table); 
 static BOOL PERFFlushLog(pal_perf_thread_info * local_buffer, BOOL output_header);
@@ -670,9 +670,10 @@ PERFlushAllLogs( )
 
 static
 void
-PERFLogFileName(char *destFileName, const char *fileName, const char *suffix, int max_length)
+PERFLogFileName(PathCharString * destFileString, const char *fileName, const char *suffix, int max_length)
 {
     const char *dir_path;
+    char * destFileName = new char[max_length];
     dir_path = (profile_log_path == NULL) ? "." : profile_log_path;
 
     if (fileName != NULL)
@@ -684,20 +685,24 @@ PERFLogFileName(char *destFileName, const char *fileName, const char *suffix, in
         snprintf(destFileName, max_length, "%s%s%d_%d%s", dir_path, PATH_SEPARATOR,
             program_info.process_id, THREADSilentGetCurrentThreadId(), suffix);
     }
-
+    
+    destFileString.Set(destFileName);
+    delete [] destFileName;
+    destFileName = NULL;
 }
 
 static
 int
 PERFWriteCounters( pal_perf_api_info * table )
 {
-    char fileName[MAX_LONGPATH];
+    PathCharString fileName;
     pal_perf_api_info * off;
     PERF_FILE * hFile;
     int i;
 
     off = table;
-    PERFLogFileName(fileName, profile_summary_log_name, "_perf_summary.log", MAX_LONGPATH);
+    
+    PERFLogFileName(&fileName, profile_summary_log_name, "_perf_summary.log", MAX_LONGPATH);
     hFile = PERF_FILEFN(fopen)(fileName, "a+");
     if(hFile != NULL)
     {   
@@ -737,7 +742,7 @@ PERFWriteCounters( pal_perf_api_info * table )
     if (pal_perf_histogram_size > 0)
     {
         off = table;
-        PERFLogFileName(fileName, profile_summary_log_name, "_perf_summary.hist", MAX_LONGPATH);
+        PERFLogFileName(&fileName, profile_summary_log_name, "_perf_summary.hist", MAX_LONGPATH);
         hFile = PERF_FILEFN(fopen)(fileName, "a+");
 
         if (hFile != NULL)
@@ -789,7 +794,8 @@ PERFReadSetting(  )
     char * ptr;
     char function_name[PAL_PERF_MAX_FUNCTION_NAME];  //no function can be longer than 127 bytes.
 
-    char  file_name_buf[MAX_LONGPATH];  
+    char * file_name_buf;
+    PathCharString file_name_bufPS;
     char  * input_file_name; 
     char  * summary_flag_env;
     char  * nested_tracing_env;
@@ -906,15 +912,19 @@ PERFReadSetting(  )
         {
             if( PERFIsValidFile(perf_default_path,traced_apis_filename))
             {
-                if ((strcpy_s(file_name_buf, sizeof(file_name_buf), perf_default_path) != SAFECRT_SUCCESS) ||
-                    (strcat_s(file_name_buf, sizeof(file_name_buf), PATH_SEPARATOR) != SAFECRT_SUCCESS) ||
-                    (strcat_s(file_name_buf, sizeof(file_name_buf), traced_apis_filename) != SAFECRT_SUCCESS))
+                int length = strlen(perf_default_path) + strlen(PATH_SEPARATOR) + strlen(traced_apis_filename);
+                file_name_buf = file_name_bufPS.OpenStringBuffer(length);
+                if ((strcpy_s(file_name_buf, file_name_bufPS.GetSizeOf(), perf_default_path) != SAFECRT_SUCCESS) ||
+                    (strcat_s(file_name_buf, file_name_bufPS.GetSizeOf(), PATH_SEPARATOR) != SAFECRT_SUCCESS) ||
+                    (strcat_s(file_name_buf, file_name_bufPS.GetSizeOf(), traced_apis_filename) != SAFECRT_SUCCESS))
                 {
+                    file_name_bufPS.CloseBuffer(0);
                     ret = FALSE;
                     input_file_name = NULL;
                 }
                 else
                 {
+                    file_name_bufPS.CloseBuffer(length);
                     input_file_name = file_name_buf;
                 }
             }
@@ -1078,14 +1088,14 @@ BOOL
 PERFFlushLog(pal_perf_thread_info * local_info, BOOL output_header)
 {
     BOOL ret = FALSE;
-    char fileName[MAX_LONGPATH];
+    PathCharString fileName;
     int nWrittenBytes = 0;
     PERF_FILE * hFile;
 
     if (summary_only)
         return TRUE;
 
-    PERFLogFileName(fileName, profile_time_log_name, "_perf_time.log", MAX_LONGPATH);
+    PERFLogFileName(&fileName, profile_time_log_name, "_perf_time.log", MAX_LONGPATH);
 
     hFile = PERF_FILEFN(fopen)(fileName, "a+");
 
@@ -1108,6 +1118,10 @@ PERFFlushLog(pal_perf_thread_info * local_info, BOOL output_header)
         PERF_FILEFN(fclose)(hFile);
         ret = TRUE;
     }
+    
+    delete [] fileName;
+    fileName = NULL;
+
     return ret;
 }
 
@@ -1432,20 +1446,25 @@ char *
 PERFIsValidFile( const char * path, const char * file)
 {
     FILE * hFile;
-    char temp[MAX_LONGPATH];
+    char * temp;
+    PathCharString tempPS;
 
     if(file==NULL || strlen(file)==0) 
         return NULL;
 
        if ( strcmp(path, "") )
-    {   
+    {
+        int length = strlen(path) + strlen(PATH_SEPARATOR) + strlen(file);
+        temp = tempPS.OpenStringBuffer(length);   
         if ((strcpy_s(temp, sizeof(temp), path) != SAFECRT_SUCCESS) ||
             (strcat_s(temp, sizeof(temp), PATH_SEPARATOR) != SAFECRT_SUCCESS) ||
             (strcat_s(temp, sizeof(temp), file) != SAFECRT_SUCCESS))
         {
+            tempPS.CloseBuffer(0);
             return NULL;
         }
 
+        tempPS.CloseBuffer(length);
         hFile = fopen(temp, "r");
     }
     else
index 2546635..036a62e 100644 (file)
@@ -369,8 +369,8 @@ static Volatile<HANDLE> locking_thread;
 
 #ifndef CORECLR
 /* suffix template for mkstemp */
-static char segment_name_template[MAX_LONGPATH];
-static char lockfile_name[MAX_LONGPATH];
+static char segment_name_template[MAX_PATH];
+static char lockfile_name[MAX_PATH];
 #endif // !defined(CORECLR)
 
 /* Constants ******************************************************************/
@@ -445,8 +445,8 @@ BOOL SHMInitialize(void)
     int fd_map;
     int i;
     int j;
-    CHAR config_dir[MAX_LONGPATH];
-    CHAR first_segment_name[MAX_LONGPATH];
+    CHAR config_dir[MAX_PATH];
+    CHAR first_segment_name[MAX_PATH];
     ssize_t sBytes;
 #endif // !CORECLR
 
@@ -455,14 +455,14 @@ BOOL SHMInitialize(void)
     init_waste();
     
 #ifndef CORECLR
-    if ( PALGetPalConfigDir( config_dir, MAX_LONGPATH ) )
+    if ( PALGetPalConfigDir( config_dir, MAX_PATH ) )
     {
         if ( ( strlen( config_dir ) + strlen( segment_name_prefix ) + 
                                            /* + 3 for the / _ and \0 */
-               strlen( first_segment_suffix ) + 3 ) < MAX_LONGPATH && 
+               strlen( first_segment_suffix ) + 3 ) < MAX_PATH && 
              
              ( strlen( config_dir ) + strlen( segment_name_prefix ) + 
-               SEGMENT_NAME_SUFFIX_LENGTH + 3 ) < MAX_LONGPATH ) 
+               SEGMENT_NAME_SUFFIX_LENGTH + 3 ) < MAX_PATH ) 
         {
             /* build first segment's file name */
             sprintf( first_segment_name,"%s/%s_%s", config_dir,
@@ -481,7 +481,7 @@ BOOL SHMInitialize(void)
         else
         {
             ASSERT( "Configuration directory length + segment name length "
-                   "is greater then MAX_LONGPATH.\n" );
+                   "is greater then MAX_PATH.\n" );
             return FALSE;
         }
     }
@@ -973,15 +973,15 @@ void SHMCleanup(void)
     }
     else /* Clean up everything. */
     {
-        CHAR PalDir[ MAX_LONGPATH + 1 ];
-        CHAR FileName[ MAX_LONGPATH + 1 ];
+        CHAR PalDir[ MAX_PATH + 1 ];
+        CHAR FileName[ MAX_PATH + 1 ];
         UINT nEndOfPathAndPrefix = 0;
         SHM_SEGMENT_HEADER segment_header;
         LPCSTR suffix;
         int fd_segment;
 
         /* Build start of filename. */
-        if ( !PALGetPalConfigDir( PalDir, MAX_LONGPATH ) )
+        if ( !PALGetPalConfigDir( PalDir, MAX_PATH ) )
         {
             ASSERT( "Unable to determine the PAL config directory.\n" );
 #ifdef O_EXLOCK
@@ -1839,13 +1839,13 @@ Return value :
 --*/
 static LPVOID SHMMapSegment(char *segment_name)
 {
-    char segment_path[MAX_LONGPATH];
+    char segment_path[MAX_PATH];
     int segment_fd;
     LPVOID *segment;
     struct stat sb;
 
     /* Construct the file's full path */
-    if ( !PALGetPalConfigDir( segment_path, MAX_LONGPATH ) )
+    if ( !PALGetPalConfigDir( segment_path, MAX_PATH ) )
     {
         ASSERT( "Unable to determine the PAL config directory.\n" );
         return NULL;
@@ -1965,7 +1965,7 @@ Notes :
 static BOOL SHMAddSegment(void)
 {
 #ifndef CORECLR
-    char segment_name[MAX_LONGPATH];
+    char segment_name[MAX_PATH];
     char *suffix_start;
     int fd_map;
 #endif // !CORECLR
@@ -2634,7 +2634,7 @@ static void save_waste(void)
 {
     int fd;
     FILE *log_file;
-    char file_name[MAX_LONGPATH];
+    char file_name[MAX_PATH];
     enum SHM_POOL_SIZES sps;
     int avg;
     LPSTR env_string;
@@ -2646,7 +2646,7 @@ static void save_waste(void)
         return;
     }
     
-    if ( !PALGetPalConfigDir( file_name, MAX_LONGPATH ) )
+    if ( !PALGetPalConfigDir( file_name, MAX_PATH ) )
     {
         ERROR( "Unable to determine the PAL config directory.\n" );
         return;
index 4ccfec6..4a90220 100644 (file)
@@ -2949,7 +2949,7 @@ namespace CorUnix
         PAL_ERROR palErr = NO_ERROR;
         int iProcessPipe, iBytesToWrite, iRetryCount;
         ssize_t sszRet;
-        char strPipeFilename[MAX_LONGPATH];
+        char strPipeFilename[MAX_PATH];
         BYTE * pPos = pMsg;
         bool fRet;
         CPalThread *pthrCurrent = InternalGetCurrentThread();
@@ -2958,7 +2958,7 @@ namespace CorUnix
                     "SendMsgToRemoteWorker called with local process as "
                     "target process\n");
 
-        fRet = GetProcessPipeName(strPipeFilename, MAX_LONGPATH, dwProcessId);
+        fRet = GetProcessPipeName(strPipeFilename, MAX_PATH, dwProcessId);
 
         _ASSERT_MSG(fRet, "Failed to retrieve process pipe's name!\n");
 
@@ -3823,10 +3823,10 @@ namespace CorUnix
 
 #ifndef CORECLR
         int iPipeRd = -1, iPipeWr = -1;
-        char szPipeFilename[MAX_LONGPATH];
+        char szPipeFilename[MAX_PATH];
 
         /* Create the blocking pipe */        
-        if(!GetProcessPipeName(szPipeFilename, MAX_LONGPATH, gPID))
+        if(!GetProcessPipeName(szPipeFilename, MAX_PATH, gPID))
         {
             ERROR("couldn't get process pipe's name\n");
             szPipeFilename[0] = 0;
@@ -3966,9 +3966,9 @@ namespace CorUnix
     {
         PAL_ERROR palErr = NO_ERROR;        
 #ifndef CORECLR
-        char szPipeFilename[MAX_LONGPATH];
+        char szPipeFilename[MAX_PATH];
 
-        if(GetProcessPipeName(szPipeFilename, MAX_LONGPATH, gPID))
+        if(GetProcessPipeName(szPipeFilename, MAX_PATH, gPID))
         {
             if (InternalUnlink(pthrCurrent, szPipeFilename) == -1)
             {
@@ -4021,13 +4021,13 @@ namespace CorUnix
         int iDestSize,
         DWORD dwPid)
     {
-        CHAR config_dir[MAX_LONGPATH];
+        CHAR config_dir[MAX_PATH];
         int needed_size;
 
         _ASSERT_MSG(NULL != pDest, "Destination pointer is NULL!\n");
         _ASSERT_MSG(0 < iDestSize,"Invalid buffer size %d\n", iDestSize);
 
-        if (!PALGetPalConfigDir(config_dir, MAX_LONGPATH))
+        if (!PALGetPalConfigDir(config_dir, MAX_PATH))
         {
             ASSERT("Unable to determine the PAL config directory.\n");
             pDest[0] = '\0';
index fcca7e3..69c65f6 100644 (file)
@@ -33,6 +33,7 @@ Abstract:
 #include "pal/utils.h"
 #include "pal/misc.h"
 #include "pal/virtual.h"
+#include "pal/stackstring.hpp"
 
 #include <errno.h>
 #if HAVE_POLL
@@ -560,7 +561,7 @@ CorUnix::InternalCreateProcess(
     int iFdErr = -1;
     
     pid_t processId;
-    char lpFileName[MAX_LONGPATH] ;
+    char * lpFileName = new char[MAX_LONGPATH];
     char **lppArgv = NULL;
     UINT nArg;
     int  iRet;
@@ -581,6 +582,12 @@ CorUnix::InternalCreateProcess(
         palError = ERROR_INVALID_PARAMETER;
         goto InternalCreateProcessExit;
     }
+    
+    if (NULL == lpFileName)
+    {
+        palError = ERROR_NOT_ENOUGH_MEMORY;
+        goto InternalCreateProcessExit;
+    } 
 
     if (0 != (dwCreationFlags & ~(CREATE_SUSPENDED|CREATE_NEW_CONSOLE)))
     {
@@ -698,8 +705,8 @@ CorUnix::InternalCreateProcess(
             if ( PAL_GetPALDirectoryA( lpFileName,
                                       (MAX_LONGPATH - (strlen(PROCESS_PELOADER_FILENAME)+1))))
             {
-                if ((strcat_s(lpFileName, sizeof(lpFileName), "/") != SAFECRT_SUCCESS) ||
-                    (strcat_s(lpFileName, sizeof(lpFileName), PROCESS_PELOADER_FILENAME) != SAFECRT_SUCCESS))
+                if ((strcat_s(lpFileName, sizeof(char) * MAX_LONGPATH, "/") != SAFECRT_SUCCESS) ||
+                    (strcat_s(lpFileName, sizeof(char) * MAX_LONGPATH, PROCESS_PELOADER_FILENAME) != SAFECRT_SUCCESS))
                 {
                     ERROR("strcpy_s/strcat_s failed!\n");
                     palError = ERROR_INTERNAL_ERROR;
@@ -3203,7 +3210,8 @@ getFileName(
 {
     LPWSTR lpEnd;
     WCHAR wcEnd;
-    char lpFileName[MAX_LONGPATH];
+    char * lpFileName;
+    PathCharString lpFileNamePS;
     char *lpTemp;
 
     if (lpApplicationName)
@@ -3286,13 +3294,17 @@ getFileName(
         *lpEnd = 0x0000;
 
         /* Convert to ASCII */
-        if (!WideCharToMultiByte(CP_ACP, 0, lpCommandLine, -1,
-                                 lpFileName, MAX_LONGPATH, NULL, NULL))
+        int size = 0;
+        int length = (PAL_wcslen(lpCommandLine)+1) * sizeof(WCHAR);
+        lpFileName = lpFileNamePS.OpenStringBuffer(length);
+        if (!(size = WideCharToMultiByte(CP_ACP, 0, lpCommandLine, -1,
+                                 lpFileName, length, NULL, NULL)))
         {
             ASSERT("WideCharToMultiByte failure\n");
             return FALSE;
         }
 
+        lpFileNamePS.CloseBuffer(size);
         /* restore last character */
         *lpEnd = wcEnd;
 
@@ -4069,4 +4081,4 @@ CorUnix::CProcProcessLocalData::~CProcProcessLocalData()
         DestroyProcessModules(pProcessModules);
     }
 }
-        
\ No newline at end of file
+        
index 46cb57f..7744f31 100644 (file)
@@ -139,7 +139,7 @@ int __cdecl main(int argc, char *argv[]) {
         Fail ("SearchPathA: ERROR2 -> Did not Find valid file[%s][%s][%d]\n", lpPath, szFileNameExistsWithExt, error);
     }
 
-    RemoveAll();
+    /*RemoveAll();*/
     PAL_Terminate();
     return PASS; 
 }