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 8059 e70f5749f692
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
--- a/src/stdlib/SDL_string.c	Wed Dec 11 21:13:45 2013 -0800
+++ b/src/stdlib/SDL_string.c	Wed Dec 11 21:17:24 2013 -0800
@@ -665,23 +665,9 @@
 
     if (value < 0) {
         *bufp++ = '-';
-        value = -value;
-    }
-    if (value) {
-        while (value > 0) {
-            *bufp++ = ntoa_table[value % radix];
-            value /= radix;
-        }
+        SDL_ultoa(-value, bufp, radix);
     } else {
-        *bufp++ = '0';
-    }
-    *bufp = '\0';
-
-    /* The numbers went into the string backwards. :) */
-    if (*string == '-') {
-        SDL_strrev(string + 1);
-    } else {
-        SDL_strrev(string);
+        SDL_ultoa(value, bufp, radix);
     }
 
     return string;
@@ -723,23 +709,9 @@
 
     if (value < 0) {
         *bufp++ = '-';
-        value = -value;
-    }
-    if (value) {
-        while (value > 0) {
-            *bufp++ = ntoa_table[value % radix];
-            value /= radix;
-        }
+        SDL_ulltoa(-value, bufp, radix);
     } else {
-        *bufp++ = '0';
-    }
-    *bufp = '\0';
-
-    /* The numbers went into the string backwards. :) */
-    if (*string == '-') {
-        SDL_strrev(string + 1);
-    } else {
-        SDL_strrev(string);
+        SDL_ulltoa(value, bufp, radix);
     }
 
     return string;