Tcl Source Code

Artifact [d0d6f7e6c7]
Login

Artifact d0d6f7e6c7bb8bd1ea2ae20e64ef1575dd2a8378e04e36b30a67b79a101f2002:

Attachment "0001-Fix-undefined-behavior-in-TclFormatInt.patch" to ticket [90a70fb142] added by anonymous 2022-06-16 13:29:13. (unpublished)
From 965ed8debbd03d3a3dc833b226b7411f6939918b Mon Sep 17 00:00:00 2001
From: Tom Stellard <[email protected]>
Date: Thu, 16 Jun 2022 13:19:19 +0000
Subject: [PATCH] Fix undefined behavior in TclFormatInt

When n is LONG_MIN, negating it would be undefined behavior since
-LONG_MIN is too large to fit into the long type, so we need to cast
n to unsigned long before negating it.

This was caught by the undefined behavior sanitizer.
---
 generic/tclUtil.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/generic/tclUtil.c b/generic/tclUtil.c
index 7a38b07ff..56ccab48a 100644
--- a/generic/tclUtil.c
+++ b/generic/tclUtil.c
@@ -3616,7 +3616,7 @@ TclFormatInt(
      * Generate the characters of the result backwards in the buffer.
      */
 
-    intVal = (n < 0 ? -n : n);
+    intVal = (n < 0 ? -(unsigned long)n : n);
     i = 0;
     buffer[0] = '\0';
     do {
-- 
2.36.1