为什么你不能检查errno是否等于ERANGE?

问题描述 投票:6回答:2

我一直在尝试使用strtol将char数组正确转换为long,检查是否存在溢出或下溢,然后在long上进行int转换。一路上,我注意到很多代码看起来像这样

if ((result == LONG_MAX || result == LONG_MIN) && errno == ERANGE)
{
   // Handle the error
}

为什么你不能只说

if(errno == ERANGE)
{
    // Handle the error
}

根据我的理解,如果发生下溢或溢出,则在两种情况下都将errno设置为ERANGE。前者真的有必要吗?可以单独检查ERANGE是否有问题?

这就是我的代码现在的样子

 char *endPtr;
 errno = 0;
 long result = strtol(str, &endPtr, 10);

 if(errno == ERANGE)
 {
     // Handle Error
 }
 else if(result > INT_MAX || result < INT_MIN)
 {
    // Handle Error
 }
 else if(endPtr == str || *endPtr != '\0')
 {
     // Handle Error
 }

 num = (int)result;
 return num;

如果有前者的原因请告诉我。

c errno strtol
2个回答
6
投票

第一个代码片段是完全错误的,我稍后会解释原因,但首先我们需要一些背景知识。

errno是一个thread-local变量。当系统调用或某些库函数失败时,它将设置为非零值。系统调用成功时,它保持不变。因此它始终包含上次调用失败的错误号。

这意味着您有两个选择。在每次调用之前将errno设置为0,或者使用errno的标准惯用法。这是标准习语的伪代码

if ( foo() == some_value_that_indicates_that_an_error_occurred )
    then the value in errno applies to foo
else
    foo succeeded and the errno must be ignored because it could be anything

大多数程序员都会使用标准习惯用法,因为在每次系统调用之前将errno设置为0是烦人且重复的。更不用说你可能忘记在实际上重要的一个地方将errno设置为0。


回到第一个代码段。这是错误的,因为没有来自strtol的返回值明确表明strtol失败了。如果strtol返回LONG_MAX,则可能是发生错误,或者字符串实际包含数字LONG_MAX。没有办法知道strtol呼叫是成功还是失败。这意味着标准习语(这是第一个代码片段试图实现的内容)不能与strtol一起使用。

要正确使用strtol,你需要在调用之前将errno设置为0,就像这样

errno = 0;
result = strtol( buffer, &endptr, 10 );
if ( errno == ERANGE )
{
    // handle the error
    // ERANGE is the only error mentioned in the C specification
}
else if ( endptr == buffer )
{
    // handle the error
    // the conversion failed, i.e. the input string was empty,
    // or only contained whitespace, or the first non-whitespace 
    // character was not valid
}

请注意,某些实现为errno定义了其他非零值。有关详细信息,请参见适用的手册页


3
投票

如果你打电话

result = strtol("-2147483648", NULL, 0);

要么

result = strtol("2147483647", NULL, 0);

在32位机器上,即使没有出现错误,你也可以在LONG_MIN获得LONG_MAXresult

正如user3386109所解释的,检测来自strtol的错误的一种方法是首先将errno设置为0。另一种方法是让它给你一个结束指针并查看它。有三到四种情况:

char *endptr;
long int result = strtol(str, &endptr, 10);
if(*str == '\0') {
    /* str was empty */
} else if(endptr == str) {
    /* str was completely invalid */
} else if(*endptr != '\0') {
    /* numeric result followed by trailing nonnumeric character(s) */
} else {
    /* str was a completely valid number (perhaps with leading whitespace) */
}

根据您的需要,前两个或三个案例可能会折叠在一起。您可能需要担心(a)“完全有效数字”是否可以表示(您可以使用errno进行测试),以及(b)是否有任何“尾随非数字字符”是无害的空白(唉,strtol没有检查你,所以如果你关心你将不得不检查自己)。

© www.soinside.com 2019 - 2024. All rights reserved.