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:
jsalling
2015-12-20 11:42:43 -06:00
parent 8ca3062ad4
commit 4108319007
2 changed files with 21 additions and 21 deletions

View File

@@ -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);