about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2023-11-21 17:12:37 +0100
committerLeah Neukirchen <leah@vuxu.org>2024-07-14 14:50:34 +0200
commita1dd7f477edd4af6ba0bd4cbe219a8e46950c865 (patch)
treea00dcb3660ca9f32b77048b9c1840c8cdab6187e
parent47d2c0177c285ac6fcf0458b3ef10e63e43dedb2 (diff)
downloadlibste-steprl.tar.gz
libste-steprl.tar.xz
libste-steprl.zip
steprl: fix formatting of LONG_MIN steprl
-rw-r--r--steprl.c12
-rw-r--r--tests.c8
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 <stdio.h>
 #include <string.h>
+#include <limits.h>
 
 #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;
 }