From 8a77f48634a055a11edf1583babd1b7a86e7d3a6 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Wed, 28 Nov 2018 14:40:40 -0500 Subject: [PATCH] Fix undefined behavior when printing INT_MIN/INT64_MIN. Negating the most-negative signed integer results in overflow, which is undefined behavior. Fix this by casting to an unsigned type first (unsigned overflow is well-defined as it uses modular arithmetic). --- src/unity.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unity.c b/src/unity.c index d1dda41..00c9412 100644 --- a/src/unity.c +++ b/src/unity.c @@ -183,7 +183,7 @@ void UnityPrintNumber(const UNITY_INT number_to_print) { /* A negative number, including MIN negative */ UNITY_OUTPUT_CHAR('-'); - number = (UNITY_UINT)(-number_to_print); + number = -number; } UnityPrintNumberUnsigned(number); } -- GitLab