Fix uninitvar miss on ternary (?:) condition - #92
Open
watte1997 wants to merge 1 commit into
Open
Conversation
isVariableUsage() did not treat a variable appearing as the condition of a ternary operator as a read, because '?' is an eExtendedOp token and is therefore rejected by Token::isOp(). The scope analysis then assumed the variable was assigned and stopped, so `x = flag ? 10 : 0;` was silently accepted while `if (flag)` was correctly reported. Fixes Tencent#90
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
uninitvarwas not reported when an uninitialized local variable was used as the condition of a ternary operator, e.g.RetVal = Flag ? 10 : 0;. The same use inif (Flag)was already detected.isVariableUsage()only treated the next token as a use whenToken::isOp()was true.?iseExtendedOp, so it was not recognized as a read; scope analysis then assumed the variable had been assigned and stopped.?(astParent()is?andastOperand1()is the variable), treat it as a use. Added a sample insamples/cpp/uninitvar.cpp.Test plan
make -C trunk(CLI objects needed extra flags on this environment due to existingSIGSTKSZ/intptr_tissues, unrelated to this change)./trunk/tscancode --xml samples/cpp/uninitvar.cppreportsUninitialized variable: flagon the ternary lineUninitialized variable: aona++if (Flag)still reports uninitvar as before