简体   繁体   English

strtoll 溢出时未将 errno 设置为 ERANGE

[英]strtoll not setting errno to ERANGE upon overflow

I'm parsing a long long value using fgets and strtoll as one does, but strtoll is not setting errno to ERANGE when an overflow occurs like it's supposed to.我正在使用fgetsstrtoll解析一个 long long 值,但是当溢出发生时, strtoll不会将errnoERANGE

From the man page :从手册页

The strtol() function returns the result of the conversion, unless the value would underflow or overflow. strtol() function 返回转换结果,除非值会下溢或溢出。 If an underflow occurs, strtol() returns LONG_MIN .如果发生下溢, strtol()返回LONG_MIN If an overflow occurs, strtol() returns LONG_MAX.如果发生溢出, strtol()返回 LONG_MAX。 In both cases, errno is set to ERANGE .在这两种情况下, errno都设置为ERANGE Precisely the same holds for strtoll() (with LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX) . strtoll() (使用 LLONG_MIN 和 LLONG_MAX 而不是 LONG_MIN 和 LONG_MAX)

Sample code without fgets for MRE purposes:用于 MRE 的不带fgets的示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>

int main(){

    long long number; //max value 9223372036854775807   
    char input[] = "12345678976543245678976543";
    char *end;

    number = strtoll(input, &end, 10);

    printf("%d %d <%s> %lld %lld\n", errno, ERANGE, input, (long long)number, LLONG_MAX);
    //prints 0 for errno and 34 for ERANGE regardless of overflow
    //eerno should be set to ERANGE so it should be 34

    int e = errno; //further testing, assigning errno
    printf("%d", e);// again prints 0 should be 34
}

Output is: Output 是:

0 34 <12345678976543245678976543> 9223372036854775807 9223372036854775807
0

Should be:应该:

34 34 <12345678976543245678976543> 9223372036854775807 9223372036854775807
34

This is highly confusing to me, especially because in an online compiler it seems to work fine.这让我非常困惑,特别是因为在在线编译器中它似乎工作正常。

I'm using gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2), ldd (Ubuntu GLIBC 2.31-0ubuntu9) 2.31 in a recently updated Linux Mint 20.我在最近更新的 Linux Mint 20 中使用 gcc 版本 9.3.0(Ubuntu 9.3.0-10ubuntu2),ldd(Ubuntu GLIBC 2.31-0ubuntu9)2.31。

I reinstalled the entire system and the problem is gone now, I should have maybe reported it, I was thinking about my problems more than in the community.我重新安装了整个系统,现在问题已经消失了,我应该报告它,我在考虑我的问题而不是在社区中。

The likely situation that caused these issues is the fact that I glazed over some of the recommended steps in upgrading my system.导致这些问题的可能情况是,我对升级系统的一些推荐步骤一无所知。

The circumstantial evidence:间接证据:

  • There were other issues like a wifi error, problems with the GRUB and with the timezone, they are also gone after the new instalation.还有其他问题,例如 wifi 错误、GRUB 和时区问题,在新安装后它们也消失了。

  • I also updgraded my desktop just to see if I could reproduce the same problems, but this time following carefully all the steps and everything works fine.我还升级了我的桌面,看看我是否可以重现同样的问题,但这次仔细遵循所有步骤,一切正常。

So for anyone upgrading UNIX systems, pay atention to the upgrade instructions .所以对于任何升级 UNIX 系统的人,请注意升级说明 And have backups, it saved my life in this instance.并且有备份,在这种情况下它救了我的命。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM