Skip to content

Commit 0bc0fed

Browse files
authored
Merge pull request #784 from burakutkuc/fix-assert-uint-overflow
fix: Correct UINT max value handling (thanks @burakutkuc!)
2 parents eb79bce + f96b64f commit 0bc0fed

File tree

2 files changed

+275
-191
lines changed

2 files changed

+275
-191
lines changed

src/unity.c

Lines changed: 124 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ void UnityPrintLen(const char* string, const UNITY_UINT32 length)
195195
}
196196

197197
/*-----------------------------------------------*/
198-
void UnityPrintNumberByStyle(const UNITY_INT number, const UNITY_DISPLAY_STYLE_T style)
198+
void UnityPrintIntNumberByStyle(const UNITY_INT number, const UNITY_DISPLAY_STYLE_T style)
199199
{
200200
if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
201201
{
@@ -233,9 +233,13 @@ void UnityPrintNumberByStyle(const UNITY_INT number, const UNITY_DISPLAY_STYLE_T
233233
UnityPrintNumber(number);
234234
}
235235
}
236-
else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
236+
}
237+
238+
void UnityPrintUintNumberByStyle(const UNITY_UINT number, const UNITY_DISPLAY_STYLE_T style)
239+
{
240+
if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
237241
{
238-
UnityPrintNumberUnsigned((UNITY_UINT)number);
242+
UnityPrintNumberUnsigned(number);
239243
}
240244
else
241245
{
@@ -742,61 +746,102 @@ void UnityAssertBits(const UNITY_INT mask,
742746
}
743747

744748
/*-----------------------------------------------*/
745-
void UnityAssertEqualNumber(const UNITY_INT expected,
746-
const UNITY_INT actual,
747-
const char* msg,
748-
const UNITY_LINE_TYPE lineNumber,
749-
const UNITY_DISPLAY_STYLE_T style)
749+
void UnityAssertEqualIntNumber(const UNITY_INT expected,
750+
const UNITY_INT actual,
751+
const char* msg,
752+
const UNITY_LINE_TYPE lineNumber,
753+
const UNITY_DISPLAY_STYLE_T style)
750754
{
751755
RETURN_IF_FAIL_OR_IGNORE;
752756

753757
if (expected != actual)
754758
{
755759
UnityTestResultsFailBegin(lineNumber);
756760
UnityPrint(UnityStrExpected);
757-
UnityPrintNumberByStyle(expected, style);
761+
UnityPrintIntNumberByStyle(expected, style);
758762
UnityPrint(UnityStrWas);
759-
UnityPrintNumberByStyle(actual, style);
763+
UnityPrintIntNumberByStyle(actual, style);
760764
UnityAddMsgIfSpecified(msg);
761765
UNITY_FAIL_AND_BAIL;
762766
}
763767
}
764768

769+
void UnityAssertEqualUintNumber(const UNITY_UINT expected,
770+
const UNITY_UINT actual,
771+
const char* msg,
772+
const UNITY_LINE_TYPE lineNumber,
773+
const UNITY_DISPLAY_STYLE_T style)
774+
{
775+
RETURN_IF_FAIL_OR_IGNORE;
776+
777+
if (expected != actual)
778+
{
779+
UnityTestResultsFailBegin(lineNumber);
780+
UnityPrint(UnityStrExpected);
781+
UnityPrintUintNumberByStyle(expected, style);
782+
UnityPrint(UnityStrWas);
783+
UnityPrintUintNumberByStyle(actual, style);
784+
UnityAddMsgIfSpecified(msg);
785+
UNITY_FAIL_AND_BAIL;
786+
}
787+
}
765788
/*-----------------------------------------------*/
766-
void UnityAssertGreaterOrLessOrEqualNumber(const UNITY_INT threshold,
767-
const UNITY_INT actual,
768-
const UNITY_COMPARISON_T compare,
769-
const char *msg,
770-
const UNITY_LINE_TYPE lineNumber,
771-
const UNITY_DISPLAY_STYLE_T style)
789+
void UnityAssertIntGreaterOrLessOrEqualNumber(const UNITY_INT threshold,
790+
const UNITY_INT actual,
791+
const UNITY_COMPARISON_T compare,
792+
const char *msg,
793+
const UNITY_LINE_TYPE lineNumber,
794+
const UNITY_DISPLAY_STYLE_T style)
772795
{
773796
int failed = 0;
774797
RETURN_IF_FAIL_OR_IGNORE;
775798

776-
if ((threshold == actual) && (compare & UNITY_EQUAL_TO)) { return; }
777-
if ((threshold == actual)) { failed = 1; }
799+
if ((threshold == actual) && !(compare & UNITY_EQUAL_TO)) { failed = 1; }
778800

779-
if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
780-
{
781-
if ((actual > threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
782-
if ((actual < threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
783-
}
784-
else /* UINT or HEX */
801+
if ((actual > threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
802+
if ((actual < threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
803+
804+
if (failed)
785805
{
786-
if (((UNITY_UINT)actual > (UNITY_UINT)threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
787-
if (((UNITY_UINT)actual < (UNITY_UINT)threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
806+
UnityTestResultsFailBegin(lineNumber);
807+
UnityPrint(UnityStrExpected);
808+
UnityPrintIntNumberByStyle(actual, style);
809+
if (compare & UNITY_GREATER_THAN) { UnityPrint(UnityStrGt); }
810+
if (compare & UNITY_SMALLER_THAN) { UnityPrint(UnityStrLt); }
811+
if (compare & UNITY_EQUAL_TO) { UnityPrint(UnityStrOrEqual); }
812+
if (compare == UNITY_NOT_EQUAL) { UnityPrint(UnityStrNotEqual); }
813+
UnityPrintIntNumberByStyle(threshold, style);
814+
UnityAddMsgIfSpecified(msg);
815+
UNITY_FAIL_AND_BAIL;
788816
}
817+
}
818+
819+
void UnityAssertUintGreaterOrLessOrEqualNumber(const UNITY_UINT threshold,
820+
const UNITY_UINT actual,
821+
const UNITY_COMPARISON_T compare,
822+
const char *msg,
823+
const UNITY_LINE_TYPE lineNumber,
824+
const UNITY_DISPLAY_STYLE_T style)
825+
{
826+
int failed = 0;
827+
RETURN_IF_FAIL_OR_IGNORE;
828+
829+
if ((threshold == actual) && !(compare & UNITY_EQUAL_TO)) { failed = 1; }
830+
831+
/* UINT or HEX */
832+
if ((actual > threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
833+
if ((actual < threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
789834

790835
if (failed)
791836
{
792837
UnityTestResultsFailBegin(lineNumber);
793838
UnityPrint(UnityStrExpected);
794-
UnityPrintNumberByStyle(actual, style);
839+
UnityPrintUintNumberByStyle(actual, style);
795840
if (compare & UNITY_GREATER_THAN) { UnityPrint(UnityStrGt); }
796841
if (compare & UNITY_SMALLER_THAN) { UnityPrint(UnityStrLt); }
797842
if (compare & UNITY_EQUAL_TO) { UnityPrint(UnityStrOrEqual); }
798843
if (compare == UNITY_NOT_EQUAL) { UnityPrint(UnityStrNotEqual); }
799-
UnityPrintNumberByStyle(threshold, style);
844+
UnityPrintUintNumberByStyle(threshold, style);
800845
UnityAddMsgIfSpecified(msg);
801846
UNITY_FAIL_AND_BAIL;
802847
}
@@ -910,9 +955,9 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
910955
UnityPrint(UnityStrElement);
911956
UnityPrintNumberUnsigned(num_elements - elements - 1);
912957
UnityPrint(UnityStrExpected);
913-
UnityPrintNumberByStyle(expect_val, style);
958+
UnityPrintIntNumberByStyle(expect_val, style);
914959
UnityPrint(UnityStrWas);
915-
UnityPrintNumberByStyle(actual_val, style);
960+
UnityPrintIntNumberByStyle(actual_val, style);
916961
UnityAddMsgIfSpecified(msg);
917962
UNITY_FAIL_AND_BAIL;
918963
}
@@ -1410,47 +1455,65 @@ void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual,
14101455
#endif /* not UNITY_EXCLUDE_DOUBLE */
14111456

14121457
/*-----------------------------------------------*/
1413-
void UnityAssertNumbersWithin(const UNITY_UINT delta,
1414-
const UNITY_INT expected,
1415-
const UNITY_INT actual,
1416-
const char* msg,
1417-
const UNITY_LINE_TYPE lineNumber,
1418-
const UNITY_DISPLAY_STYLE_T style)
1458+
void UnityAssertIntNumbersWithin(const UNITY_UINT delta,
1459+
const UNITY_INT expected,
1460+
const UNITY_INT actual,
1461+
const char* msg,
1462+
const UNITY_LINE_TYPE lineNumber,
1463+
const UNITY_DISPLAY_STYLE_T style)
14191464
{
14201465
RETURN_IF_FAIL_OR_IGNORE;
14211466

1422-
if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
1467+
if (actual > expected)
14231468
{
1424-
if (actual > expected)
1425-
{
1426-
Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta);
1427-
}
1428-
else
1429-
{
1430-
Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta);
1431-
}
1469+
Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta);
14321470
}
14331471
else
14341472
{
1435-
if ((UNITY_UINT)actual > (UNITY_UINT)expected)
1436-
{
1437-
Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta);
1438-
}
1439-
else
1440-
{
1441-
Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta);
1442-
}
1473+
Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta);
1474+
}
1475+
1476+
if (Unity.CurrentTestFailed)
1477+
{
1478+
UnityTestResultsFailBegin(lineNumber);
1479+
UnityPrint(UnityStrDelta);
1480+
UnityPrintIntNumberByStyle((UNITY_INT)delta, style);
1481+
UnityPrint(UnityStrExpected);
1482+
UnityPrintIntNumberByStyle(expected, style);
1483+
UnityPrint(UnityStrWas);
1484+
UnityPrintIntNumberByStyle(actual, style);
1485+
UnityAddMsgIfSpecified(msg);
1486+
UNITY_FAIL_AND_BAIL;
1487+
}
1488+
}
1489+
1490+
void UnityAssertUintNumbersWithin(const UNITY_UINT delta,
1491+
const UNITY_UINT expected,
1492+
const UNITY_UINT actual,
1493+
const char* msg,
1494+
const UNITY_LINE_TYPE lineNumber,
1495+
const UNITY_DISPLAY_STYLE_T style)
1496+
{
1497+
RETURN_IF_FAIL_OR_IGNORE;
1498+
1499+
if (actual > expected)
1500+
{
1501+
Unity.CurrentTestFailed = ((actual - expected) > delta);
1502+
}
1503+
else
1504+
{
1505+
Unity.CurrentTestFailed = ((expected - actual) > delta);
14431506
}
14441507

14451508
if (Unity.CurrentTestFailed)
14461509
{
14471510
UnityTestResultsFailBegin(lineNumber);
14481511
UnityPrint(UnityStrDelta);
1449-
UnityPrintNumberByStyle((UNITY_INT)delta, style);
1512+
UnityPrintUintNumberByStyle(delta, style);
14501513
UnityPrint(UnityStrExpected);
1451-
UnityPrintNumberByStyle(expected, style);
1514+
UnityPrintUintNumberByStyle(expected, style);
14521515
UnityPrint(UnityStrWas);
1453-
UnityPrintNumberByStyle(actual, style);
1516+
UnityPrintUintNumberByStyle(actual, style);
14541517
UnityAddMsgIfSpecified(msg);
14551518
UNITY_FAIL_AND_BAIL;
14561519
}
@@ -1601,13 +1664,13 @@ void UnityAssertNumbersArrayWithin(const UNITY_UINT delta,
16011664
}
16021665
UnityTestResultsFailBegin(lineNumber);
16031666
UnityPrint(UnityStrDelta);
1604-
UnityPrintNumberByStyle((UNITY_INT)delta, style);
1667+
UnityPrintIntNumberByStyle((UNITY_INT)delta, style);
16051668
UnityPrint(UnityStrElement);
16061669
UnityPrintNumberUnsigned(num_elements - elements - 1);
16071670
UnityPrint(UnityStrExpected);
1608-
UnityPrintNumberByStyle(expect_val, style);
1671+
UnityPrintIntNumberByStyle(expect_val, style);
16091672
UnityPrint(UnityStrWas);
1610-
UnityPrintNumberByStyle(actual_val, style);
1673+
UnityPrintIntNumberByStyle(actual_val, style);
16111674
UnityAddMsgIfSpecified(msg);
16121675
UNITY_FAIL_AND_BAIL;
16131676
}
@@ -1838,9 +1901,9 @@ void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected,
18381901
UnityPrint(UnityStrByte);
18391902
UnityPrintNumberUnsigned(length - bytes - 1);
18401903
UnityPrint(UnityStrExpected);
1841-
UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
1904+
UnityPrintIntNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
18421905
UnityPrint(UnityStrWas);
1843-
UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
1906+
UnityPrintIntNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
18441907
UnityAddMsgIfSpecified(msg);
18451908
UNITY_FAIL_AND_BAIL;
18461909
}

0 commit comments

Comments
 (0)