CodeQL documentation

Self comparison

ID: cpp/comparison-of-identical-expressions
Kind: problem
Security severity: 
Severity: warning
Precision: high
Tags:
   - readability
   - maintainability
Query suites:
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

The purpose of comparing a variable to itself is usually to detect either integer overflow or floating point NaN. If the comparison does neither then it is most likely a coding error.

Recommendation

If the purpose of the cast is to detect integer overflow, then make sure that the comparison uses explicit casts and that the types are as intended.

Example

typedef int T;

bool checkOverflow(T x) {
  return (x == (int)x);  // Always returns true.
}

The comparison is always true, because the explicit cast to int has no effect. This might mean that the definition of T has changed since checkOverflow was written and that checkOverflow is now obsolete.

  • © GitHub, Inc.
  • Terms
  • Privacy
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy