X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdlib%2Fstrtol.c;h=7ff35b3243c28617411fb1df2b80c2b454c82f63;hb=12e17136786afb1775c9dc946cbe41f5e230c24a;hp=1a40409d7d030dabc609e84f7c163f973d4932e2;hpb=a7fcb7c1fec94e1992098fb892bc8fb7d607ae5e;p=pdclib.old diff --git a/functions/stdlib/strtol.c b/functions/stdlib/strtol.c index 1a40409..7ff35b3 100644 --- a/functions/stdlib/strtol.c +++ b/functions/stdlib/strtol.c @@ -8,26 +8,27 @@ Permission is granted to use, modify, and / or redistribute at will. */ -#include <_PDCLIB_int.h> #include -#include #include #ifndef REGTEST +#include + long int strtol( const char * s, char ** endptr, int base ) { long int rc; char sign = '+'; const char * p = _PDCLIB_strtox_prelim( s, &sign, &base ); + if ( base < 2 || base > 36 ) return 0; if ( sign == '+' ) { - rc = _PDCLIB_strtox_main( &p, base, LONG_MAX, LONG_MAX / base, LONG_MAX % base, &sign ); + rc = _PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LONG_MAX, (uintmax_t)( LONG_MAX / base ), (uintmax_t)( LONG_MAX % base ), &sign ); } else { /* FIXME: This breaks on some machines that round negatives wrongly */ - rc = _PDCLIB_strtox_main( &p, base, LONG_MIN, LONG_MIN / -base, -( LONG_MIN % base ), &sign ); + rc = _PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LONG_MIN, (uintmax_t)( LONG_MIN / -base ), (uintmax_t)( -( LONG_MIN % base ) ), &sign ); } if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s; return ( sign == '+' ) ? rc : -rc; @@ -37,14 +38,19 @@ long int strtol( const char * s, char ** endptr, int base ) #ifdef TEST #include <_PDCLIB_test.h> + +#ifndef _PDCLIB_INT_H +#define _PDCLIB_INT_H +#include <_PDCLIB_int.h> +#endif + #include -int main() +int main( void ) { char * endptr; /* this, to base 36, overflows even a 256 bit integer */ char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_"; - BEGIN_TESTS; errno = 0; /* basic functionality */ TESTCASE( strtol( "123", NULL, 10 ) == 123 );