From 8a77f48634a055a11edf1583babd1b7a86e7d3a6 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Wed, 28 Nov 2018 14:40:40 -0500 Subject: [PATCH 1/2] 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); } From d09f4953ffd67bcaf774d3f0e6476a9ec33265f4 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Wed, 28 Nov 2018 15:17:25 -0500 Subject: [PATCH 2/2] Fix another signed integer overflow. --- src/unity.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/unity.c b/src/unity.c index 00c9412..70c6b26 100644 --- a/src/unity.c +++ b/src/unity.c @@ -1019,22 +1019,22 @@ void UnityAssertNumbersWithin(const UNITY_UINT delta, { if (actual > expected) { - Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta); + Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta); } else { - Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta); + Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta); } } else { if ((UNITY_UINT)actual > (UNITY_UINT)expected) { - Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta); + Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta); } else { - Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta); + Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta); } }