Expr: Warn on count underflow for c -= 1 and c = c - 1

I've skipped treating overflows as warnings, as ++ wrapping around at 0
doesn't currently trigger a runtime error and might be expected to be
quiet and silently wrap.

Closes #2486
This commit is contained in:
Arne Welzel 2022-11-29 15:58:36 +01:00
parent a07b0c333f
commit e48618e244
7 changed files with 67 additions and 2 deletions

View file

@ -888,6 +888,10 @@ ValPtr BinaryExpr::Fold(Val* v1, Val* v2) const
case EXPR_SUB:
case EXPR_REMOVE_FROM:
DO_FOLD(-);
// When subtracting and the result is larger than the left
// operand we mostly likely underflowed and log a warning.
if ( is_unsigned && u3 > u1 )
reporter->ExprRuntimeWarning(this, "count underflow");
break;
case EXPR_TIMES:
DO_FOLD(*);
@ -1447,7 +1451,7 @@ ValPtr IncrExpr::DoSingleEval(Frame* f, Val* v) const
--k;
if ( k < 0 && v->GetType()->InternalType() == TYPE_INTERNAL_UNSIGNED )
RuntimeError("count underflow");
reporter->ExprRuntimeWarning(this, "count underflow");
}
const auto& ret_type = IsVector(GetType()->Tag()) ? GetType()->Yield() : GetType();