通常会导致字符串没有空终止的原因 [英] What could normally causes the string to have no null terminated

查看:130
本文介绍了通常会导致字符串没有空终止的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



鉴于字符串是空终止类型。什么可能是

可能的原因(根据经验)字符串没有空终止

并导致缓冲区溢出。我知道这是相当广泛的,就像

尽可能找出原因,以便我可以对我的代码进行更严格的

检查。


注意:我无法使用std :: string因为它需要总重写

重写。


谢谢。


Given that the string is of null terminated type. What could be the
possible causes (by experience) the string to have no null terminated
and cause buffer overflow later. I know it is quite broad, just like to
find out the causes as much as possible so that I could impose stricter
checking toward my codes.

note: I could not use std::string cause it will require a total
rewrite.

thanks.

推荐答案



semutaécrit:

semut a écrit :

鉴于字符串是null终止类型。什么可能是

可能的原因(根据经验)字符串没有空终止

并导致缓冲区溢出。我知道这是相当广泛的,就像

尽可能找出原因,以便我可以对我的代码进行更严格的

检查。


注意:我不能使用std :: string因为它需要总重写

重写。


谢谢。
Given that the string is of null terminated type. What could be the
possible causes (by experience) the string to have no null terminated
and cause buffer overflow later. I know it is quite broad, just like to
find out the causes as much as possible so that I could impose stricter
checking toward my codes.

note: I could not use std::string cause it will require a total
rewrite.

thanks.



我只能考虑字符串结构,但没有更多关于你如何构建它的信息
我不确定我将能够获得更多的b $ b帮助。请记住,您应该自己添加这个nul(不是null $ / b $ b BTW)字符,或者使用自动添加的函数

重新复制输入缓冲区时(如果它存在)。 snprintf,

strncpy,strncat和其他相关函数如果n参数太小,可能无法复制字符串(包括nul字符)的结尾。


还记得使用std :: string可能需要你重写

所有东西,但是当你遇到这样的时候也会节省你的时间

讨厌的错误。总而言之,它最终仍然可能是一场胜利:)


问候,


- Emmanuel Deloget,Artware


" semut" < an ***** @ gmail.comwrites:
"semut" <an*****@gmail.comwrites:

鉴于该字符串是空终止类型。什么可能是

可能的原因(根据经验)字符串没有空终止

并导致缓冲区溢出。我知道它是相当广泛的,就像

尽可能找出原因,以便我可以对我的代码进行更严格的

检查。
Given that the string is of null terminated type. What could be the
possible causes (by experience) the string to have no null terminated
and cause buffer overflow later. I know it is quite broad, just like to
find out the causes as much as possible so that I could impose stricter
checking toward my codes.



我想这个问题在comp.lang.c会被更好地问为人们

习惯了这样的字符串而(就像我一样)推测)在comp.lang.c ++

人们更喜欢std :: string。


关于你的问题可能使用gets(),strcpy(),sprintf ()而不是
的fgets(),strncpy(),snprintf()等。还要注意

strncpy(),因为它并不是在所有情况下写入null终结者。


-

祝你好运,_ _

.o。 | Serenly Enlightened Majesty of o'',=。/`o

..o |计算机科学,Michalmina86 Nazarewicz(oo)

ooo + - < mina86 * tlen.pl> ---< jid:mina86 * chrome.pl> - ooO - (_) - Ooo--

I guess that question would be better asked at comp.lang.c as people
there are used to such strings whereas (as I presume) at comp.lang.c++
people prefer std::string.

As to your question probably using gets(), strcpy(), sprintf() instead
of fgets(), strncpy(), snprintf() etc. Also pay attention to
strncpy() as it does not in all case write the null terminator.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o'' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--


12月4日下午2:27,semut < ant .... @ gmail.comwrote:
On Dec 4, 2:27 pm, "semut" <ant....@gmail.comwrote:

鉴于该字符串是空终止类型。什么可能是

可能的原因(根据经验)字符串没有空终止

并导致缓冲区溢出。我知道它是相当广泛的,就像

尽可能找出原因,以便我可以对我的代码进行更严格的

检查。
Given that the string is of null terminated type. What could be the
possible causes (by experience) the string to have no null terminated
and cause buffer overflow later. I know it is quite broad, just like to
find out the causes as much as possible so that I could impose stricter
checking toward my codes.



只是众多例子中的一个:

如果你创建一个大小的缓冲区(char *)并使用某个函数

将文本放在此缓冲区中,但在函数调用中指定缓冲区的大小时,您忘记考虑\0

。这将

导致函数将\0放在缓冲区之外(到目前为止可能没有

真正的伤害)。由于某种原因,缓冲区外的\0更改

(该内存空间由一个变量使用,你刚刚分配了一个

值。现在你不要在字符串的末尾有一个\0,并且

会给你很多时间来弄清楚出了什么问题。


- -

Erik Wikstr?m

Just one of many examples:
If you create a buffer (char*) of some size and use some function that
places text in this buffer, but you forget to take into account the \0
when specifying the size of the buffer in the function call. This will
cause the function to put the \0 outside the buffer (so far maybe no
real harm done). The for some reason the \0 outside the buffer changes
(that memory-space was used by a variable which you just assigned a
value to. Now you don''t have a \0 at the end of the string and that
will give you lots of hours trying to figure out what went wrong.

--
Erik Wikstr?m


这篇关于通常会导致字符串没有空终止的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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