vc 的 sprintf_s函数没有任何效果

for_onxx 2011-04-18 01:40:10
对应sprintf的安全版本有snprintf和_snprintf两个,听说VC下有个是sprintf_s,但是好像没有任何效果,如下面的代码是溢出错误的:
har buff[265];

int main()
{
char buff[4];
sprintf_s(buff,sizeof(buff),"hello world");//出错

return 0;
}
然而下面的代码则是正确的,但是编译是警告:snprintf声明为否决的:
int main()
{
char buff[4];
snprintf(buff,sizeof(buff),"hello world");//正常
}

到底是什么原因呢?
...全文
356 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnlm2 2011-04-18
  • 打赏
  • 举报
回复
你的代码一个问题是buff长度太小,二是你没打印buff自然看不到!
for_onxx 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 c_losed 的回复:]
引用 3 楼 for_onxx 的回复:

引用 1 楼 c_losed 的回复:
C/C++ code

#include <stdio.h>

int main()
{
char buff[40];
sprintf_s(buff,sizeof(buff),"hello world");//出错

printf(buff);
return 0;
}

大哥,我知道……

[/Quote]

对你无语
luciferisnotsatan 2011-04-18
  • 打赏
  • 举报
回复
sprintf_s的
The other main difference between sprintf_s and sprintf is that sprintf_s takes a length parameter specifying the size of the output buffer in characters. If the buffer is too small for the text being printed then the buffer is set to an empty string and the invalid parameter handler is invoked. Unlike snprintf, sprintf_s guarantees that the buffer will be null-terminated (unless the buffer size is zero).

snprintf的
If len < count, then len characters are stored in buffer, a null-terminator is appended, and len is returned.
If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned.
If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned.

delphiwcdj 2011-04-18
  • 打赏
  • 举报
回复
应该使用snprintf并根据返回判断是否溢出
delphiwcdj 2011-04-18
  • 打赏
  • 举报
回复
LZ的效果指的是什么?
参考:
It's by design. The entire point of sprintf_s, and other functions from the *_s family, is to catch buffer overrun errors and treat them as precondition violations. This means that they're not really meant to be recoverable. This is designed to catch errors only - you shouldn't ever call sprintf_s if you know the string can be too large for a destination buffer. In that case, use strlen first to check and decide whether you need to trim.
luciferisnotsatan 2011-04-18
  • 打赏
  • 举报
回复
vc2005里就是弄崩掉

if (retvalue == -2)
{
_VALIDATE_RETURN(("Buffer too small", 0), ERANGE, -1);
}
luciferisnotsatan 2011-04-18
  • 打赏
  • 举报
回复
int __cdecl _vsprintf_s_l (
char *string,
size_t sizeInBytes,
const char *format,
_locale_t plocinfo,
va_list ap
)
{
int retvalue = -1;

/* validation section */
_VALIDATE_RETURN(format != NULL, EINVAL, -1);
_VALIDATE_RETURN(string != NULL && sizeInBytes > 0, EINVAL, -1);

retvalue = _vsnprintf_helper(_output_s_l, string, sizeInBytes, format, plocinfo, ap);
if (retvalue < 0)
{
string[0] = 0;
_SECURECRT__FILL_STRING(string, sizeInBytes, 1);
}
if (retvalue == -2) [color=#008000]// 错误提示
{
_VALIDATE_RETURN(("Buffer too small", 0), ERANGE, -1);
}[/color]
if (retvalue >= 0)
{
_SECURECRT__FILL_STRING(string, sizeInBytes, retvalue + 1);
}

return retvalue;
}
delphiwcdj 2011-04-18
  • 打赏
  • 举报
回复
有提示错误呀
char buff[4];
sprintf_s(buff,sizeof(buff),"%s","hello world");
赵4老师 2011-04-18
  • 打赏
  • 举报
回复
printf("%s\n",buff);
或者
printf("%s",buff);fflush(stdout);

赵4老师 2011-04-18
  • 打赏
  • 举报
回复
printf("%s",buff);
c_losed 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 for_onxx 的回复:]

引用 1 楼 c_losed 的回复:
C/C++ code

#include <stdio.h>

int main()
{
char buff[40];
sprintf_s(buff,sizeof(buff),"hello world");//出错

printf(buff);
return 0;
}

大哥,我知道这样子正确,我想知道的是为什么sprintf_……
[/Quote]

数组太小
赵4老师 2011-04-18
  • 打赏
  • 举报
回复
试试sprintf_s(buff,sizeof(buff),"%s","hello world");

for_onxx 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 c_losed 的回复:]
报错是因为你的数组太小了 溢出
[/Quote]

你也是不看题目
for_onxx 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 c_losed 的回复:]
C/C++ code

#include <stdio.h>

int main()
{
char buff[40];
sprintf_s(buff,sizeof(buff),"hello world");//出错

printf(buff);
return 0;
}
[/Quote]
大哥,我知道这样子正确,我想知道的是为什么sprintf_s不起作用
c_losed 2011-04-18
  • 打赏
  • 举报
回复
报错是因为你的数组太小了 溢出
c_losed 2011-04-18
  • 打赏
  • 举报
回复

#include <stdio.h>

int main()
{
char buff[40];
sprintf_s(buff,sizeof(buff),"hello world");//出错

printf(buff);
return 0;
}

64,318

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧