Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Comparison is constant

ID: cs/constant-comparisonKind: problemSecurity severity: Severity: warningPrecision: highTags:   - quality   - reliability   - correctnessQuery suites:   - csharp-security-and-quality.qls

Click to see the query in the CodeQL repository

Comparisons which always yield the same result are unnecessary and may indicate a bug in the logic. This can happen when the data type of one of the operands has a limited range of values. For example unsigned integers are always greater than or equal to zero, andbyte values are always less than 256.

The following expressions always have the same result:

  • Unsigned<0 is always false,

  • 0>Unsigned is always false,

  • 0&le;Unsigned is always true,

  • Unsigned&ge;0 is always true,

  • Unsigned==-1 is always false,

  • Byte<512 is always true.

Recommendation

Examine the logic of the program to determine whether the comparison is necessary. Either change the data types, or remove the unnecessary code.

Example

The following example attempts to count down fromnumberOfOrders to0, however the loop never terminates becauseorder is an unsigned integer and so the conditionorder>=0 is always true.

for(uintorder=numberOfOrders;order>=0;order--)ProcessOrder(order);

The solution is to change the type of the variableorder.

References


[8]ページ先頭

©2009-2025 Movatter.jp