Commit: 7225750df35edf7ee20298f5dbb1dff44bd5c634
Parent: 56610f607a15317745697fd264acf2604264054b
Author: Randy Palamar
Date:   Thu, 21 Nov 2024 15:58:14 -0700
guard against I64_MIN in printing code
we don't care about printing this but we will set the error field
and return
Diffstat:
| M | util.c |  |  | 53 | ++++++++++++++++++++++++++++++----------------------- | 
| M | util.h |  |  | 2 | ++ | 
2 files changed, 32 insertions(+), 23 deletions(-)
diff --git a/util.c b/util.c
@@ -294,31 +294,35 @@ stream_push_s8s(Stream *s, u32 count, s8 *strs)
 static void
 stream_push_hex_u64(Stream *s, u64 n)
 {
-	static u8 hex[16] = {"0123456789abcdef"};
-	u8  buf[16];
-	u8 *end = buf + sizeof(buf);
-	u8 *beg = end;
-	while (n) {
-		*--beg = hex[n & 0x0F];
-		n >>= 4;
+	if (!s->errors) {
+		static u8 hex[16] = {"0123456789abcdef"};
+		u8  buf[16];
+		u8 *end = buf + sizeof(buf);
+		u8 *beg = end;
+		while (n) {
+			*--beg = hex[n & 0x0F];
+			n >>= 4;
+		}
+		while (end - beg < 2)
+			*--beg = '0';
+		stream_push_s8(s, (s8){.len = end - beg, .data = beg});
 	}
-	while (end - beg < 2)
-		*--beg = '0';
-	stream_push_s8(s, (s8){.len = end - beg, .data = beg});
 }
 
 static void
 stream_push_u64_padded(Stream *s, u64 n, u32 width)
 {
-	u8 tmp[64];
-	u8 *end = tmp + sizeof(tmp);
-	u8 *beg = end;
-	do { *--beg = '0' + (n % 10); } while (n /= 10);
-
-	s8 str = {.len = end - beg, .data = beg};
-	for (u32 i = str.len; i < width; i++)
-		stream_push_byte(s, ' ');
-	stream_push_s8(s, str);
+	if (!s->errors) {
+		u8 tmp[64];
+		u8 *end = tmp + sizeof(tmp);
+		u8 *beg = end;
+		do { *--beg = '0' + (n % 10); } while (n /= 10);
+
+		s8 str = {.len = end - beg, .data = beg};
+		for (u32 i = str.len; i < width; i++)
+			stream_push_byte(s, ' ');
+		stream_push_s8(s, str);
+	}
 }
 
 static void
@@ -330,11 +334,14 @@ stream_push_u64(Stream *s, u64 n)
 static void
 stream_push_i64(Stream *s, i64 n)
 {
-	if (n < 0) {
-		stream_push_byte(s, '-');
-		n = -n;
+	s->errors |= n == I64_MIN;
+	if (!s->errors) {
+		if (n < 0) {
+			stream_push_byte(s, '-');
+			n = -n;
+		}
+		stream_push_u64_padded(s, n, 0);
 	}
-	stream_push_u64_padded(s, n, 0);
 }
 
 static void
diff --git a/util.h b/util.h
@@ -50,6 +50,8 @@
 #define ALT_BACKLOG_SIZE  (2 * MEGABYTE)
 #define ALT_BACKLOG_LINES (1024UL)
 
+#define I64_MIN INT64_MIN
+
 typedef float     f32;
 typedef double    f64;
 typedef char      c8;