summaryrefslogtreecommitdiff
path: root/strings/int2str.c
diff options
context:
space:
mode:
authorunknown <knielsen@loke.(none)>2007-10-31 10:34:26 +0100
committerunknown <knielsen@loke.(none)>2007-10-31 10:34:26 +0100
commitf8b5a34083ebcaccdca66100ccdb72138dbd9a2f (patch)
tree8d9c36f9a96e41cc8635d043cb5bea05bb7f5739 /strings/int2str.c
parentc63f2e3fb6bf3a5eb6da2995c47965843e9b212d (diff)
downloadmariadb-git-f8b5a34083ebcaccdca66100ccdb72138dbd9a2f.tar.gz
BUG#31799: Scrambled number output due to integer overflow
An integer overflow in number->string conversion caused completely wrong output of the number LONGLONG_MIN with gcc 4.2.1. Fixed by eliminating the overflow, using only operations that are well-defined in ANSI C. strings/ctype-simple.c: An integer overflow in number->string conversion caused completely wrong output of the number LONGLONG_MIN with gcc 4.2.1. Fixed by eliminating the overflow, using only operations that are well-defined in ANSI C. strings/ctype-ucs2.c: An integer overflow in number->string conversion caused completely wrong output of the number LONGLONG_MIN with gcc 4.2.1. Fixed by eliminating the overflow, using only operations that are well-defined in ANSI C. strings/int2str.c: An integer overflow in number->string conversion caused completely wrong output of the number LONGLONG_MIN with gcc 4.2.1. Fixed by eliminating the overflow, using only operations that are well-defined in ANSI C. strings/longlong2str.c: An integer overflow in number->string conversion caused completely wrong output of the number LONGLONG_MIN with gcc 4.2.1. Fixed by eliminating the overflow, using only operations that are well-defined in ANSI C.
Diffstat (limited to 'strings/int2str.c')
-rw-r--r--strings/int2str.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/strings/int2str.c b/strings/int2str.c
index 9fc53032819..fba98aac3f1 100644
--- a/strings/int2str.c
+++ b/strings/int2str.c
@@ -57,6 +57,7 @@ int2str(register long int val, register char *dst, register int radix,
register char *p;
long int new_val;
char *dig_vec= upcase ? _dig_vec_upper : _dig_vec_lower;
+ ulong uval= (ulong) val;
if (radix < 0)
{
@@ -65,7 +66,8 @@ int2str(register long int val, register char *dst, register int radix,
if (val < 0)
{
*dst++ = '-';
- val = -val;
+ /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
+ uval = (ulong)0 - uval;
}
radix = -radix;
}
@@ -86,8 +88,8 @@ int2str(register long int val, register char *dst, register int radix,
*/
p = &buffer[sizeof(buffer)-1];
*p = '\0';
- new_val=(ulong) val / (ulong) radix;
- *--p = dig_vec[(uchar) ((ulong) val- (ulong) new_val*(ulong) radix)];
+ new_val= uval / (ulong) radix;
+ *--p = dig_vec[(uchar) (uval- (ulong) new_val*(ulong) radix)];
val = new_val;
#ifdef HAVE_LDIV
while (val != 0)
@@ -133,20 +135,22 @@ char *int10_to_str(long int val,char *dst,int radix)
char buffer[65];
register char *p;
long int new_val;
+ unsigned long int uval = (unsigned long int) val;
if (radix < 0) /* -10 */
{
if (val < 0)
{
*dst++ = '-';
- val = -val;
+ /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
+ uval = (unsigned long int)0 - uval;
}
}
p = &buffer[sizeof(buffer)-1];
*p = '\0';
- new_val= (long) ((unsigned long int) val / 10);
- *--p = '0'+ (char) ((unsigned long int) val - (unsigned long) new_val * 10);
+ new_val= (long) (uval / 10);
+ *--p = '0'+ (char) (uval - (unsigned long) new_val * 10);
val = new_val;
while (val != 0)