Lori (lightning_rose) wrote in cprogramming,
Lori
lightning_rose
cprogramming

Holy WTF?!

I have found what I think is a bug in the GNU stdlib functions atoi(), atol(), and strtol().

I'm using: gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3



For most of my coding, I use unsigned ints/longs. Today I was converting strings to their numeric values and found that string values exceeding the MAX_SIGNED_INT_VALUE (ie 32767, 2147483647) for the given type are set to the MAX_SIGNED_INT_VALUE.

Here's a test program I wrote, including myAtol() - my own ascii to long conversion routine, and a sample run.

Note: atoll() and strtoll() work as expected.

Comments?


$ ./atotest 4294967290 4294967295 2147483647 2147483648 -1 sizeof(value) = 4 :: Max Value = 4294967295 String: 4294967290 atoi: 2147483647 :: 1073741823 :: 0x7fffffff atol: 2147483647 :: 1073741823 :: 0x7fffffff strtol: 2147483647 :: 1073741823 :: 0x7fffffff atoll: 4294967290 :: 2147483645 :: 0xfffffffa strtoll: 4294967290 :: 2147483645 :: 0xfffffffa myAtol: 4294967290 :: 2147483645 :: 0xfffffffa String: 4294967295 atoi: 2147483647 :: 1073741823 :: 0x7fffffff atol: 2147483647 :: 1073741823 :: 0x7fffffff strtol: 2147483647 :: 1073741823 :: 0x7fffffff atoll: 4294967295 :: 2147483647 :: 0xffffffff strtoll: 4294967295 :: 2147483647 :: 0xffffffff myAtol: 4294967295 :: 2147483647 :: 0xffffffff String: 2147483647 atoi: 2147483647 :: 1073741823 :: 0x7fffffff atol: 2147483647 :: 1073741823 :: 0x7fffffff strtol: 2147483647 :: 1073741823 :: 0x7fffffff atoll: 2147483647 :: 1073741823 :: 0x7fffffff strtoll: 2147483647 :: 1073741823 :: 0x7fffffff myAtol: 2147483647 :: 1073741823 :: 0x7fffffff String: 2147483648 atoi: 2147483647 :: 1073741823 :: 0x7fffffff atol: 2147483647 :: 1073741823 :: 0x7fffffff strtol: 2147483647 :: 1073741823 :: 0x7fffffff atoll: 2147483648 :: 1073741824 :: 0x80000000 strtoll: 2147483648 :: 1073741824 :: 0x80000000 myAtol: 2147483648 :: 1073741824 :: 0x80000000 String: -1 atoi: 4294967295 :: 2147483647 :: 0xffffffff atol: 4294967295 :: 2147483647 :: 0xffffffff strtol: 4294967295 :: 2147483647 :: 0xffffffff atoll: 4294967295 :: 2147483647 :: 0xffffffff strtoll: 4294967295 :: 2147483647 :: 0xffffffff myAtol: 4294967295 :: 2147483647 :: 0xffffffff


#include <stdio.h> #include <stdlib.h> /*************************************************************************/ typedef unsigned int unint; typedef unsigned char uchar; typedef unsigned long ulong; typedef unsigned short ushort; /*************************************************************************/ long myAtol( const char *sPtr ) { long value = 0; int negFlag = 1; if ( *sPtr == '\0' ) return( 0 ); if ( *sPtr == '-' ) { negFlag = -1; ++sPtr; } while ( *sPtr ) { if (( *sPtr < '0' ) || ( *sPtr > '9' )) return( value *= negFlag ); value = ( value * 10 ) + ( *sPtr - '0' ); ++sPtr; } return( value *= negFlag ); } /*************************************************************************/ int main( int argc, char **argv ) { ulong value; // unint value; // ushort value; value = -1; printf( "\n sizeof(value) = %d :: Max Value = %u", sizeof( value ), (unint) value ); if ( argc < 2 ) { printf( "\n\nUsage is: %s VALUE [ VALUE ... ] \n\n", *argv ); exit( 1 ); } while ( --argc ) { printf( "\n\n String: %s", *++argv ); value = atoi( *argv ); printf( "\n atoi: %10u :: %10u :: 0x%08x", (unint) value, (unint) ( value / 2 ), (unint) value ); value = atol( *argv ); printf( "\n atol: %10u :: %10u :: 0x%08x", (unint) value, (unint) ( value / 2 ), (unint) value ); value = strtol( *argv, NULL, 10 ); printf( "\n strtol: %10u :: %10u :: 0x%08x", (unint) value, (unint) ( value / 2 ), (unint) value ); value = atoll( *argv ); printf( "\n atoll: %10u :: %10u :: 0x%08x", (unint) value, (unint) ( value / 2 ), (unint) value ); value = strtoll( *argv, NULL, 10 ); printf( "\n strtoll: %10u :: %10u :: 0x%08x", (unint) value, (unint) ( value / 2 ), (unint) value ); value = myAtol( *argv ); printf( "\n myAtol: %10u :: %10u :: 0x%08x", (unint) value, (unint) ( value / 2 ), (unint) value ); } printf( "\n\n" ); return( 0 ); } /* eoFile ****************************************************************/ /*************************************************************************/
Subscribe
  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 2 comments