Skip to content

Commit 8036885

Browse files
Remove "wprintf" from PAL (#77852)
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
1 parent ae1ada9 commit 8036885

73 files changed

Lines changed: 33 additions & 4830 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/coreclr/dlls/mscordac/mscordac_unixexports.src

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ nativeStringResourceTable_mscorrc
7575
#sprintf_s
7676
#vsprintf_s
7777
#_snprintf_s
78-
#_snwprintf_s
7978
#_vsnprintf_s
80-
#_vsnwprintf_s
8179
#_itow_s
8280
#_i64tow_s
8381
#memcpy_s

src/coreclr/pal/inc/mbusafecrt.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,10 @@ extern errno_t _wsplitpath_s( const WCHAR* inPath, WCHAR* outDrive, size_t inDri
8686
extern int sprintf_s( char *string, size_t sizeInBytes, const char *format, ... );
8787

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

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

94-
extern int _vsnwprintf_s( WCHAR* string, size_t sizeInWords, size_t count, const WCHAR* format, va_list arglist );
95-
9693
extern int sscanf_s( const char *string, const char *format, ... );
9794
extern int swscanf_s( const WCHAR *string, const WCHAR *format, ... );
9895

src/coreclr/pal/inc/pal.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4122,11 +4122,8 @@ PALIMPORT DLLEXPORT int __cdecl _wcsicmp(const WCHAR *, const WCHAR*);
41224122
PALIMPORT int __cdecl _wcsnicmp(const WCHAR *, const WCHAR *, size_t);
41234123
PALIMPORT int __cdecl _vsnprintf(char *, size_t, const char *, va_list);
41244124
PALIMPORT DLLEXPORT int __cdecl _vsnprintf_s(char *, size_t, size_t, const char *, va_list);
4125-
PALIMPORT DLLEXPORT int __cdecl _vsnwprintf_s(WCHAR *, size_t, size_t, const WCHAR *, va_list);
4126-
PALIMPORT DLLEXPORT int __cdecl _snwprintf_s(WCHAR *, size_t, size_t, const WCHAR *, ...);
41274125
PALIMPORT DLLEXPORT int __cdecl _snprintf_s(char *, size_t, size_t, const char *, ...);
41284126
PALIMPORT DLLEXPORT int __cdecl sprintf_s(char *, size_t, const char *, ... );
4129-
PALIMPORT int __cdecl _snwprintf_s(WCHAR *, size_t, size_t, const WCHAR *, ...);
41304127
PALIMPORT DLLEXPORT int __cdecl sscanf_s(const char *, const char *, ...);
41314128
PALIMPORT DLLEXPORT errno_t __cdecl _itow_s(int, WCHAR *, size_t, int);
41324129

src/coreclr/pal/inc/rt/safecrt.h

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,6 @@ void __cdecl _invalid_parameter(const WCHAR *_Message, const WCHAR *_FunctionNam
385385
#define _tmakepath_s _wmakepath_s
386386
#define _tsplitpath_s _wsplitpath_s
387387
#define _stprintf_s swprintf_s
388-
#define _vsntprintf_s _vsnwprintf_s
389388
#define _tscanf_s wscanf_s
390389
#define _tsscanf_s swscanf_s
391390

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

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

17081707
/* no inline version of _vsnprintf_s */
17091708

1710-
/* _snwprintf_s, _vsnwprintf_s */
1711-
_SAFECRT__EXTERN_C
1712-
int __cdecl _vsnwprintf_s(WCHAR *_Dst, size_t _SizeInWords, size_t _Count, const WCHAR *_Format, va_list _ArgList);
1713-
1714-
#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
1715-
template <size_t _SizeInWords>
1716-
inline
1717-
int __cdecl _snwprintf_s(WCHAR (&_Dst)[_SizeInWords], size_t _Count, const WCHAR *_Format, ...)
1718-
{
1719-
int ret;
1720-
va_list _ArgList;
1721-
va_start(_ArgList, _Format);
1722-
ret = _vsnwprintf_s(_Dst, _SizeInWords, _Count, _Format, _ArgList);
1723-
va_end(_ArgList);
1724-
return ret;
1725-
}
1726-
1727-
template <size_t _SizeInWords>
1728-
inline
1729-
int __cdecl _vsnwprintf_s(char (&_Dst)[_SizeInWords], size_t _Count, const char *_Format, va_list _ArgList)
1730-
{
1731-
return _vsnwprintf_s(_Dst, _SizeInWords, _Count, _Format, _ArgList);
1732-
}
1733-
#endif
1734-
1735-
/* no inline version of _snwprintf_s, _vsnwprintf_s */
1736-
17371709
/* wscanf_s */
17381710
_SAFECRT__EXTERN_C
17391711
int __cdecl wscanf_s(const WCHAR *_Format, ...);

src/coreclr/pal/src/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ set(SOURCES
192192
safecrt/strncat_s.cpp
193193
safecrt/strncpy_s.cpp
194194
safecrt/strtok_s.cpp
195-
safecrt/swprintf.cpp
196195
safecrt/vsprintf.cpp
197-
safecrt/vswprint.cpp
198196
safecrt/wcscat_s.cpp
199197
safecrt/wcscpy_s.cpp
200198
safecrt/wcslen_s.cpp

src/coreclr/pal/src/misc/fmtmessage.cpp

Lines changed: 20 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -180,102 +180,6 @@ of _ADD_TO_STRING, as we will resize the buffer if necessary. */
180180
_ADD_TO_STRING( c );\
181181
}
182182

183-
184-
/*++
185-
Function :
186-
187-
FMTMSG_ProcessPrintf
188-
189-
Processes the printf formatters based on the format.
190-
191-
Returns the LPWSTR string, or NULL on failure.
192-
*/
193-
194-
static LPWSTR FMTMSG_ProcessPrintf( WCHAR c ,
195-
LPWSTR lpPrintfString,
196-
LPWSTR lpInsertString)
197-
{
198-
LPWSTR lpBuffer = NULL;
199-
LPWSTR lpBuffer2 = NULL;
200-
LPWSTR lpFormat = NULL;
201-
#if _DEBUG
202-
// small size for _DEBUG to exercise buffer reallocation logic
203-
int tmpSize = 4;
204-
#else
205-
int tmpSize = 64;
206-
#endif
207-
UINT nFormatLength = 0;
208-
int nBufferLength = 0;
209-
210-
TRACE( "FMTMSG_ProcessPrintf( %C, %S, %p )\n", c,
211-
lpPrintfString, lpInsertString );
212-
213-
switch ( c )
214-
{
215-
case 'e' :
216-
/* Fall through */
217-
case 'E' :
218-
/* Fall through */
219-
case 'f' :
220-
/* Fall through */
221-
case 'g' :
222-
/* Fall through */
223-
case 'G' :
224-
ERROR( "%%%c is not supported by FormatMessage.\n", c );
225-
SetLastError( ERROR_INVALID_PARAMETER );
226-
return NULL;
227-
}
228-
229-
nFormatLength = PAL_wcslen( lpPrintfString ) + 2; /* Need to count % AND NULL */
230-
lpFormat = (LPWSTR)PAL_malloc( nFormatLength * sizeof( WCHAR ) );
231-
if ( !lpFormat )
232-
{
233-
ERROR( "Unable to allocate memory.\n" );
234-
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
235-
return NULL;
236-
}
237-
/* Create the format string. */
238-
memset( lpFormat, 0, nFormatLength * sizeof(WCHAR) );
239-
*lpFormat = '%';
240-
241-
PAL_wcscat( lpFormat, lpPrintfString );
242-
243-
lpBuffer = (LPWSTR) PAL_malloc(tmpSize*sizeof(WCHAR));
244-
245-
/* try until the buffer is big enough */
246-
while (TRUE)
247-
{
248-
if (!lpBuffer)
249-
{
250-
ERROR("Unable to allocate memory\n");
251-
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
252-
PAL_free(lpFormat);
253-
return NULL;
254-
}
255-
nBufferLength = _snwprintf_s( lpBuffer, tmpSize, tmpSize,
256-
lpFormat, lpInsertString);
257-
258-
if ((nBufferLength >= 0) && (nBufferLength != tmpSize))
259-
{
260-
break; /* succeeded */
261-
}
262-
else
263-
{
264-
tmpSize *= 2;
265-
lpBuffer2 = static_cast<WCHAR *>(
266-
PAL_realloc(lpBuffer, tmpSize*sizeof(WCHAR)));
267-
if (lpBuffer2 == NULL)
268-
PAL_free(lpBuffer);
269-
lpBuffer = lpBuffer2;
270-
}
271-
}
272-
273-
PAL_free( lpFormat );
274-
lpFormat = NULL;
275-
276-
return lpBuffer;
277-
}
278-
279183
/*++
280184
Function:
281185
FormatMessageW
@@ -481,113 +385,33 @@ FormatMessageW(
481385
}
482386
if ( *lpSourceString == '!' )
483387
{
484-
LPWSTR lpInsertString = NULL;
485-
LPWSTR lpPrintfString = NULL;
486-
LPWSTR lpStartOfFormattedString = NULL;
487-
UINT nPrintfLength = 0;
488-
LPWSTR lpFormattedString = NULL;
489-
UINT nFormattedLength = 0;
490-
491-
if ( !bIsVaList )
492-
{
493-
lpInsertString = ((LPWSTR*)Arguments)[ Index - 1 ];
494-
}
495-
else
496-
{
497-
va_list TheArgs;
498-
499-
va_copy(TheArgs, *Arguments);
500-
UINT i = 0;
501-
for ( ; i < Index; i++ )
502-
{
503-
lpInsertString = va_arg( TheArgs, LPWSTR );
504-
}
505-
}
506-
507-
/* Calculate the length, and extract the printf string.*/
508-
lpSourceString++;
509-
{
510-
LPWSTR p = PAL_wcschr( lpSourceString, '!' );
511-
512-
if ( NULL == p )
513-
{
514-
nPrintfLength = 0;
515-
}
516-
else
517-
{
518-
nPrintfLength = p - lpSourceString;
519-
}
520-
}
521-
522-
lpPrintfString =
523-
(LPWSTR)PAL_malloc( ( nPrintfLength + 1 ) * sizeof( WCHAR ) );
524-
525-
if ( !lpPrintfString )
526-
{
527-
ERROR( "Unable to allocate memory.\n" );
528-
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
529-
lpWorkingString = NULL;
530-
nCount = 0;
531-
goto exit;
532-
}
533-
534-
PAL_wcsncpy( lpPrintfString, lpSourceString, nPrintfLength );
535-
*( lpPrintfString + nPrintfLength ) = '\0';
536-
537-
lpStartOfFormattedString = lpFormattedString =
538-
FMTMSG_ProcessPrintf( *lpPrintfString,
539-
lpPrintfString,
540-
lpInsertString);
541-
542-
if ( !lpFormattedString )
543-
{
544-
ERROR( "Unable to process the format string.\n" );
545-
/* Function will set the error code. */
546-
PAL_free( lpPrintfString );
547-
lpWorkingString = NULL;
548-
goto exit;
549-
}
550-
551-
552-
nFormattedLength = PAL_wcslen( lpFormattedString );
553-
554-
/* Append the processed printf string into the working string */
555-
while ( *lpFormattedString )
556-
{
557-
_CHECKED_ADD_TO_STRING( *lpFormattedString );
558-
lpFormattedString++;
559-
}
388+
ERROR( "Embedded printf formatting ('!<printf format>!') is unsupported\n" );
389+
SetLastError( ERROR_INVALID_PARAMETER );
390+
lpWorkingString = NULL;
391+
nCount = 0;
392+
goto exit;
393+
}
560394

561-
lpSourceString += nPrintfLength + 1;
562-
PAL_free( lpPrintfString );
563-
PAL_free( lpStartOfFormattedString );
564-
lpPrintfString = lpFormattedString = NULL;
395+
LPWSTR lpInsert = NULL;
396+
if ( !bIsVaList )
397+
{
398+
lpInsert = ((LPWSTR*)Arguments)[Index - 1];
565399
}
566400
else
567401
{
568-
/* The printf format string defaults to 's'.*/
569-
LPWSTR lpInsert = NULL;
570-
571-
if ( !bIsVaList )
572-
{
573-
lpInsert = ((LPWSTR*)Arguments)[Index - 1];
574-
}
575-
else
402+
va_list TheArgs;
403+
va_copy(TheArgs, *Arguments);
404+
UINT i = 0;
405+
for ( ; i < Index; i++ )
576406
{
577-
va_list TheArgs;
578-
va_copy(TheArgs, *Arguments);
579-
UINT i = 0;
580-
for ( ; i < Index; i++ )
581-
{
582-
lpInsert = va_arg( TheArgs, LPWSTR );
583-
}
407+
lpInsert = va_arg( TheArgs, LPWSTR );
584408
}
409+
}
585410

586-
while ( *lpInsert )
587-
{
588-
_CHECKED_ADD_TO_STRING( *lpInsert );
589-
lpInsert++;
590-
}
411+
while ( *lpInsert )
412+
{
413+
_CHECKED_ADD_TO_STRING( *lpInsert );
414+
lpInsert++;
591415
}
592416
}
593417
/* Format specifiers. */

src/coreclr/pal/src/safecrt/internal.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,21 +279,15 @@ int __cdecl _output(__inout FILE * _File, _In_z_ __format_string const char *_Fo
279279
int __cdecl _woutput(__inout FILE * _File, _In_z_ __format_string const char16_t *_Format, va_list _ArgList);
280280
int __cdecl _output_s(__inout FILE * _File, _In_z_ __format_string const char *_Format, va_list _ArgList);
281281
int __cdecl _output_p(__inout FILE * _File, _In_z_ __format_string const char *_Format, va_list _ArgList);
282-
int __cdecl _woutput_s(__inout FILE * _File, _In_z_ __format_string const char16_t *_Format, va_list _ArgList);
283-
int __cdecl _woutput_p(__inout FILE * _File, _In_z_ __format_string const char16_t *_Format, va_list _ArgList);
284282
typedef int (*OUTPUTFN)(FILE *, const char *, va_list);
285-
typedef int (*WOUTPUTFN)(FILE *, const char16_t *, va_list);
286283

287284
#else /* _SAFECRT_IMPL */
288285

289286
int __cdecl _output_l(__inout FILE * _File, _In_z_ __format_string const char *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList);
290287
int __cdecl _woutput_l(__inout FILE * _File, _In_z_ __format_string const char16_t *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList);
291288
int __cdecl _output_s_l(__inout FILE * _File, _In_z_ __format_string const char *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList);
292289
int __cdecl _output_p_l(__inout FILE * _File, _In_z_ __format_string const char *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList);
293-
int __cdecl _woutput_s_l(__inout FILE * _File, _In_z_ __format_string const char16_t *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList);
294-
int __cdecl _woutput_p_l(__inout FILE * _File, _In_z_ __format_string const char16_t *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList);
295290
typedef int (*OUTPUTFN)(__inout FILE * _File, const char *, _locale_t, va_list);
296-
typedef int (*WOUTPUTFN)(__inout FILE * _File, const char16_t *, _locale_t, va_list);
297291

298292
#endif /* _SAFECRT_IMPL */
299293

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

src/coreclr/pal/src/safecrt/mbusafecrt_internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ void _safecrt_wfassign(int flag, void* argument, char16_t * number );
8888
int _minimal_chartowchar( char16_t* outWChar, const char* inChar );
8989

9090
int _output_s( miniFILE* outfile, const char* _Format, va_list _ArgList);
91-
int _woutput_s( miniFILE* outfile, const char16_t* _Format, va_list _ArgList);
9291
int _output( miniFILE *outfile, const char* _Format, va_list _ArgList);
9392

9493
int __tinput_s( miniFILE* inFile, const unsigned char * inFormat, va_list inArgList );

0 commit comments

Comments
 (0)