You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
#if: ?: treats a zero condition as true unless it is written exactly as 0 #726
constFoldQuestionOp checks the condition by comparing strings (condTok->str() != "0"). A condition that is zero but not written exactly as 0 is taken as true, so the wrong branch is chosen. Examples are 00, -0, and -X
where X is not defined (it folds to -0).
Wrong branch, no division:
#if00 ? 0 : 1yes#elseno#endif
gcc/clang: yes. simplecpp: no. #if -0 ? 0 : 1 gives the same result.
Since #722 the condition also decides which operand is not evaluated, so an error in the branch that should be evaluated is silently dropped:
#if00 ? 1 : 1/0yes#endif
gcc/clang: error, division by zero in #if. simplecpp: yes, no error. The same happens with #if -0 ? 1 : 1/0, and with #if -X ? -1 : (0%0) ? 1 : 2 where X is undefined.
&& and || (constFoldLogicalOp) already use stringToLL(...) != 0 and handle these values correctly. ?: should use the same check, stringToLL(condTok->str()) != 0.
For the fix, test cases for 00 ? 0 : 1, -0 ? 0 : 1, 00 ? 1 : 1/0 (should error) and -X ? 0 : 1 with X undefined would cover it.
Activity
Fix cppcheck-opensource#726 (#if: ?: treats a zero condition as true …