Skip to content

Commit 7c258df

Browse files
authored
Merge pull request #2323 from corob-msft/cr-1558
Fix code sample for vsprintf_s per 1558
2 parents 4264944 + afdb145 commit 7c258df

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

docs/c-runtime-library/reference/vsprintf-s-vsprintf-s-l-vswprintf-s-vswprintf-s-l.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "vsprintf_s, _vsprintf_s_l, vswprintf_s, _vswprintf_s_l"
3-
ms.date: "03/26/2019"
3+
ms.date: "09/12/2019"
44
apiname: ["_vswprintf_s_l", "vsprintf_s", "vswprintf_s", "_vsprintf_s_l"]
55
apilocation: ["msvcrt.dll", "msvcr80.dll", "msvcr90.dll", "msvcr100.dll", "msvcr100_clr0400.dll", "msvcr110.dll", "msvcr110_clr0400.dll", "msvcr120.dll", "msvcr120_clr0400.dll", "ucrtbase.dll", "ntoskrnl.exe"]
66
apitype: "DLLExport"
@@ -10,7 +10,7 @@ ms.assetid: 60e90518-57f0-4f1b-b732-f62a69702833
1010
---
1111
# vsprintf_s, _vsprintf_s_l, vswprintf_s, _vswprintf_s_l
1212

13-
Write formatted output using a pointer to a list of arguments. These are versions of [vsprintf, _vsprintf_l, vswprintf, _vswprintf_l, \__vswprintf_l](vsprintf-vsprintf-l-vswprintf-vswprintf-l-vswprintf-l.md) with security enhancements as described in [Security Features in the CRT](../../c-runtime-library/security-features-in-the-crt.md).
13+
Write formatted output using a pointer to a list of arguments. These functions are versions of [vsprintf, _vsprintf_l, vswprintf, _vswprintf_l, \__vswprintf_l](vsprintf-vsprintf-l-vswprintf-vswprintf-l-vswprintf-l.md) with security enhancements as described in [Security Features in the CRT](../../c-runtime-library/security-features-in-the-crt.md).
1414

1515
## Syntax
1616

@@ -88,7 +88,7 @@ These functions differ from the non-secure versions only in that the secure vers
8888

8989
The versions of these functions with the **_l** suffix are identical except that they use the locale parameter passed in instead of the current thread locale.
9090

91-
In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. For more information, see [Secure Template Overloads](../../c-runtime-library/secure-template-overloads.md).
91+
In C++, using these functions is simplified by template overloads. The overloads can infer buffer length automatically, eliminating the need to specify a size argument. And, they can automatically replace non-secure functions with their secure counterparts. For more information, see [Secure Template Overloads](../../c-runtime-library/secure-template-overloads.md).
9292

9393
### Generic-Text Routine Mappings
9494

@@ -112,13 +112,15 @@ For additional compatibility information, see [Compatibility](../../c-runtime-li
112112

113113
```C
114114
// crt_vsprintf_s.c
115+
// Compile with: cl /W4 crt_vsprintf_s.c
115116
// This program uses vsprintf_s to write to a buffer.
116117
// The size of the buffer is determined by _vscprintf.
117118

118119
#include <stdlib.h>
120+
#include <stdio.h>
119121
#include <stdarg.h>
120122

121-
void test( char * format, ... )
123+
void test( char const * const format, ... )
122124
{
123125
va_list args;
124126
int len;
@@ -127,10 +129,13 @@ void test( char * format, ... )
127129
va_start( args, format );
128130
len = _vscprintf( format, args ) // _vscprintf doesn't count
129131
+ 1; // terminating '\0'
130-
buffer = malloc( len * sizeof(char) );
131-
vsprintf_s( buffer, len, format, args );
132-
puts( buffer );
133-
free( buffer );
132+
buffer = (char *) malloc( len * sizeof(char) );
133+
if ( NULL != buffer )
134+
{
135+
vsprintf_s( buffer, len, format, args );
136+
puts( buffer );
137+
free( buffer );
138+
}
134139
va_end( args );
135140
}
136141

@@ -154,4 +159,4 @@ This is a string
154159
[fprintf, _fprintf_l, fwprintf, _fwprintf_l](fprintf-fprintf-l-fwprintf-fwprintf-l.md)<br/>
155160
[printf, _printf_l, wprintf, _wprintf_l](printf-printf-l-wprintf-wprintf-l.md)<br/>
156161
[sprintf, _sprintf_l, swprintf, _swprintf_l, \__swprintf_l](sprintf-sprintf-l-swprintf-swprintf-l-swprintf-l.md)<br/>
157-
[va_arg, va_copy, va_end, va_start](va-arg-va-copy-va-end-va-start.md)<br/>
162+
[va_arg, va_copy, va_end, va_start](va-arg-va-copy-va-end-va-start.md)

0 commit comments

Comments
 (0)