Skip to content

Commit

Permalink
Remove "wprintf" from PAL (dotnet#77852)
Browse files Browse the repository at this point in the history
The last usage was in FormatMessageW, which
only used wprintf syntax for messages that used
embedded printf format between two "!". We do
not use this feature in any of our messages so
this code path is technically unused.

Remove _snwprintf_s from PAL

Remove _vsnwprintf_s from PAL

_woutput_s was not removed because it is intertwined
with the implementation for printf. The linker will just
prune the implementation anyways.

FormatMessageW errors if embedded formatting is used
Update PAL tests for FormatMessageW
  • Loading branch information
AaronRobinsonMSFT authored Nov 4, 2022
1 parent ae1ada9 commit 8036885
Show file tree
Hide file tree
Showing 73 changed files with 33 additions and 4,830 deletions.
2 changes: 0 additions & 2 deletions src/coreclr/dlls/mscordac/mscordac_unixexports.src
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ nativeStringResourceTable_mscorrc
#sprintf_s
#vsprintf_s
#_snprintf_s
#_snwprintf_s
#_vsnprintf_s
#_vsnwprintf_s
#_itow_s
#_i64tow_s
#memcpy_s
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/pal/inc/mbusafecrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,10 @@ extern errno_t _wsplitpath_s( const WCHAR* inPath, WCHAR* outDrive, size_t inDri
extern int sprintf_s( char *string, size_t sizeInBytes, const char *format, ... );

extern int _snprintf_s( char *string, size_t sizeInBytes, size_t count, const char *format, ... );
extern int _snwprintf_s( WCHAR *string, size_t sizeInWords, size_t count, const WCHAR *format, ... );

extern int vsprintf_s( char* string, size_t sizeInBytes, const char* format, va_list arglist );
extern int _vsnprintf_s( char* string, size_t sizeInBytes, size_t count, const char* format, va_list arglist );

extern int _vsnwprintf_s( WCHAR* string, size_t sizeInWords, size_t count, const WCHAR* format, va_list arglist );

extern int sscanf_s( const char *string, const char *format, ... );
extern int swscanf_s( const WCHAR *string, const WCHAR *format, ... );

Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -4122,11 +4122,8 @@ PALIMPORT DLLEXPORT int __cdecl _wcsicmp(const WCHAR *, const WCHAR*);
PALIMPORT int __cdecl _wcsnicmp(const WCHAR *, const WCHAR *, size_t);
PALIMPORT int __cdecl _vsnprintf(char *, size_t, const char *, va_list);
PALIMPORT DLLEXPORT int __cdecl _vsnprintf_s(char *, size_t, size_t, const char *, va_list);
PALIMPORT DLLEXPORT int __cdecl _vsnwprintf_s(WCHAR *, size_t, size_t, const WCHAR *, va_list);
PALIMPORT DLLEXPORT int __cdecl _snwprintf_s(WCHAR *, size_t, size_t, const WCHAR *, ...);
PALIMPORT DLLEXPORT int __cdecl _snprintf_s(char *, size_t, size_t, const char *, ...);
PALIMPORT DLLEXPORT int __cdecl sprintf_s(char *, size_t, const char *, ... );
PALIMPORT int __cdecl _snwprintf_s(WCHAR *, size_t, size_t, const WCHAR *, ...);
PALIMPORT DLLEXPORT int __cdecl sscanf_s(const char *, const char *, ...);
PALIMPORT DLLEXPORT errno_t __cdecl _itow_s(int, WCHAR *, size_t, int);

Expand Down
30 changes: 1 addition & 29 deletions src/coreclr/pal/inc/rt/safecrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ void __cdecl _invalid_parameter(const WCHAR *_Message, const WCHAR *_FunctionNam
#define _tmakepath_s _wmakepath_s
#define _tsplitpath_s _wsplitpath_s
#define _stprintf_s swprintf_s
#define _vsntprintf_s _vsnwprintf_s
#define _tscanf_s wscanf_s
#define _tsscanf_s swscanf_s

Expand Down Expand Up @@ -1681,7 +1680,7 @@ int __cdecl vsprintf_s(char (&_Dst)[_SizeInBytes], const char *_Format, va_list

/* _vsnprintf_s */
/*
* _snwprintf_s, _vsnprintf_s, _vsnwprintf_s format a string and copy at max _Count characters into _Dst;
* _vsnprintf_s formats a string and copy at max _Count characters into _Dst;
* need safecrt.lib and msvcrt.dll;
* string _Dst will always be null-terminated;
* will call _SAFECRT_INVALID_PARAMETER if there is not enough space in _Dst;
Expand All @@ -1707,33 +1706,6 @@ int __cdecl _vsnprintf_s(char (&_Dst)[_SizeInBytes], size_t _Count, const char *

/* no inline version of _vsnprintf_s */

/* _snwprintf_s, _vsnwprintf_s */
_SAFECRT__EXTERN_C
int __cdecl _vsnwprintf_s(WCHAR *_Dst, size_t _SizeInWords, size_t _Count, const WCHAR *_Format, va_list _ArgList);

#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
template <size_t _SizeInWords>
inline
int __cdecl _snwprintf_s(WCHAR (&_Dst)[_SizeInWords], size_t _Count, const WCHAR *_Format, ...)
{
int ret;
va_list _ArgList;
va_start(_ArgList, _Format);
ret = _vsnwprintf_s(_Dst, _SizeInWords, _Count, _Format, _ArgList);
va_end(_ArgList);
return ret;
}

template <size_t _SizeInWords>
inline
int __cdecl _vsnwprintf_s(char (&_Dst)[_SizeInWords], size_t _Count, const char *_Format, va_list _ArgList)
{
return _vsnwprintf_s(_Dst, _SizeInWords, _Count, _Format, _ArgList);
}
#endif

/* no inline version of _snwprintf_s, _vsnwprintf_s */

/* wscanf_s */
_SAFECRT__EXTERN_C
int __cdecl wscanf_s(const WCHAR *_Format, ...);
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,7 @@ set(SOURCES
safecrt/strncat_s.cpp
safecrt/strncpy_s.cpp
safecrt/strtok_s.cpp
safecrt/swprintf.cpp
safecrt/vsprintf.cpp
safecrt/vswprint.cpp
safecrt/wcscat_s.cpp
safecrt/wcscpy_s.cpp
safecrt/wcslen_s.cpp
Expand Down
216 changes: 20 additions & 196 deletions src/coreclr/pal/src/misc/fmtmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,102 +180,6 @@ of _ADD_TO_STRING, as we will resize the buffer if necessary. */
_ADD_TO_STRING( c );\
}


/*++
Function :
FMTMSG_ProcessPrintf
Processes the printf formatters based on the format.
Returns the LPWSTR string, or NULL on failure.
*/

static LPWSTR FMTMSG_ProcessPrintf( WCHAR c ,
LPWSTR lpPrintfString,
LPWSTR lpInsertString)
{
LPWSTR lpBuffer = NULL;
LPWSTR lpBuffer2 = NULL;
LPWSTR lpFormat = NULL;
#if _DEBUG
// small size for _DEBUG to exercise buffer reallocation logic
int tmpSize = 4;
#else
int tmpSize = 64;
#endif
UINT nFormatLength = 0;
int nBufferLength = 0;

TRACE( "FMTMSG_ProcessPrintf( %C, %S, %p )\n", c,
lpPrintfString, lpInsertString );

switch ( c )
{
case 'e' :
/* Fall through */
case 'E' :
/* Fall through */
case 'f' :
/* Fall through */
case 'g' :
/* Fall through */
case 'G' :
ERROR( "%%%c is not supported by FormatMessage.\n", c );
SetLastError( ERROR_INVALID_PARAMETER );
return NULL;
}

nFormatLength = PAL_wcslen( lpPrintfString ) + 2; /* Need to count % AND NULL */
lpFormat = (LPWSTR)PAL_malloc( nFormatLength * sizeof( WCHAR ) );
if ( !lpFormat )
{
ERROR( "Unable to allocate memory.\n" );
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return NULL;
}
/* Create the format string. */
memset( lpFormat, 0, nFormatLength * sizeof(WCHAR) );
*lpFormat = '%';

PAL_wcscat( lpFormat, lpPrintfString );

lpBuffer = (LPWSTR) PAL_malloc(tmpSize*sizeof(WCHAR));

/* try until the buffer is big enough */
while (TRUE)
{
if (!lpBuffer)
{
ERROR("Unable to allocate memory\n");
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
PAL_free(lpFormat);
return NULL;
}
nBufferLength = _snwprintf_s( lpBuffer, tmpSize, tmpSize,
lpFormat, lpInsertString);

if ((nBufferLength >= 0) && (nBufferLength != tmpSize))
{
break; /* succeeded */
}
else
{
tmpSize *= 2;
lpBuffer2 = static_cast<WCHAR *>(
PAL_realloc(lpBuffer, tmpSize*sizeof(WCHAR)));
if (lpBuffer2 == NULL)
PAL_free(lpBuffer);
lpBuffer = lpBuffer2;
}
}

PAL_free( lpFormat );
lpFormat = NULL;

return lpBuffer;
}

/*++
Function:
FormatMessageW
Expand Down Expand Up @@ -481,113 +385,33 @@ FormatMessageW(
}
if ( *lpSourceString == '!' )
{
LPWSTR lpInsertString = NULL;
LPWSTR lpPrintfString = NULL;
LPWSTR lpStartOfFormattedString = NULL;
UINT nPrintfLength = 0;
LPWSTR lpFormattedString = NULL;
UINT nFormattedLength = 0;

if ( !bIsVaList )
{
lpInsertString = ((LPWSTR*)Arguments)[ Index - 1 ];
}
else
{
va_list TheArgs;

va_copy(TheArgs, *Arguments);
UINT i = 0;
for ( ; i < Index; i++ )
{
lpInsertString = va_arg( TheArgs, LPWSTR );
}
}

/* Calculate the length, and extract the printf string.*/
lpSourceString++;
{
LPWSTR p = PAL_wcschr( lpSourceString, '!' );

if ( NULL == p )
{
nPrintfLength = 0;
}
else
{
nPrintfLength = p - lpSourceString;
}
}

lpPrintfString =
(LPWSTR)PAL_malloc( ( nPrintfLength + 1 ) * sizeof( WCHAR ) );

if ( !lpPrintfString )
{
ERROR( "Unable to allocate memory.\n" );
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
lpWorkingString = NULL;
nCount = 0;
goto exit;
}

PAL_wcsncpy( lpPrintfString, lpSourceString, nPrintfLength );
*( lpPrintfString + nPrintfLength ) = '\0';

lpStartOfFormattedString = lpFormattedString =
FMTMSG_ProcessPrintf( *lpPrintfString,
lpPrintfString,
lpInsertString);

if ( !lpFormattedString )
{
ERROR( "Unable to process the format string.\n" );
/* Function will set the error code. */
PAL_free( lpPrintfString );
lpWorkingString = NULL;
goto exit;
}


nFormattedLength = PAL_wcslen( lpFormattedString );

/* Append the processed printf string into the working string */
while ( *lpFormattedString )
{
_CHECKED_ADD_TO_STRING( *lpFormattedString );
lpFormattedString++;
}
ERROR( "Embedded printf formatting ('!<printf format>!') is unsupported\n" );
SetLastError( ERROR_INVALID_PARAMETER );
lpWorkingString = NULL;
nCount = 0;
goto exit;
}

lpSourceString += nPrintfLength + 1;
PAL_free( lpPrintfString );
PAL_free( lpStartOfFormattedString );
lpPrintfString = lpFormattedString = NULL;
LPWSTR lpInsert = NULL;
if ( !bIsVaList )
{
lpInsert = ((LPWSTR*)Arguments)[Index - 1];
}
else
{
/* The printf format string defaults to 's'.*/
LPWSTR lpInsert = NULL;

if ( !bIsVaList )
{
lpInsert = ((LPWSTR*)Arguments)[Index - 1];
}
else
va_list TheArgs;
va_copy(TheArgs, *Arguments);
UINT i = 0;
for ( ; i < Index; i++ )
{
va_list TheArgs;
va_copy(TheArgs, *Arguments);
UINT i = 0;
for ( ; i < Index; i++ )
{
lpInsert = va_arg( TheArgs, LPWSTR );
}
lpInsert = va_arg( TheArgs, LPWSTR );
}
}

while ( *lpInsert )
{
_CHECKED_ADD_TO_STRING( *lpInsert );
lpInsert++;
}
while ( *lpInsert )
{
_CHECKED_ADD_TO_STRING( *lpInsert );
lpInsert++;
}
}
/* Format specifiers. */
Expand Down
7 changes: 0 additions & 7 deletions src/coreclr/pal/src/safecrt/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,21 +279,15 @@ int __cdecl _output(__inout FILE * _File, _In_z_ __format_string const char *_Fo
int __cdecl _woutput(__inout FILE * _File, _In_z_ __format_string const char16_t *_Format, va_list _ArgList);
int __cdecl _output_s(__inout FILE * _File, _In_z_ __format_string const char *_Format, va_list _ArgList);
int __cdecl _output_p(__inout FILE * _File, _In_z_ __format_string const char *_Format, va_list _ArgList);
int __cdecl _woutput_s(__inout FILE * _File, _In_z_ __format_string const char16_t *_Format, va_list _ArgList);
int __cdecl _woutput_p(__inout FILE * _File, _In_z_ __format_string const char16_t *_Format, va_list _ArgList);
typedef int (*OUTPUTFN)(FILE *, const char *, va_list);
typedef int (*WOUTPUTFN)(FILE *, const char16_t *, va_list);

#else /* _SAFECRT_IMPL */

int __cdecl _output_l(__inout FILE * _File, _In_z_ __format_string const char *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList);
int __cdecl _woutput_l(__inout FILE * _File, _In_z_ __format_string const char16_t *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList);
int __cdecl _output_s_l(__inout FILE * _File, _In_z_ __format_string const char *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList);
int __cdecl _output_p_l(__inout FILE * _File, _In_z_ __format_string const char *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList);
int __cdecl _woutput_s_l(__inout FILE * _File, _In_z_ __format_string const char16_t *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList);
int __cdecl _woutput_p_l(__inout FILE * _File, _In_z_ __format_string const char16_t *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList);
typedef int (*OUTPUTFN)(__inout FILE * _File, const char *, _locale_t, va_list);
typedef int (*WOUTPUTFN)(__inout FILE * _File, const char16_t *, _locale_t, va_list);

#endif /* _SAFECRT_IMPL */

Expand All @@ -302,7 +296,6 @@ typedef int (*WOUTPUTFN)(__inout FILE * _File, const char16_t *, _locale_t, va_l
int __cdecl _input(_In_ FILE * _File, _In_z_ __format_string const unsigned char * _Format, va_list _ArgList);
int __cdecl _winput(_In_ FILE * _File, _In_z_ __format_string const char16_t * _Format, va_list _ArgList);
int __cdecl _input_s(_In_ FILE * _File, _In_z_ __format_string const unsigned char * _Format, va_list _ArgList);
int __cdecl _winput_s(_In_ FILE * _File, _In_z_ __format_string const char16_t * _Format, va_list _ArgList);
typedef int (*INPUTFN)(FILE *, const unsigned char *, va_list);
typedef int (*WINPUTFN)(FILE *, const char16_t *, va_list);

Expand Down
1 change: 0 additions & 1 deletion src/coreclr/pal/src/safecrt/mbusafecrt_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ void _safecrt_wfassign(int flag, void* argument, char16_t * number );
int _minimal_chartowchar( char16_t* outWChar, const char* inChar );

int _output_s( miniFILE* outfile, const char* _Format, va_list _ArgList);
int _woutput_s( miniFILE* outfile, const char16_t* _Format, va_list _ArgList);
int _output( miniFILE *outfile, const char* _Format, va_list _ArgList);

int __tinput_s( miniFILE* inFile, const unsigned char * inFormat, va_list inArgList );
Expand Down
Loading

0 comments on commit 8036885

Please sign in to comment.