mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2025-12-17 22:08:07 +08:00
Fix for overflow issue in UnityAssertNumbersWithin()
Make subtraction result unsigned, change prototype & casts in internals. If "actual - expected" overflowed, it wrapped to a negative number, but would fit in an unsigned type, example is INT_MAX - (-1) = INT_MIN For correctness, 'delta' should be unsigned too. Passing in a negative number always passed. The delta can be between INT_MAX & UINT_MAX.
This commit is contained in:
12
src/unity.c
12
src/unity.c
@@ -956,7 +956,7 @@ void UnityAssertDoubleSpecial(const _UD actual,
|
||||
#endif // not UNITY_EXCLUDE_DOUBLE
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityAssertNumbersWithin( const _U_SINT delta,
|
||||
void UnityAssertNumbersWithin( const _U_UINT delta,
|
||||
const _U_SINT expected,
|
||||
const _U_SINT actual,
|
||||
const char* msg,
|
||||
@@ -968,23 +968,23 @@ void UnityAssertNumbersWithin( const _U_SINT delta,
|
||||
if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
|
||||
{
|
||||
if (actual > expected)
|
||||
Unity.CurrentTestFailed = ((actual - expected) > delta);
|
||||
Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > delta);
|
||||
else
|
||||
Unity.CurrentTestFailed = ((expected - actual) > delta);
|
||||
Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > delta);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((_U_UINT)actual > (_U_UINT)expected)
|
||||
Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > (_U_UINT)delta);
|
||||
Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > delta);
|
||||
else
|
||||
Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > (_U_UINT)delta);
|
||||
Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > delta);
|
||||
}
|
||||
|
||||
if (Unity.CurrentTestFailed)
|
||||
{
|
||||
UnityTestResultsFailBegin(lineNumber);
|
||||
UnityPrint(UnityStrDelta);
|
||||
UnityPrintNumberByStyle(delta, style);
|
||||
UnityPrintNumberByStyle((_U_SINT)delta, style);
|
||||
UnityPrint(UnityStrExpected);
|
||||
UnityPrintNumberByStyle(expected, style);
|
||||
UnityPrint(UnityStrWas);
|
||||
|
||||
Reference in New Issue
Block a user