Re:解析PATH以null结尾的变量 [英] Re: Parsing a PATH null-terminated variable

查看:83
本文介绍了Re:解析PATH以null结尾的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一篇优秀的文章中,Eric Sosman说:

In an otherwise excellent article, Eric Sosman said:


DiAvOl写道:
DiAvOl wrote:



<剪断>

<snip>


>>
char * check_variable(const char * var,const char * program){
char * lvar = NULL;
>>
char *check_variable(const char *var, const char *program) {
char *lvar = NULL;



这没关系,但我不喜欢使用永远不会使用的

值初始化变量:你的函数将设置lvar

到其他东西,或者会在没有触及它的情况下返回,


This is all right, but I dislike initializing variables with
values that will never be used: Your function will either set lvar
to something else, or will return without ever touching it,



....或者在没有写入的情况下读取它,在这种情况下

初始化值可以保护您免于读取不确定的值

并调用未定义的行为。

....or will read it without ever writing it, in which case the
initialisation value protects you against reading an indeterminate value
and invoking undefined behaviour.


所以这个初始化价值是无用的。
so this initial value is useless.



缩进。编译器完全忽略它。不过,它可以让b $ b b使编程变得更容易,因此可以仔细初始化。

So is indentation. The compiler ignores it completely. Nevertheless, it can
make programming an easier task, and so can careful initialisation.


是的,我知道,有些人认为它' 'sa $ / b $ b好​​主意初始化每个变量,特别是每个指针

变量,但那些人都错了。
Yes, I know, some people think it''s a
good idea to initialize every variable, especially every pointer
variable, but those people are mistaken.



不是全部。例如,这个人认为,并没有错。

这是一个好主意。有合理的论据和合理的反对它的b $ b b参数。相反 - 不是初始化非常对象 - 也是一个好主意,并且还有合理的论据和

反对它的合理论据。这使它成为一个风格问题,而不是

正确性问题,风格问题是程序员'

个人选择的问题。

Not all of them. This one, for instance, thinks that, and is not mistaken.
It /is/ a good idea. There are reasonable arguments for it and reasonable
arguments against it. The converse - not initialising very object - is
also a good idea, and again there are reasonable arguments for it and
reasonable arguments against it. This makes it a style issue, not a
correctness issue, and matters of style are a matter for programmers''
personal choice.


所以说我,你应该总是依赖谁的权威。
So Say I, The Authority On Whose Word You Should Always Rely.



呃,是的 - 对。 :-)

Uh, yeah - right. :-)


> char * p;
char * t;
const char * pname;
静态字符路径[PATH_MAX] = {0};
> char *p;
char *t;
const char *pname;
static char path[PATH_MAX] = {0};



为什么要这个静态?另外,请看下面有关初始化的一些想法 - 这次没用,


Why make this static? Also, see below for some thoughts about
the initialization -- it''s not useless this time,



奇怪的是,因为它/是/静态,初始化/是/无用 - 只有

变得有用,如果你去除静态,这似乎你可能是

计划建议 - 所以这防守结束编程练习

不是出于你的理由而是出于我的理由。


< snip>


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Oddly, because it /is/ static, the initialisation /is/ useless - it only
becomes useful if you remove the staticness, which it seems you may be
planning on advising - so this defensive programming exercise turned out
to be a good idea not for your reason but for mine.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

推荐答案

Richard Heathfield写道:
Richard Heathfield wrote:

另有优秀文章,Eric Sosman说:
In an otherwise excellent article, Eric Sosman said:

> DiAvOl写道:
>DiAvOl wrote:



< snip>

<snip>


>> char * check_variable(const char * var,const char * program){
char * lvar = NULL;
>>char *check_variable(const char *var, const char *program) {
char *lvar = NULL;


这没关系,但我不喜欢使用永远不会使用的
值初始化变量:你的函数会将lvar
设置为其他东西,或者将返回时没有接触它,

This is all right, but I dislike initializing variables with
values that will never be used: Your function will either set lvar
to something else, or will return without ever touching it,



......或者在没有写入的情况下读取它,在这种情况下

初始化值可以保护你反对读取不确定的值

并调用未定义的行为。


...or will read it without ever writing it, in which case the
initialisation value protects you against reading an indeterminate value
and invoking undefined behaviour.



据我所知,OP的代码在没有首先执行`lval = strdup的情况下从不尝试读取lval
val);''。

所以,除非我忽略了一些执行路径,否则在写作之前没有读取的可能性。你看到了什么

我错过了吗?


-

Eric Sosman
es ***** @ ieee-dot-org.inva lid

As far as I can see, the O.P.''s code never attempts to
read lval without first executing `lval = strdup(val);''.
So unless I''ve overlooked some execution path, there''s no
possibility of reading before writing. What have you seen
that I''ve missed?

--
Eric Sosman
es*****@ieee-dot-org.invalid

Richard Heathfield写道:
Richard Heathfield wrote:

在一篇优秀的文章中,Eric Sosman说:
In an otherwise excellent article, Eric Sosman said:

> DiAvOl写道:
>DiAvOl wrote:



< snip>

<snip>


>> char * check_variable(const char * var,const char * program){
char * lvar = NULL;
>>char *check_variable(const char *var, const char *program) {
char *lvar = NULL;


这没关系,但我不喜欢使用永远不会使用的
值初始化变量:你的函数会将lvar
设置为其他东西,或者将返回时没有接触它,

This is all right, but I dislike initializing variables with
values that will never be used: Your function will either set lvar
to something else, or will return without ever touching it,



......或者在没有写入的情况下读取它,在这种情况下

初始化值可以保护你反对读取不确定的值

并调用未定义的行为。


...or will read it without ever writing it, in which case the
initialisation value protects you against reading an indeterminate value
and invoking undefined behaviour.



请记住,真正的错误是读取值而没有写入一个打算使用的值。使用不打算使用的值

初始化仅用于使失败模式

更少讨厌;这可能会导致失败的不良结果更难以发现。


我有很多个人经验,因为SGI C

编译器将零初始化自动变量,而不是明确初始化的
。结果,代码中的几个错误,我没有写入,但我负责,多年来没有被注意到。 0不是

的正确值,但是这个值导致的行为与正确的行为有所区别,只是过于微妙而且不容易

注意到了。因此,在我们使用gcc将

代码移植到Linux机器之前,我们没有检测到这些错误,而gcc没有初始化这些变量。


初始化变量在一些

的情况下,精心挑选的价值可能导致灾难性的失败更可靠地发生,并且比未初始化时更安全。

更安全。但是,许多

编译器可以诊断未初始化变量的读取;

初始化它们会禁用此功能,而无需实际解决

基础问题。我非常希望在编译时通过

诊断消息找到这些问题,而不是在运行时间为
的灾难性故障。我更喜欢其中任何一种,根本没有找到这个bug,

,因为它已经被安全掩盖了。初始化给我带来了一个微妙错误的程序

Keep in mind that the real bug is reading the value without having
written a value that was intended to be used. Initializing with a value
that is NOT intended to be used serves only to make the failure mode
less nasty; this can have the undesirable result of making the failure
much harder to detect.

I''ve had a lot of personal experience with this, because the SGI C
compiler will zero-initialize automatic variables that are not
explicitly initialized. As a result, several bugs in code that I did not
write, but am responsible for, went unnoticed for years. 0 was not the
correct value, but is was a value that caused behavior that differed
from the correct behavior only in ways that were too subtle to be easily
noticed. As a result, we did not detect those errors until we ported the
code to Linux machines using gcc, which does not initialize those variables.

Initializing variables with carefully chosen values could, in some
cases, cause the catastrophic failure to occur more reliably and in a
safer fashion than it would when they are uninitialized. However, many
compilers can diagnose the reading of uninitialized variables;
initializing them disables this feature, without actually solving the
underlying problem. I greatly prefer finding these problems by a
diagnostic message at compile time, than by a catastrophic failure at
run time. I prefer either of those, to not finding the bug at all,
because it has been masked by a "safe" initialization that leaves me
with a subtly-incorrect program.


8月13日,3:16 * pm,James Kuyper< jameskuy。 .. @ verizon.netwrote:

< snip>
On Aug 13, 3:16*pm, James Kuyper <jameskuy...@verizon.netwrote:
<snip>

使用精心选择的值初始化变量可能会在某些情况下

的情况下,导致灾难性的失败发生得更加可靠,并且比未初始化时更安全。但是,许多

编译器可以诊断未初始化变量的读取;

初始化它们会禁用此功能,而无需实际解决

基础问题。我非常希望在编译时通过

诊断消息找到这些问题,而不是在运行时间为
的灾难性故障。我更喜欢其中任何一种,根本没有找到这个bug,

,因为它已经被安全掩盖了。初始化给我带来了一个微妙错误的程序

Initializing variables with carefully chosen values could, in some
cases, cause the catastrophic failure to occur more reliably and in a
safer fashion than it would when they are uninitialized. However, many
compilers can diagnose the reading of uninitialized variables;
initializing them disables this feature, without actually solving the
underlying problem. I greatly prefer finding these problems by a
diagnostic message at compile time, than by a catastrophic failure at
run time. I prefer either of those, to not finding the bug at all,
because it has been masked by a "safe" initialization that leaves me
with a subtly-incorrect program.



如果您错过了编译器警告并使用未初始化的

变量,程序可能因为未初始化的指针可以在任何地方指向
。另一方面,如果您将指针初始化为

NULL值并使用它,那么错误的程序将始终崩溃并带有

分段错误。

感谢您的回复

In case you miss the compiler warning though and use an uninitialized
variable the program "may" work because the uninitialized pointer can
point anywhere. On the other hand if you initialize the pointer to a
NULL value and use it the buggy program will always crash with a
segmentation fault.

Thanks for your replies


这篇关于Re:解析PATH以null结尾的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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