Fixed bug 2050 - Obvious bugs in SDL_ltoa and SDL_lltoa
authorSam Lantinga <slouken@libsdl.org>
Wed, 11 Dec 2013 21:17:24 -0800
changeset 8059e70f5749f692
parent 8058 8e4512b74223
child 8060 d1948d163145
Fixed bug 2050 - Obvious bugs in SDL_ltoa and SDL_lltoa

pjz

SDL_ltoa(-2147483648,s,10) only returns "-" because there is a bug in the code:

if ( value < 0 ) {
*bufp++ = '-';
value = -value;
}

but -(-2147483648) is still -2147483648 (0x80000000) as signed int (or long), so the following loop doesn't run at all. Similar bug are also in SDL_lltoa.

BTW, there is no sanity check for radix.
src/stdlib/SDL_string.c
     1.1 --- a/src/stdlib/SDL_string.c	Wed Dec 11 21:13:45 2013 -0800
     1.2 +++ b/src/stdlib/SDL_string.c	Wed Dec 11 21:17:24 2013 -0800
     1.3 @@ -665,23 +665,9 @@
     1.4  
     1.5      if (value < 0) {
     1.6          *bufp++ = '-';
     1.7 -        value = -value;
     1.8 -    }
     1.9 -    if (value) {
    1.10 -        while (value > 0) {
    1.11 -            *bufp++ = ntoa_table[value % radix];
    1.12 -            value /= radix;
    1.13 -        }
    1.14 +        SDL_ultoa(-value, bufp, radix);
    1.15      } else {
    1.16 -        *bufp++ = '0';
    1.17 -    }
    1.18 -    *bufp = '\0';
    1.19 -
    1.20 -    /* The numbers went into the string backwards. :) */
    1.21 -    if (*string == '-') {
    1.22 -        SDL_strrev(string + 1);
    1.23 -    } else {
    1.24 -        SDL_strrev(string);
    1.25 +        SDL_ultoa(value, bufp, radix);
    1.26      }
    1.27  
    1.28      return string;
    1.29 @@ -723,23 +709,9 @@
    1.30  
    1.31      if (value < 0) {
    1.32          *bufp++ = '-';
    1.33 -        value = -value;
    1.34 -    }
    1.35 -    if (value) {
    1.36 -        while (value > 0) {
    1.37 -            *bufp++ = ntoa_table[value % radix];
    1.38 -            value /= radix;
    1.39 -        }
    1.40 +        SDL_ulltoa(-value, bufp, radix);
    1.41      } else {
    1.42 -        *bufp++ = '0';
    1.43 -    }
    1.44 -    *bufp = '\0';
    1.45 -
    1.46 -    /* The numbers went into the string backwards. :) */
    1.47 -    if (*string == '-') {
    1.48 -        SDL_strrev(string + 1);
    1.49 -    } else {
    1.50 -        SDL_strrev(string);
    1.51 +        SDL_ulltoa(value, bufp, radix);
    1.52      }
    1.53  
    1.54      return string;