Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 0633a25

Browse files
committed
Merge pull request #1689 from Priya91/palstring
Apply StackString for pal MAX_LONGPATH references.
2 parents 0eb192e + 4da174f commit 0633a25

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

src/pal/src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ set(SOURCES
138138
misc/interlock.cpp
139139
misc/miscpalapi.cpp
140140
misc/msgbox.cpp
141-
misc/stackstring.cpp
142141
misc/strutil.cpp
143142
misc/sysinfo.cpp
144143
misc/time.cpp

src/pal/src/debug/debug.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Revision History:
3838
#include "pal/misc.h"
3939
#include "pal/malloc.hpp"
4040
#include "pal/module.h"
41+
#include "pal/stackstring.hpp"
4142
#include "pal/virtual.h"
4243

4344
#include <signal.h>
@@ -343,15 +344,24 @@ DebugBreakCommand()
343344
const char *command_string = getenv (PAL_RUN_ON_DEBUG_BREAK);
344345
if (command_string) {
345346
char pid_buf[sizeof (PID_TEXT) + 32];
346-
char exe_buf[sizeof (EXE_TEXT) + MAX_LONGPATH + 1];
347+
PathCharString exe_bufString;
348+
int libNameLength = 10;
349+
if (exe_module.lib_name != NULL)
350+
{
351+
libNameLength = PAL_wcslen(exe_module.lib_name);
352+
}
353+
354+
SIZE_T dwexe_buf = strlen(EXE_TEXT) + libNameLength + 1;
355+
CHAR * exe_buf = exe_bufString.OpenStringBuffer(dwexe_buf);
347356

348357
if (snprintf (pid_buf, sizeof (pid_buf), PID_TEXT "%d", getpid()) <= 0) {
349358
goto FAILED;
350359
}
351-
if (snprintf (exe_buf, sizeof (exe_buf), EXE_TEXT "%ls", (wchar_t *)exe_module.lib_name) <= 0) {
360+
if (snprintf (exe_buf, sizeof (CHAR) * (dwexe_buf + 1), EXE_TEXT "%ls", (wchar_t *)exe_module.lib_name) <= 0) {
352361
goto FAILED;
353362
}
354363

364+
exe_bufString.CloseBuffer(dwexe_buf);
355365
/* strictly speaking, we might want to only set these environment
356366
variables in the child process, but if we do that we can't check
357367
for errors. putenv/setenv can fail when out of memory */
@@ -1598,7 +1608,7 @@ PAL_CreateExecWatchpoint(
15981608
CPalThread *pTargetThread = NULL;
15991609
IPalObject *pobjThread = NULL;
16001610
int fd = -1;
1601-
char ctlPath[MAX_LONGPATH];
1611+
char ctlPath[50];
16021612

16031613
struct
16041614
{
@@ -1642,6 +1652,7 @@ PAL_CreateExecWatchpoint(
16421652
}
16431653

16441654
snprintf(ctlPath, sizeof(ctlPath), "/proc/%u/lwp/%u/lwpctl", getpid(), pTargetThread->GetLwpId());
1655+
16451656
fd = InternalOpen(pThread, ctlPath, O_WRONLY);
16461657
if (-1 == fd)
16471658
{
@@ -1719,7 +1730,7 @@ PAL_DeleteExecWatchpoint(
17191730
CPalThread *pTargetThread = NULL;
17201731
IPalObject *pobjThread = NULL;
17211732
int fd = -1;
1722-
char ctlPath[MAX_LONGPATH];
1733+
char ctlPath[50];
17231734

17241735
struct
17251736
{
@@ -1744,6 +1755,7 @@ PAL_DeleteExecWatchpoint(
17441755
}
17451756

17461757
snprintf(ctlPath, sizeof(ctlPath), "/proc/%u/lwp/%u/lwpctl", getpid(), pTargetThread->GetLwpId());
1758+
17471759
fd = InternalOpen(pThread, ctlPath, O_WRONLY);
17481760
if (-1 == fd)
17491761
{

src/pal/src/misc/stackstring.cpp renamed to src/pal/src/include/pal/stackstring.hpp

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
#include "pal/malloc.hpp"
5-
#include "pal/dbgmsg.h"
4+
#ifndef __STACKSTRING_H_
5+
#define __STACKSTRING_H_
66

7-
SET_DEFAULT_DEBUG_CHANNEL(MISC);
7+
#include "pal/malloc.hpp"
88

9-
template <SIZE_T STACKCOUNT>
9+
template <SIZE_T STACKCOUNT, class T>
1010
class StackString
1111
{
1212
private:
13-
WCHAR m_innerBuffer[STACKCOUNT + 1];
14-
WCHAR * m_buffer;
13+
T m_innerBuffer[STACKCOUNT + 1];
14+
T * m_buffer;
1515
SIZE_T m_count; // actual allocated count
1616

1717
void NullTerminate()
1818
{
19-
m_buffer[m_count] = W('\0');
19+
m_buffer[m_count] = 0;
2020
}
2121

2222
void DeleteBuffer()
@@ -31,10 +31,9 @@ class StackString
3131
void ReallocateBuffer(SIZE_T count)
3232
{
3333
// count is always > STACKCOUNT here.
34-
WCHAR * newBuffer = (WCHAR *)PAL_malloc((count + 1) * sizeof(WCHAR));
34+
T * newBuffer = (T *)PAL_malloc((count + 1) * sizeof(T));
3535
if (NULL == newBuffer)
3636
{
37-
ERROR("malloc failed\n");
3837
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
3938

4039
DeleteBuffer();
@@ -84,24 +83,19 @@ class StackString
8483
Set(s);
8584
}
8685

87-
~StackString()
88-
{
89-
DeleteBuffer();
90-
}
91-
9286
public:
9387
StackString()
94-
: m_count(0), m_buffer(m_innerBuffer)
88+
: m_buffer(m_innerBuffer), m_count(0)
9589
{
9690
}
9791

98-
BOOL Set(const WCHAR * buffer, SIZE_T count)
92+
BOOL Set(const T * buffer, SIZE_T count)
9993
{
10094
Resize(count);
10195
if (NULL == m_buffer)
10296
return FALSE;
10397

104-
CopyMemory(m_buffer, buffer, (count + 1) * sizeof(WCHAR));
98+
CopyMemory(m_buffer, buffer, (count + 1) * sizeof(T));
10599
NullTerminate();
106100
return TRUE;
107101
}
@@ -111,20 +105,25 @@ class StackString
111105
return Set(s.m_buffer, s.m_count);
112106
}
113107

114-
SIZE_T Getcount() const
108+
SIZE_T GetCount() const
115109
{
116110
return m_count;
117111
}
112+
113+
SIZE_T GetSizeOf() const
114+
{
115+
return (m_count+1) * sizeof(T);
116+
}
118117

119-
CONST WCHAR * GetString() const
118+
CONST T * GetString() const
120119
{
121-
return (const WCHAR *)m_buffer;
120+
return (const T *)m_buffer;
122121
}
123122

124-
WCHAR * OpenStringBuffer(SIZE_T count)
123+
T * OpenStringBuffer(SIZE_T count)
125124
{
126125
Resize(count);
127-
return (WCHAR *)m_buffer;
126+
return (T *)m_buffer;
128127
}
129128

130129
void CloseBuffer(SIZE_T count)
@@ -135,4 +134,18 @@ class StackString
135134
NullTerminate();
136135
return;
137136
}
137+
138+
~StackString()
139+
{
140+
DeleteBuffer();
141+
}
138142
};
143+
144+
#if _DEBUG
145+
typedef StackString<32, CHAR> PathCharString;
146+
typedef StackString<32, WCHAR> PathWCharString;
147+
#else
148+
typedef StackString<260, CHAR> PathCharString;
149+
typedef StackString<260, WCHAR> PathWCharString;
150+
#endif
151+
#endif

0 commit comments

Comments
 (0)