From: John Marshall Date: Tue, 23 Jul 2013 10:56:15 +0000 (+0100) Subject: Fix kputl(LONG_MIN) bug X-Git-Tag: spawn-final~21^2 X-Git-Url: http://www.kaiwu.me/postgresql/commit/static/gitweb.js?a=commitdiff_plain;h=d47fc9def4581eabedffbbffbe5ad3ab6af70822;p=klib.git Fix kputl(LONG_MIN) bug Use an explicitly unsigned long, and do any negation as that type. Make similar changes to kputw(), as the code is clearer. --- diff --git a/kstring.h b/kstring.h index 9438b7f..da53ba6 100644 --- a/kstring.h +++ b/kstring.h @@ -174,10 +174,10 @@ static inline int kputsn_(const void *p, int l, kstring_t *s) static inline int kputw(int c, kstring_t *s) { char buf[16]; - int l, x; - if (c == 0) return kputc('0', s); - if (c < 0) for (l = 0, x = c; x < 0; x /= 10) buf[l++] = '0' - (x%10); - else for (l = 0, x = c; x > 0; x /= 10) buf[l++] = x%10 + '0'; + int i, l = 0; + unsigned int x = c; + if (c < 0) x = -x; + do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0); if (c < 0) buf[l++] = '-'; if (s->l + l + 1 >= s->m) { char *tmp; @@ -188,7 +188,7 @@ static inline int kputw(int c, kstring_t *s) else return EOF; } - for (x = l - 1; x >= 0; --x) s->s[s->l++] = buf[x]; + for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i]; s->s[s->l] = 0; return 0; } @@ -217,9 +217,10 @@ static inline int kputuw(unsigned c, kstring_t *s) static inline int kputl(long c, kstring_t *s) { char buf[32]; - long l, x; - if (c == 0) return kputc('0', s); - for (l = 0, x = c < 0? -c : c; x > 0; x /= 10) buf[l++] = x%10 + '0'; + int i, l = 0; + unsigned long x = c; + if (c < 0) x = -x; + do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0); if (c < 0) buf[l++] = '-'; if (s->l + l + 1 >= s->m) { char *tmp; @@ -230,7 +231,7 @@ static inline int kputl(long c, kstring_t *s) else return EOF; } - for (x = l - 1; x >= 0; --x) s->s[s->l++] = buf[x]; + for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i]; s->s[s->l] = 0; return 0; }