Skip to content

Fix uninitvar miss on ternary (?:) condition - #92

Open
watte1997 wants to merge 1 commit into
Tencent:masterfrom
watte1997:fix-uninitvar-ternary-condition
Open

Fix uninitvar miss on ternary (?:) condition#92
watte1997 wants to merge 1 commit into
Tencent:masterfrom
watte1997:fix-uninitvar-ternary-condition

Conversation

@watte1997

@watte1997 watte1997 commented Aug 24, 2026

Copy link
Copy Markdown

Summary

  • Fixes TscanCode无法检测出三目运算符(?:)中的uninitvar错误 #90: uninitvar was 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 in if (Flag) was already detected.
  • Root cause: isVariableUsage() only treated the next token as a use when Token::isOp() was true. ? is eExtendedOp, so it was not recognized as a read; scope analysis then assumed the variable had been assigned and stopped.
  • Change: if the variable is the AST condition of ? (astParent() is ? and astOperand1() is the variable), treat it as a use. Added a sample in samples/cpp/uninitvar.cpp.

Test plan

  • make -C trunk (CLI objects needed extra flags on this environment due to existing SIGSTKSZ / intptr_t issues, unrelated to this change)
  • ./trunk/tscancode --xml samples/cpp/uninitvar.cpp reports Uninitialized variable: flag on the ternary line
  • Existing sample still reports Uninitialized variable: a on a++
  • Please also confirm if (Flag) still reports uninitvar as before

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TscanCode无法检测出三目运算符(?:)中的uninitvar错误

1 participant