Skip to content

Exclude non integer literals in RULE-7-3 #494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions c/common/src/codingstandards/c/Literals.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Reuse the `IntegerLiteral` class
import codingstandards.cpp.Cpp14Literal

class IntegerLiteral = Cpp14Literal::IntegerLiteral;
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

import cpp
import codingstandards.c.misra
import codingstandards.c.Literals

from Literal l
from IntegerLiteral l
where
not isExcluded(l, SyntaxPackage::lowercaseCharacterLUsedInLiteralSuffixQuery()) and
not l instanceof StringLiteral and
exists(l.getValueText().indexOf("l"))
select l, "Lowercase 'l' used as a literal suffix."
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-7-3/LowercaseCharacterLUsedInLiteralSuffix.ql
1 change: 1 addition & 0 deletions c/misra/test/rules/RULE-7-3/cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This test case was added to validate FP report [#319](https://github.com/github/codeql-coding-standards/issues/319) that occurs when this rule is run on a translation unit with language mode c++.
1 change: 1 addition & 0 deletions c/misra/test/rules/RULE-7-3/cpp/options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
semmle-extractor-options:--clang -std=c++14 --edg --diag_error=implicit_func_decl -nostdinc -I../../../../../cpp/common/test/includes/standard-library
1 change: 1 addition & 0 deletions c/misra/test/rules/RULE-7-3/cpp/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int x = false; // COMPLIANT - reported as FP in #319
2 changes: 1 addition & 1 deletion c/misra/test/rules/RULE-7-3/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ long d9 = 001LU; // COMPLIANT

char *e1 = "";
char *e2 = "ul";
char *e3 = "UL";
char *e3 = "UL";
2 changes: 2 additions & 0 deletions change_notes/2024-01-18-fix-reported-fp-for-rule-7-3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`RULE-7-3`: `c/misra/lowercase-character-l-used-in-literal-suffix`
- Exclude non integer literals. This removes a false positive triggered when analyzing C++ code containing the `false` literal.
2 changes: 1 addition & 1 deletion cpp/common/src/codingstandards/cpp/Cpp14Literal.qll
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Cpp14Literal {
* Octal literals must always start with the digit `0`.
*/
class OctalLiteral extends IntegerLiteral {
OctalLiteral() { getValueText().regexpMatch("\\s*0[0-7']+[uUlL]*\\s*") }
OctalLiteral() { getValueText().regexpMatch("\\s*0[0-7']*[uUlL]*\\s*") }

override string getAPrimaryQlClass() { result = "OctalLiteral" }
}
Expand Down