snprint理由? [英] snprint rationale?

查看:64
本文介绍了snprint理由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

snprintf以返回字符数

(不包括尾随的''\0'')的理由是什么,这些字符将被写入最终的

string如果有足够的空间可用?


这只是每次都打结我的面条!什么是正确的方式

来测试溢出的返回值?


负责此事的人的姓名和地址是什么?


Mike

What is the rationale for snprintf to "return the number of characters
(excluding the trailing ''\0'') which would have been written to the final
string if enough space had been available"?

This just twists my noodle in a knot every time! What is the proper way
to test the return value for overflow?

What is the name and address of the person responsible for this?

Mike

推荐答案

Michael B Allen< mb ***** @ ioplex.com>写道:
Michael B Allen <mb*****@ioplex.com> writes:
snprintf的原因是什么?返回字符数
(不包括尾随的''\ 0''),这些字符将被写入最终的
字符串,如果有足够的空间可用?

这只是每次都打结我的面条!测试溢出返回值的正确方法是什么?
What is the rationale for snprintf to "return the number of characters
(excluding the trailing ''\0'') which would have been written to the final
string if enough space had been available"?

This just twists my noodle in a knot every time! What is the proper way
to test the return value for overflow?




snprintf()的声明是:


int snprintf(char * restrict s,size_t n,

const char * restrict format,...);


可用字符数作为第二个参数传入。

要测试溢出,检查结果是否超过n。


如果用n ==调用snprintf() 0;它不会写任何字符,但

它将返回已经写入的字符数。

然后你可以分配适当的空格并调用snprintf( )再次

使用相同的参数,但是非零n。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



The declaration of snprintf() is:

int snprintf(char * restrict s, size_t n,
const char * restrict format, ...);

The number of characters available is passed in as the second argument.
To test for overflow, check whether the result exceeds n.

If you call snprintf() with n==0; it won''t write any characters, but
it will return the number of characters that would have been written.
You can then allocate the appropriate space and call snprintf() again
with the same arguments, but with a non-zero n.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


在文章< pa *********************** *********@ioplex.com>

Michael B Allen< mb ***** @ ioplex.com>写道:
In article <pa********************************@ioplex.com>
Michael B Allen <mb*****@ioplex.com> wrote:
snprintf的理由是什么?返回字符数
(不包括尾随的''\ 0''),这些字符将写入最终的
字符串,如果有足够的空间可用?


这可以让你分配一个足够大的缓冲区,而不需要

做多次传递:


needed = snprintf(NULL,0,fmt,arg1,arg2);

如果(需要< 0)...处理错误...

mem = malloc(需要+ 1);

if(mem == NULL)...处理错误...

result = snprintf(mem,需要+ 1,fmt,arg1, arg2);


它与fprintf()一致,它返回打印的

字符数。

这只是每次都把我的面条拧成一个结!测试溢出返回值的正确方法是什么?


给定一个缓冲区buf尺寸尺寸:


result = snprintf(buf,size,fmt,arg);


if(result> = 0&& result< size)

all_is_well();

else

needed_more_space();

负责此事的人的姓名和地址是什么?
What is the rationale for snprintf to "return the number of characters
(excluding the trailing ''\0'') which would have been written to the final
string if enough space had been available"?
This lets you allocate a buffer that is big enough, without having
to do many passes:

needed = snprintf(NULL, 0, fmt, arg1, arg2);
if (needed < 0) ... handle error ...
mem = malloc(needed + 1);
if (mem == NULL) ... handle error ...
result = snprintf(mem, needed + 1, fmt, arg1, arg2);

It is also consistent with fprintf(), which returns the number of
characters printed.
This just twists my noodle in a knot every time! What is the proper way
to test the return value for overflow?
Given a buffer "buf" of size "size":

result = snprintf(buf, size, fmt, arg);

if (result >= 0 && result < size)
all_is_well();
else
needed_more_space();
What is the name and address of the person responsible for this?




那就是我。 :-)

-

现实生活:风河系统Chris Torek

美国犹他州盐湖城(40 °39.22''N,111°50.29''W)+1 801 277 2603

电子邮件:忘了它 http://web.torek.net/torek/index.html

阅读电子邮件就像在垃圾中搜索食物一样,感谢垃圾邮件发送者。



That would be me. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


Michael B Allen写道:
Michael B Allen wrote:

snprintf返回的数量是什么理由字符
(不包括尾随的''\ 0'')如果有足够的空间可以写入最终的
字符串?

这只是扭曲我的每次都要结面!测试溢出返回值的正确方法是什么?

What is the rationale for snprintf to "return the number of characters
(excluding the trailing ''\0'') which would have been written to the final
string if enough space had been available"?

This just twists my noodle in a knot every time! What is the proper way
to test the return value for overflow?




Snprintf保证不会溢出。


这个有用:


if(snprintf(buf,buflen," ...",....)< buflen)

puts(没有发生溢出);?

else

puts(溢出可能已经发生);


这也有效:


buflen = snprintf(NULL,0," ...",....);

buf = malloc (buflen + 1);

snprintf(buf,buflen," ...",....);


Erik

-

+ ---------------------------------- ------------------------- +

Erik de Castro Lopo 没有**** @ mega-nerd.com (是的,这是有效的)

+ ---------- ------------------------------------------------- +

在comp.lang.python上看到:

问:如果某人在python中有代码用于缓冲区溢出,

请发布。

答:Python不支持缓冲区溢出,抱歉。



Snprintf is guaranteed not to overflow.

This works:

if (snprintf (buf, buflen, "...", ....) < buflen)
puts ("No overflow occurred");
else
puts ("Overflow might have occurred");

This also works:

buflen = snprintf (NULL, 0, "...", ....);
buf = malloc (buflen + 1) ;
snprintf (buf, buflen, "...", ....);

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it''s valid)
+-----------------------------------------------------------+
Seen on comp.lang.python:
Q : If someone has the code in python for a buffer overflow,
please post it.
A : Python does not support buffer overflows, sorry.


这篇关于snprint理由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆