strtoll溢出时未将errno设置为ERANGE [英] strtoll not setting errno to ERANGE upon overflow

查看:119
本文介绍了strtoll溢出时未将errno设置为ERANGE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 fgets strtoll 解析长整型值,但是 strtoll 没有设置 errno ERANGE 时(如预期的那样)发生溢出.

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.

在手册页中:

strtol()函数返回转换结果,除非该值下溢或溢出.如果发生下溢,则 strtol()返回 LONG_MIN .如果发生溢出, strtol()返回LONG_MAX.在这两种情况下, errno 都设置为 ERANGE .. strtoll()完全相同(使用LLONG_MIN和LLONG_MAX代替LONG_MIN和LONG_MAX).

The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs, strtol() returns LONG_MIN. If an overflow occurs, strtol() returns LONG_MAX. In both cases, errno is set to ERANGE. Precisely the same holds for strtoll() (with LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX).

出于MRE目的而没有 fgets 的示例代码:

Sample code without fgets for MRE purposes:

#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
}

输出为:

0 34 <12345678976543245678976543> 9223372036854775807 9223372036854775807
0

应该是:

34 34 <12345678976543245678976543> 9223372036854775807 9223372036854775807
34

这对我来说非常混乱,尤其是因为在在线编译器中 似乎可以正常工作.

This is highly confusing to me, especially because in an online compiler it seems to work fine.

我在最近更新的Linux Mint 20中使用gcc版本9.3.0(Ubuntu 9.3.0-10ubuntu2),ldd(Ubuntu GLIBC 2.31-0ubuntu9)2.31.

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.

推荐答案

我重新安装了整个系统,现在问题已经消失了,我应该报告一下,我在社区中比在考虑问题.

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.

间接证据:

  • 还有其他问题,例如wifi错误,GRUB和时区问题,在新安装后也不再存在.

  • There were other issues like a wifi error, problems with the GRUB and with the timezone, they are also gone after the new instalation.

我还升级了桌面,以查看是否可以重现相同的问题,但是这次仔细地遵循了所有步骤,一切都很好.

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.

因此,对于任何升级UNIX系统的人, 请注意升级说明 .并进行备份,在这种情况下挽救了我的生命.

So for anyone upgrading UNIX systems, pay atention to the upgrade instructions. And have backups, it saved my life in this instance.

这篇关于strtoll溢出时未将errno设置为ERANGE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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