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>2023-11-21 17:12:37 +0100
commita73b6dcb522ad51263a9c24bf53a5edf47dd55be (patch)
tree413fd57753a5409375b08bcca68ebf0b7c26332a
parent858c2bff541b09f345343c673c6c9d262b897f4f (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;
 }