From a1dd7f477edd4af6ba0bd4cbe219a8e46950c865 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Tue, 21 Nov 2023 17:12:37 +0100 Subject: steprl: fix formatting of LONG_MIN --- steprl.c | 12 +++++------- tests.c | 8 +++++++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/steprl.c b/steprl.c index d1ae3b9..39ae92e 100644 --- a/steprl.c +++ b/steprl.c @@ -10,16 +10,14 @@ steprl(char *dst, char *end, long n) char *bufend = buf + sizeof buf; char *s = bufend; - int neg = n < 0; - if (neg) - n = -n; + unsigned long u = n < 0 ? -n : n; do { - *--s = '0' + (n % 10); - n /= 10; - } while (n > 0); + *--s = '0' + (u % 10); + u /= 10; + } while (u); - if (neg) + if (n < 0) *--s = '-'; return stecpe(dst, end, s, bufend); diff --git a/tests.c b/tests.c index 7d6d9c2..5831e3c 100644 --- a/tests.c +++ b/tests.c @@ -1,5 +1,6 @@ #include #include +#include #include "ste.h" @@ -16,7 +17,7 @@ is(const char *desc, int ok) int main() { - printf("1..55\n"); + printf("1..56\n"); printf("# stecpy\n"); @@ -159,5 +160,10 @@ main() is("77777777 = full", strlen(buf) == 15); is("return value is end", pos == end); + pos = buf2; + end = buf2 + sizeof buf2; + pos = steprl(pos, end, LONG_MIN); + is("can format LONG_MIN", buf2[0] == '-' && strlen(buf2) > 9); + return status; } -- cgit 1.4.1