problem with sprintf_s() on win 7
-
char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP
-
char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP
Second parameter of sprintf_s is the size of destination buffer. If length of formatted string is greater than the size of source buffer, sprintf_s will create debug assertion. If length of string in Buffer is less than 3, then it will not create an debug assertion. Here you can change the second parameter of sprintf_s to 47,by considering offset in buffer.
-
char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP
-
Second parameter of sprintf_s is the size of destination buffer. If length of formatted string is greater than the size of source buffer, sprintf_s will create debug assertion. If length of string in Buffer is less than 3, then it will not create an debug assertion. Here you can change the second parameter of sprintf_s to 47,by considering offset in buffer.
char buffer[50] int offset =3; char Buffer[250]; OR char buffer[50] int offset =3; char Buffer[50]; Is working fine with VS6.0, but when the same code is compiled with VS2008 I am still getting the same error as mentioned above.
-
char buffer[50] int offset =3; char Buffer[250]; OR char buffer[50] int offset =3; char Buffer[50]; Is working fine with VS6.0, but when the same code is compiled with VS2008 I am still getting the same error as mentioned above.
-
char buffer[50] int offset =3; char Buffer[250]; OR char buffer[50] int offset =3; char Buffer[50]; Is working fine with VS6.0, but when the same code is compiled with VS2008 I am still getting the same error as mentioned above.
Which function is used in VC6.0 ? Is it sprintf() or sprintf_s() ?
If it is sprintf(), then nothing to wonder, becausee sprintfdoesnot check the output buffer size. -
char buffer[50] int offset =3; char Buffer[250]; OR char buffer[50] int offset =3; char Buffer[50]; Is working fine with VS6.0, but when the same code is compiled with VS2008 I am still getting the same error as mentioned above.
which function is used in VC6.0 ? Is it sprintf_s() ?
sprintf() is not checking the output buffer size. Sprintf_s ensures the output buffer overwritinng is not happening. -
char buffer[50] int offset =3; char Buffer[250]; OR char buffer[50] int offset =3; char Buffer[50]; Is working fine with VS6.0, but when the same code is compiled with VS2008 I am still getting the same error as mentioned above.
Short answer: You
sprintf
statement is plain wrong if 'it is working fine' maybe by accident. Somewhat longer answer: If you have a50
bytes buffer, why don't you pass50
instead of3
? Why are you using the misleading nameoffset
for passing a size?Veni, vidi, vici.
-
char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP
First thing is that, trust the implementation made by Microsoft. This is a very primitive function and you will have to read the documentation first. Second you can your strcpy_s or strcat_s (if you're concatenating). sprint_s is not really a choice string copy. Third your strings must be null terminated. To understand this better, I am illustrating this with a code example
char buffer\[12\]= {0}; int offset =3; char Buffer\[50\] = "Hello World"; sprintf\_s(buffer,sizeof(buffer), "%s", Buffer);
in the above example buffer has 12 character in legnth and Hello World String contains 11 characters + null terminated. Anything less than 12 character size will raise assertion buffer too small. for sprintf_s only the number of characters being copied only matters. not really the original size of the buffers.
-Sarath.
My blog - iSpeak code
Rate the answers and close your posts if it's answered
-
char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP
I have fixed the issue, I am very much thankful to all your replies