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

Commit

Permalink
Remove all usage of vsnprintf
Browse files Browse the repository at this point in the history
This change removes all usages of vsnprintf and modifies runtime to not to use
vsnprintf or _vsnprintf

I've also fixed two issues in PAL TRACE function string format parameters that
caused crashes when I was trying to run all PAL tests with PAL tracing enabled.
  • Loading branch information
janvorli committed Dec 22, 2016
1 parent 14b0b57 commit f011398
Show file tree
Hide file tree
Showing 105 changed files with 129 additions and 1,757 deletions.
4 changes: 0 additions & 4 deletions src/ToolBox/SOS/Strike/strike.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@
#define _wcsstr wcsstr
#endif // !PAL_STDCPP_COMPAT

#ifdef PLATFORM_UNIX
#define _vsnprintf vsnprintf
#endif

#define ___in _SAL1_Source_(__in, (), _In_)
#define ___out _SAL1_Source_(__out, (), _Out_)

Expand Down
2 changes: 1 addition & 1 deletion src/ToolBox/SOS/Strike/xplat/dbgeng.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class DebugClient
va_list args)
{
char str[4096];
int length = PAL__vsnprintf(str, sizeof(str), format, args);
int length = _vsnprintf_s(str, sizeof(str), _TRUNCATE, format, args);
if (length > 0)
{
return Output(mask, "%s", str);
Expand Down
4 changes: 2 additions & 2 deletions src/dlls/mscordac/mscordac_unixexports.src
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ PAL_fflush
PAL__flushall
PAL_free
PAL_fwprintf
PAL_swprintf
PAL_GetPALDirectoryW
PAL_GetResourceString
PAL_get_stdout
Expand All @@ -35,7 +34,6 @@ PAL_printf
PAL_qsort
PAL_Reenter
PAL_fprintf
PAL__vsnprintf
PAL__wcstoui64
PAL_wcstoul
PAL_iswprint
Expand All @@ -52,6 +50,8 @@ _wcsicmp
_stricmp
sprintf_s
swprintf_s
vsprintf_s
_snprintf_s
_snwprintf_s
_vsnprintf_s
_vsnwprintf_s
Expand Down
2 changes: 1 addition & 1 deletion src/gc/env/gcenv.base.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ inline HRESULT HRESULT_FROM_WIN32(unsigned long x)
#define UNREFERENCED_PARAMETER(P) (void)(P)

#ifdef PLATFORM_UNIX
#define _vsnprintf vsnprintf
#define _vsnprintf_s(string, sizeInBytes, count, format, args) vsnprintf(string, sizeInBytes, format, args)
#define sprintf_s snprintf
#define swprintf_s swprintf
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/gc/gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ void log_va_msg(const char *fmt, va_list args)
int pid_len = sprintf_s (&pBuffer[buffer_start], BUFFERSIZE - buffer_start, "[%5d]", (uint32_t)GCToOSInterface::GetCurrentThreadIdForLogging());
buffer_start += pid_len;
memset(&pBuffer[buffer_start], '-', BUFFERSIZE - buffer_start);
int msg_len = _vsnprintf(&pBuffer[buffer_start], BUFFERSIZE - buffer_start, fmt, args );
int msg_len = _vsnprintf_s(&pBuffer[buffer_start], BUFFERSIZE - buffer_start, _TRUNCATE, fmt, args );
if (msg_len == -1)
{
msg_len = BUFFERSIZE - buffer_start;
Expand Down
5 changes: 4 additions & 1 deletion src/md/compiler/regmeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,10 @@ int DumpMD_VWriteMarker(__in __in_z const char *str, va_list marker)
{
if (FAILED(hr = m_output.ReSizeNoThrow(STRING_BUFFER_LEN * i)))
return 0;
count = _vsnprintf((char *)m_output.Ptr(), STRING_BUFFER_LEN * i, str, marker);
va_list markerCopy;
va_copy(markerCopy, marker);
count = _vsnprintf_s((char *)m_output.Ptr(), STRING_BUFFER_LEN * i, _TRUNCATE, str, markerCopy);
va_end(markerCopy);
i *= 2;
}
OutputDebugStringA((LPCSTR)m_output.Ptr());
Expand Down
6 changes: 0 additions & 6 deletions src/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5667,7 +5667,6 @@ CoCreateGuid(OUT GUID * pguid);
#define printf PAL_printf
#define vprintf PAL_vprintf
#define wprintf PAL_wprintf
#define swprintf PAL_swprintf
#define wcsspn PAL_wcsspn
#define wcstod PAL_wcstod
#define wcstol PAL_wcstol
Expand All @@ -5694,8 +5693,6 @@ CoCreateGuid(OUT GUID * pguid);
#define iswxdigit PAL_iswxdigit
#define towlower PAL_towlower
#define towupper PAL_towupper
#define vsprintf PAL_vsprintf
#define vswprintf PAL_vswprintf
#define realloc PAL_realloc
#define fopen PAL_fopen
#define strtok PAL_strtok
Expand Down Expand Up @@ -5758,7 +5755,6 @@ CoCreateGuid(OUT GUID * pguid);
#define _close PAL__close
#define _wcstoui64 PAL__wcstoui64
#define _flushall PAL__flushall
#define _vsnprintf PAL__vsnprintf
#define strnlen PAL_strnlen

#ifdef _AMD64_
Expand Down Expand Up @@ -5836,7 +5832,6 @@ PALIMPORT char * __cdecl strstr(const char *, const char *);
PALIMPORT char * __cdecl strtok(char *, const char *);
PALIMPORT size_t __cdecl strspn(const char *, const char *);
PALIMPORT size_t __cdecl strcspn(const char *, const char *);
PALIMPORT int __cdecl vsprintf(char *, const char *, va_list);
PALIMPORT int __cdecl atoi(const char *);
PALIMPORT LONG __cdecl atol(const char *);
PALIMPORT ULONG __cdecl strtoul(const char *, char **, int);
Expand Down Expand Up @@ -5900,7 +5895,6 @@ PALIMPORT WCHAR * __cdecl PAL_wcstok(WCHAR *, const WCHAR *);
PALIMPORT size_t __cdecl PAL_wcscspn(const WCHAR *, const WCHAR *);
PALIMPORT int __cdecl PAL_swprintf(WCHAR *, const WCHAR *, ...);
PALIMPORT int __cdecl PAL_vswprintf(WCHAR *, const WCHAR *, va_list);
PALIMPORT int __cdecl PAL__vsnprintf(LPSTR Buffer, size_t Count, LPCSTR Format, va_list ap);
PALIMPORT int __cdecl PAL_swscanf(const WCHAR *, const WCHAR *, ...);
PALIMPORT LONG __cdecl PAL_wcstol(const WCHAR *, WCHAR **, int);
PALIMPORT ULONG __cdecl PAL_wcstoul(const WCHAR *, WCHAR **, int);
Expand Down
30 changes: 4 additions & 26 deletions src/pal/inc/rt/palrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,6 @@ Remember to fix the errcode defintion in safecrt.h.
*/

#define _wcslwr_s _wcslwr_unsafe
#define _snprintf_s _snprintf_unsafe
#define swscanf_s swscanf

#define _wfopen_s _wfopen_unsafe
Expand All @@ -903,8 +902,6 @@ Remember to fix the errcode defintion in safecrt.h.

#define _vscprintf _vscprintf_unsafe

#define vsprintf_s _vsnprintf

extern "C++" {

#include <safemath.h>
Expand Down Expand Up @@ -959,8 +956,11 @@ inline int __cdecl _vscprintf_unsafe(const char *_Format, va_list _ArgList)
if(buf == nullptr)
return 0;

int ret = _vsnprintf(buf, guess, _Format, _ArgList);
va_list argListCopy;
va_copy(argListCopy, _ArgList);
int ret = _vsnprintf_s(buf, guess, _TRUNCATE, _Format, argListCopy);
free(buf);
va_end(argListCopy);

if ((ret != -1) && (ret < guess))
return ret;
Expand All @@ -969,28 +969,6 @@ inline int __cdecl _vscprintf_unsafe(const char *_Format, va_list _ArgList)
}
}

inline int __cdecl _vsnprintf_unsafe(char *_Dst, size_t _SizeInWords, size_t _Count, const char *_Format, va_list _ArgList)
{
if (_Count == _TRUNCATE) _Count = _SizeInWords - 1;
int ret = _vsnprintf(_Dst, _Count, _Format, _ArgList);
_Dst[_SizeInWords - 1] = L'\0';
if (ret < 0 && errno == 0)
{
errno = ERANGE;
}
return ret;
}

inline int __cdecl _snprintf_unsafe(char *_Dst, size_t _SizeInWords, size_t _Count, const char *_Format, ...)
{
int ret;
va_list _ArgList;
va_start(_ArgList, _Format);
ret = _vsnprintf_unsafe(_Dst, _SizeInWords, _Count, _Format, _ArgList);
va_end(_ArgList);
return ret;
}

inline errno_t __cdecl _wfopen_unsafe(PAL_FILE * *ff, const WCHAR *fileName, const WCHAR *mode)
{
PAL_FILE *result = _wfopen(fileName, mode);
Expand Down
4 changes: 0 additions & 4 deletions src/pal/inc/rt/safecrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3253,10 +3253,6 @@ int __cdecl vswprintf_s(WCHAR (&_Dst)[_SizeInWords], const WCHAR *_Format, va_li
* return -1 if the formatted string does not entirely fit into _Dst (we will not call _SAFECRT_INVALID_PARAMETER);
* if _Count == 0, then (_Dst == nullptr && _SizeInBytes == 0) is allowed
*/
_SAFECRT__EXTERN_C
int __cdecl _snprintf_s(char *_Dst, size_t _SizeInBytes, size_t _Count, const char *_Format, ...);
_SAFECRT__EXTERN_C
int __cdecl _vsnprintf_s(char *_Dst, size_t _SizeInBytes, size_t _Count, const char *_Format, va_list _ArgList);

#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
template <size_t _SizeInBytes>
Expand Down
82 changes: 0 additions & 82 deletions src/pal/src/cruntime/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,34 +1363,6 @@ int PAL_wvsscanf(LPCWSTR Buffer, LPCWSTR Format, va_list ap)
return Length;
}

/*++
Function:
PAL_swprintf

See MSDN doc.
--*/
int
__cdecl
PAL_swprintf(
wchar_16 *buffer,
const wchar_16 *format,
...)
{
LONG Length;
va_list ap;

PERF_ENTRY(swprintf);
ENTRY("PAL_swprintf (buffer=%p, format=%p (%S))\n", buffer, format, format);

va_start(ap, format);
Length = PAL__wvsnprintf(buffer, 0x7fffffff, format, ap);
va_end(ap);

LOGEXIT("PAL_swprintf returns int %d\n", Length);
PERF_EXIT(swprintf);
return Length;
}

/*++
Function:
PAL_swscanf
Expand Down Expand Up @@ -1420,60 +1392,6 @@ PAL_swscanf(
}


/*++
Function:
PAL_vsprintf

See MSDN doc.
--*/
int
__cdecl
PAL_vsprintf(char *buffer,
const char *format,
va_list argptr)
{
LONG Length;

PERF_ENTRY(vsprintf);
ENTRY("PAL_vsprintf (buffer=%p, format=%p (%s), argptr=%p)\n",
buffer, format, format, argptr);

Length = InternalVsnprintf(CorUnix::InternalGetCurrentThread(), buffer, 0x7fffffff, format, argptr);

LOGEXIT("PAL_vsprintf returns int %d\n", Length);
PERF_EXIT(vsprintf);

return Length;
}


/*++
Function:
PAL_vswprintf

See MSDN doc.
--*/
int
__cdecl
PAL_vswprintf(wchar_16 *buffer,
const wchar_16 *format,
va_list argptr)
{
LONG Length;

PERF_ENTRY(vswprintf);
ENTRY("PAL_vswprintf (buffer=%p, format=%p (%S), argptr=%p)\n",
buffer, format, format, argptr);

Length = PAL__wvsnprintf(buffer, 0x7fffffff, format, argptr);

LOGEXIT("PAL_vswprintf returns int %d\n", Length);
PERF_EXIT(vswprintf);

return Length;
}


#if SSCANF_CANNOT_HANDLE_MISSING_EXPONENT
/*++
Function:
Expand Down
Loading

0 comments on commit f011398

Please sign in to comment.