From 8b3924453595b267fe218c0ad7af3442c471d0d2 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Thu, 18 Jan 2024 14:40:37 -0800 Subject: [PATCH 1/3] Include the literal `0` in `OctalLiteral` --- cpp/common/src/codingstandards/cpp/Cpp14Literal.qll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/common/src/codingstandards/cpp/Cpp14Literal.qll b/cpp/common/src/codingstandards/cpp/Cpp14Literal.qll index afc8cb07a3..c3908008ef 100644 --- a/cpp/common/src/codingstandards/cpp/Cpp14Literal.qll +++ b/cpp/common/src/codingstandards/cpp/Cpp14Literal.qll @@ -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" } } From f4b27f46f95f0045a4ba08d27cea59e129a8859e Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Thu, 18 Jan 2024 14:45:25 -0800 Subject: [PATCH 2/3] Exclude non-integer literals --- c/common/src/codingstandards/c/Literals.qll | 4 ++++ .../rules/RULE-7-3/LowercaseCharacterLUsedInLiteralSuffix.ql | 4 ++-- .../cpp/LowercaseCharacterLUsedInLiteralSuffix.expected | 0 .../RULE-7-3/cpp/LowercaseCharacterLUsedInLiteralSuffix.qlref | 1 + c/misra/test/rules/RULE-7-3/cpp/README.md | 1 + c/misra/test/rules/RULE-7-3/cpp/options | 1 + c/misra/test/rules/RULE-7-3/cpp/test.cpp | 1 + c/misra/test/rules/RULE-7-3/test.c | 2 +- change_notes/2024-01-18-fix-reported-fp-for-rule-7-3.md | 2 ++ 9 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 c/common/src/codingstandards/c/Literals.qll create mode 100644 c/misra/test/rules/RULE-7-3/cpp/LowercaseCharacterLUsedInLiteralSuffix.expected create mode 100644 c/misra/test/rules/RULE-7-3/cpp/LowercaseCharacterLUsedInLiteralSuffix.qlref create mode 100644 c/misra/test/rules/RULE-7-3/cpp/README.md create mode 100644 c/misra/test/rules/RULE-7-3/cpp/options create mode 100644 c/misra/test/rules/RULE-7-3/cpp/test.cpp create mode 100644 change_notes/2024-01-18-fix-reported-fp-for-rule-7-3.md diff --git a/c/common/src/codingstandards/c/Literals.qll b/c/common/src/codingstandards/c/Literals.qll new file mode 100644 index 0000000000..58cd283dc6 --- /dev/null +++ b/c/common/src/codingstandards/c/Literals.qll @@ -0,0 +1,4 @@ +// Reuse the `IntegerLiteral` class +import codingstandards.cpp.Cpp14Literal + +class IntegerLiteral = Cpp14Literal::IntegerLiteral; \ No newline at end of file diff --git a/c/misra/src/rules/RULE-7-3/LowercaseCharacterLUsedInLiteralSuffix.ql b/c/misra/src/rules/RULE-7-3/LowercaseCharacterLUsedInLiteralSuffix.ql index 311831d2b8..4fc257578b 100644 --- a/c/misra/src/rules/RULE-7-3/LowercaseCharacterLUsedInLiteralSuffix.ql +++ b/c/misra/src/rules/RULE-7-3/LowercaseCharacterLUsedInLiteralSuffix.ql @@ -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." diff --git a/c/misra/test/rules/RULE-7-3/cpp/LowercaseCharacterLUsedInLiteralSuffix.expected b/c/misra/test/rules/RULE-7-3/cpp/LowercaseCharacterLUsedInLiteralSuffix.expected new file mode 100644 index 0000000000..e69de29bb2 diff --git a/c/misra/test/rules/RULE-7-3/cpp/LowercaseCharacterLUsedInLiteralSuffix.qlref b/c/misra/test/rules/RULE-7-3/cpp/LowercaseCharacterLUsedInLiteralSuffix.qlref new file mode 100644 index 0000000000..464efc3b2f --- /dev/null +++ b/c/misra/test/rules/RULE-7-3/cpp/LowercaseCharacterLUsedInLiteralSuffix.qlref @@ -0,0 +1 @@ +rules/RULE-7-3/LowercaseCharacterLUsedInLiteralSuffix.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-7-3/cpp/README.md b/c/misra/test/rules/RULE-7-3/cpp/README.md new file mode 100644 index 0000000000..b9aa3d6d8f --- /dev/null +++ b/c/misra/test/rules/RULE-7-3/cpp/README.md @@ -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++. \ No newline at end of file diff --git a/c/misra/test/rules/RULE-7-3/cpp/options b/c/misra/test/rules/RULE-7-3/cpp/options new file mode 100644 index 0000000000..8dbed822c6 --- /dev/null +++ b/c/misra/test/rules/RULE-7-3/cpp/options @@ -0,0 +1 @@ +semmle-extractor-options:--clang -std=c++14 --edg --diag_error=implicit_func_decl -nostdinc -I../../../../../cpp/common/test/includes/standard-library \ No newline at end of file diff --git a/c/misra/test/rules/RULE-7-3/cpp/test.cpp b/c/misra/test/rules/RULE-7-3/cpp/test.cpp new file mode 100644 index 0000000000..ba3ca4f14e --- /dev/null +++ b/c/misra/test/rules/RULE-7-3/cpp/test.cpp @@ -0,0 +1 @@ +int x = false; // COMPLIANT - reported as FP in #319 \ No newline at end of file diff --git a/c/misra/test/rules/RULE-7-3/test.c b/c/misra/test/rules/RULE-7-3/test.c index 00a61817aa..5e1c448926 100644 --- a/c/misra/test/rules/RULE-7-3/test.c +++ b/c/misra/test/rules/RULE-7-3/test.c @@ -41,4 +41,4 @@ long d9 = 001LU; // COMPLIANT char *e1 = ""; char *e2 = "ul"; -char *e3 = "UL"; +char *e3 = "UL"; \ No newline at end of file diff --git a/change_notes/2024-01-18-fix-reported-fp-for-rule-7-3.md b/change_notes/2024-01-18-fix-reported-fp-for-rule-7-3.md new file mode 100644 index 0000000000..dea57f1be4 --- /dev/null +++ b/change_notes/2024-01-18-fix-reported-fp-for-rule-7-3.md @@ -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. \ No newline at end of file From b9a63088055b9ec611eaee8c211306e49fc94632 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Thu, 18 Jan 2024 14:56:23 -0800 Subject: [PATCH 3/3] Fix formatting --- c/common/src/codingstandards/c/Literals.qll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c/common/src/codingstandards/c/Literals.qll b/c/common/src/codingstandards/c/Literals.qll index 58cd283dc6..beeeccb8cc 100644 --- a/c/common/src/codingstandards/c/Literals.qll +++ b/c/common/src/codingstandards/c/Literals.qll @@ -1,4 +1,4 @@ // Reuse the `IntegerLiteral` class import codingstandards.cpp.Cpp14Literal -class IntegerLiteral = Cpp14Literal::IntegerLiteral; \ No newline at end of file +class IntegerLiteral = Cpp14Literal::IntegerLiteral;