Apply StackString for pal MAX_LONGPATH references.
authorLakshmi Priya Sekar <lasekar@microsoft.com>
Mon, 5 Oct 2015 18:51:50 +0000 (11:51 -0700)
committerLakshmi Priya Sekar <lasekar@microsoft.com>
Tue, 6 Oct 2015 22:28:40 +0000 (15:28 -0700)
src/pal/src/CMakeLists.txt
src/pal/src/debug/debug.cpp
src/pal/src/include/pal/stackstring.hpp [moved from src/pal/src/misc/stackstring.cpp with 74% similarity]

index 20393cd..cbbed24 100644 (file)
@@ -141,7 +141,6 @@ set(SOURCES
   misc/interlock.cpp
   misc/miscpalapi.cpp
   misc/msgbox.cpp
-  misc/stackstring.cpp
   misc/strutil.cpp
   misc/sysinfo.cpp
   misc/time.cpp
index 6c36011..e36a46b 100644 (file)
@@ -38,6 +38,7 @@ Revision History:
 #include "pal/misc.h"
 #include "pal/malloc.hpp"
 #include "pal/module.h"
+#include "pal/stackstring.hpp"
 #include "pal/virtual.h"
 
 #include <signal.h>
@@ -343,15 +344,18 @@ DebugBreakCommand()
     const char *command_string = getenv (PAL_RUN_ON_DEBUG_BREAK);
     if (command_string) {
         char pid_buf[sizeof (PID_TEXT) + 32];
-        char exe_buf[sizeof (EXE_TEXT) + MAX_LONGPATH + 1];
+        PathCharString exe_bufString;
+        SIZE_T dwexe_buf = strlen(EXE_TEXT) + PAL_wcslen(exe_module.lib_name) + 1;
+        CHAR * exe_buf = exe_bufString.OpenStringBuffer(dwexe_buf);
 
         if (snprintf (pid_buf, sizeof (pid_buf), PID_TEXT "%d", getpid()) <= 0) {
             goto FAILED;
         }
-        if (snprintf (exe_buf, sizeof (exe_buf), EXE_TEXT "%ls", (wchar_t *)exe_module.lib_name) <= 0) {
+        if (snprintf (exe_buf, sizeof (CHAR) * (dwexe_buf + 1), EXE_TEXT "%ls", (wchar_t *)exe_module.lib_name) <= 0) {
             goto FAILED;
         }
 
+        exe_bufString.CloseBuffer(dwexe_buf);
         /* strictly speaking, we might want to only set these environment
            variables in the child process, but if we do that we can't check
            for errors. putenv/setenv can fail when out of memory */
@@ -1598,7 +1602,7 @@ PAL_CreateExecWatchpoint(
     CPalThread *pTargetThread = NULL;
     IPalObject *pobjThread = NULL;
     int fd = -1;
-    char ctlPath[MAX_LONGPATH];
+    char ctlPath[50];
 
     struct
     {
@@ -1642,6 +1646,7 @@ PAL_CreateExecWatchpoint(
     }
 
     snprintf(ctlPath, sizeof(ctlPath), "/proc/%u/lwp/%u/lwpctl", getpid(), pTargetThread->GetLwpId());
+
     fd = InternalOpen(pThread, ctlPath, O_WRONLY);
     if (-1 == fd)
     {
@@ -1719,7 +1724,7 @@ PAL_DeleteExecWatchpoint(
     CPalThread *pTargetThread = NULL;
     IPalObject *pobjThread = NULL;
     int fd = -1;
-    char ctlPath[MAX_LONGPATH];
+    char ctlPath[50];
 
     struct
     {
@@ -1744,6 +1749,7 @@ PAL_DeleteExecWatchpoint(
     }
 
     snprintf(ctlPath, sizeof(ctlPath), "/proc/%u/lwp/%u/lwpctl", getpid(), pTargetThread->GetLwpId());
+
     fd = InternalOpen(pThread, ctlPath, O_WRONLY);
     if (-1 == fd)
     {
similarity index 74%
rename from src/pal/src/misc/stackstring.cpp
rename to src/pal/src/include/pal/stackstring.hpp
index d35c0d8..aa8bd3e 100644 (file)
@@ -1,17 +1,17 @@
 // Copyright (c) Microsoft. All rights reserved.
 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 
 
-#include "pal/malloc.hpp"
-#include "pal/dbgmsg.h"
+#ifndef __STACKSTRING_H_
+#define __STACKSTRING_H_
 
-SET_DEFAULT_DEBUG_CHANNEL(MISC);
+#include "pal/malloc.hpp"
 
-template <SIZE_T STACKCOUNT>
+template <SIZE_T STACKCOUNT, class T>
 class StackString
 {
 private:
-    WCHAR m_innerBuffer[STACKCOUNT + 1];
-    WCHAR * m_buffer;
+    T m_innerBuffer[STACKCOUNT + 1];
+    T * m_buffer;
     SIZE_T m_count; // actual allocated count
 
     void NullTerminate()
@@ -31,10 +31,9 @@ private:
     void ReallocateBuffer(SIZE_T count)
     {
         // count is always > STACKCOUNT here.
-        WCHAR * newBuffer = (WCHAR *)PAL_malloc((count + 1) * sizeof(WCHAR));
+        T * newBuffer = (T *)PAL_malloc((count + 1) * sizeof(T));
         if (NULL == newBuffer)
         {
-            ERROR("malloc failed\n");
             SetLastError(ERROR_NOT_ENOUGH_MEMORY);
 
             DeleteBuffer();
@@ -84,24 +83,19 @@ private:
         Set(s);
     }
 
-    ~StackString()
-    {
-        DeleteBuffer();
-    }
-
 public:
     StackString()
-        : m_count(0), m_buffer(m_innerBuffer)
+        : m_buffer(m_innerBuffer), m_count(0)
     {
     }
 
-    BOOL Set(const WCHAR * buffer, SIZE_T count)
+    BOOL Set(const T * buffer, SIZE_T count)
     {
         Resize(count);
         if (NULL == m_buffer)
             return FALSE;
 
-        CopyMemory(m_buffer, buffer, (count + 1) * sizeof(WCHAR));
+        CopyMemory(m_buffer, buffer, (count + 1) * sizeof(T));
         NullTerminate();
         return TRUE;
     }
@@ -116,15 +110,15 @@ public:
         return m_count;
     }
 
-    CONST WCHAR * GetString() const
+    CONST T * GetString() const
     {
-        return (const WCHAR *)m_buffer;
+        return (const T *)m_buffer;
     }
 
-    WCHAR * OpenStringBuffer(SIZE_T count)
+    T * OpenStringBuffer(SIZE_T count)
     {
         Resize(count);
-        return (WCHAR *)m_buffer;
+        return (T *)m_buffer;
     }
 
     void CloseBuffer(SIZE_T count)
@@ -135,4 +129,18 @@ public:
         NullTerminate();
         return;
     }
+    
+    ~StackString()
+    {
+        DeleteBuffer();
+    }
 };
+
+#if _DEBUG
+typedef StackString<32, CHAR> PathCharString;
+typedef StackString<32, WCHAR> PathWCharString; 
+#else
+typedef StackString<260, CHAR> PathCharString;
+typedef StackString<260, WCHAR> PathWCharString; 
+#endif
+#endif