2

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.

From the man page:

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).

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
}

Output is:

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.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • @sebastian, it seems the problem is confined to my system, ufortunately, if there was someone with the same system here maybe the problem could be pinpointed. – anastaciu Aug 22 '20 at 15:23
  • 2
    32-bit version of MSVC outputs `34 34` for an overrange input so their man page wasn't fully described. – Weather Vane Aug 22 '20 at 15:24
  • @WeatherVane yes, the ERANGE part is in the [`strtol` docs](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strtol-wcstol-strtol-l-wcstol-l?view=vs-2019) they forgot `strtoll` – anastaciu Aug 22 '20 at 15:33
  • 1
    Note that on Linux, standard functions like `strtoll` are provided by the standard C library (usually `glibc`), not by the gcc compiler itself. So focusing on gcc version numbers is not going to get anywhere; look at glibc versions instead. – Nate Eldredge Aug 22 '20 at 20:09
  • 1
    It appears this is highly unlikely to be an issue with gcc or glibc (not reproducible). Rather something wrong environment or broken gnu toolchains. – P.P Aug 22 '20 at 20:53
  • Note `int e = errno;` should happen before the first `printf()` to eliminate `printf()` as the bad boy. IOWs, capture `errno` right after `strtoll()`. – chux - Reinstate Monica Aug 22 '20 at 22:34
  • @chux arguments are evaluated *before* the function call - printf cannot change errno in a way that would affect the print – Antti Haapala -- Слава Україні Aug 23 '20 at 05:05
  • Please report it as a bug as it is one! What compiler switches are you using? Mine, Focal plain, are exactly 2.31-0ubuntu9 and 9.3.0-10ubuntu2), and I get 34... – Antti Haapala -- Слава Україні Aug 23 '20 at 05:07
  • @AnttiHaapala "printf cannot change errno in a way that would affect the print" --> Perhaps, yet there is a bug somewhere. Just trying to clearly separate candidate contributors. – chux - Reinstate Monica Aug 23 '20 at 05:11

1 Answers1

1

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.

  • 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. And have backups, it saved my life in this instance.

anastaciu
  • 23,467
  • 7
  • 28
  • 53