From 338ef4aa6cf39be4761b39b25ce5dec829af535b Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 8 Dec 2015 17:48:17 -0800 Subject: fix bug MIN_VALUE bug in ltoa and int64toa and add tests --- src/core/support/string.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/core/support/string.c b/src/core/support/string.c index fad0b4008b..bebeeb9513 100644 --- a/src/core/support/string.c +++ b/src/core/support/string.c @@ -153,6 +153,7 @@ void gpr_reverse_bytes(char *str, int len) { } int gpr_ltoa(long value, char *string) { + unsigned long uval; int i = 0; int neg = value < 0; @@ -162,10 +163,14 @@ int gpr_ltoa(long value, char *string) { return 1; } - if (neg) value = -value; - while (value) { - string[i++] = (char)('0' + value % 10); - value /= 10; + if (neg) { + uval = -value; + } else { + uval = value; + } + while (uval) { + string[i++] = (char)('0' + uval % 10); + uval /= 10; } if (neg) string[i++] = '-'; gpr_reverse_bytes(string, i); @@ -174,6 +179,7 @@ int gpr_ltoa(long value, char *string) { } int gpr_int64toa(gpr_int64 value, char *string) { + gpr_uint64 uval; int i = 0; int neg = value < 0; @@ -183,10 +189,14 @@ int gpr_int64toa(gpr_int64 value, char *string) { return 1; } - if (neg) value = -value; - while (value) { - string[i++] = (char)('0' + value % 10); - value /= 10; + if (neg) { + uval = -value; + } else { + uval = value; + } + while (uval) { + string[i++] = (char)('0' + uval % 10); + uval /= 10; } if (neg) string[i++] = '-'; gpr_reverse_bytes(string, i); -- cgit v1.2.3