diff --git a/rules/c/incorrect-unsigned-comparison.c b/rules/c/incorrect-unsigned-comparison.c index 9666e37..01b4eb3 100644 --- a/rules/c/incorrect-unsigned-comparison.c +++ b/rules/c/incorrect-unsigned-comparison.c @@ -1,6 +1,8 @@ // Marco Ivaldi #include +#include +#include int bad1() { @@ -35,6 +37,29 @@ int good1() return 0; } +struct Context { + uint64_t l_qseq; +}; + +int bad_mixed(struct Context *c, int new_l_data) +{ + // ruleid: raptor-incorrect-unsigned-comparison + if (new_l_data > INT_MAX || (uint64_t)c->l_qseq < 0) + return 1; + + return 0; +} + +int good2() +{ + size_t uvar; + // ok + if ((int32_t)uvar >= 0) + return 1; + + return 0; +} + int main() { printf("Hello, World!"); diff --git a/rules/c/incorrect-unsigned-comparison.yaml b/rules/c/incorrect-unsigned-comparison.yaml index 549c7e8..ad4d5ae 100644 --- a/rules/c/incorrect-unsigned-comparison.yaml +++ b/rules/c/incorrect-unsigned-comparison.yaml @@ -26,27 +26,13 @@ rules: languages: - c - cpp - # NOTE: some types are not covered. # NOTE: incorrect unsigned assigments are not covered. - pattern-either: - # < (always false) - - pattern: (unsigned short $_) < 0 - - pattern: (unsigned short int $_) < 0 - - pattern: (unsigned int $_) < 0 - - pattern: (unsigned long $_) < 0 - - pattern: (unsigned long int $_) < 0 - - pattern: (size_t $_) < 0 - # <= - - pattern: (unsigned short $_) <= 0 - - pattern: (unsigned short int $_) <= 0 - - pattern: (unsigned int $_) <= 0 - - pattern: (unsigned long $_) <= 0 - - pattern: (unsigned long int $_) <= 0 - - pattern: (size_t $_) <= 0 - # >= (always true) - - pattern: (unsigned short $_) >= 0 - - pattern: (unsigned short int $_) >= 0 - - pattern: (unsigned int $_) >= 0 - - pattern: (unsigned long $_) >= 0 - - pattern: (unsigned long int $_) >= 0 - - pattern: (size_t $_) >= 0 + patterns: + - pattern-either: + - pattern: ($UT $E) < $BEZ + - pattern: ($UT $E) <= $BEZ + - pattern: ($UT $E) >= $BEZ + - pattern-not: ($ST) ($UT $E) >= 0 + - metavariable-regex: + metavariable: $UT + regex: unsigned\s+(char|short|short\s+int|int|long|long\s+int)|uint(8|16|32|64)_t|size_t|uintptr_t