aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/support
diff options
context:
space:
mode:
authorGravatar Jan Tattermusch <jtattermusch@google.com>2015-12-08 17:48:17 -0800
committerGravatar Jan Tattermusch <jtattermusch@google.com>2015-12-08 17:48:17 -0800
commit338ef4aa6cf39be4761b39b25ce5dec829af535b (patch)
tree9b394a6239581b9bab3a0975ed1510e759b8fdec /src/core/support
parentf67f071f7e1fadd144a867eb7fbe6551cbfcc3f5 (diff)
fix bug MIN_VALUE bug in ltoa and int64toa and add tests
Diffstat (limited to 'src/core/support')
-rw-r--r--src/core/support/string.c26
1 files changed, 18 insertions, 8 deletions
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);