Search code examples
coverflowc99strtol

strtol not detecting integer overflow


I'm trying to convert a character array into an integer using strtol using the below code:

int foo = strtol(temp, (char **)NULL, 0);

Where temp = 4000000010

However strtol does not detect the overflow, it does not set foo to LONG_MAX and errno does not change from its inital value of 0.

Instead bob is changed to the overflowed value of -294967286.

So I was wondering what am I missing here?


Solution

  • The overflow probably occurs on the implicit long to int conversion in your statement, not inside strtol().