Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Comparison result is always the same

ID: cpp/constant-comparisonKind: problemSecurity severity: Severity: warningPrecision: highTags:   - maintainability   - readabilityQuery suites:   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

Comparison operations likex>=y orx!=y will always return the same result if the ranges ofx andy do not overlap. In some cases this can cause an infinite loop. In the example below the loop condition on line 9 is always true because the range ofi is [0..5], so the loop will never terminate.

The bounds which were deduced for the left and right operands of the comparison are included in the message as they often make it easier to understand why a result was reported. For example the message for the comparisonx>=y might read: “Comparison is always false because x >= 5 and 3 >= y.”

Recommendation

Check the expression to see whether a different semantics was intended.

Example

intf(){inti;inttotal=0;for(i=0;i<10;i=i+1){// GOOD: comparison could be either true or false.total+=i;}for(i=0;i<10;i=i+1){// BAD: comparison is always true, because i <= 5.i=i%5;total+=i;}returntotal;}

References


[8]ページ先頭

©2009-2025 Movatter.jp