From 8f61a6bc9859f5a3b528a8e0797532fc0bbb9d6e Mon Sep 17 00:00:00 2001 From: Luke Cartey Date: Tue, 2 May 2023 12:01:51 +0100 Subject: [PATCH] __func__ excluded from C-style queries Exclude `__func__` from queries that prohibit c-style strings and arrays, as it is the proscribed way to return the function name. --- change_notes/2023-05-02-func-c-style.md | 3 +++ cpp/autosar/src/rules/A18-1-1/CStyleArraysUsed.ql | 4 +++- cpp/autosar/src/rules/A27-0-4/CStyleStringsUsed.ql | 3 ++- cpp/autosar/test/rules/A18-1-1/test.cpp | 2 ++ cpp/autosar/test/rules/A27-0-4/test.cpp | 1 + 5 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 change_notes/2023-05-02-func-c-style.md diff --git a/change_notes/2023-05-02-func-c-style.md b/change_notes/2023-05-02-func-c-style.md new file mode 100644 index 0000000000..46e1710010 --- /dev/null +++ b/change_notes/2023-05-02-func-c-style.md @@ -0,0 +1,3 @@ + * Exclude the use of `__func__` from certain queries, as it is the proscribed way to return the name of the current function: + * `A27-0-4` - Use of the value returned by `__func__` is no longer flagged as a use of C-style strings. + * `A18-1-1` - `__func__` is no longer flagged as a declaration of a variable using C-style arrays. \ No newline at end of file diff --git a/cpp/autosar/src/rules/A18-1-1/CStyleArraysUsed.ql b/cpp/autosar/src/rules/A18-1-1/CStyleArraysUsed.ql index 83d0220c5a..0494e86607 100644 --- a/cpp/autosar/src/rules/A18-1-1/CStyleArraysUsed.ql +++ b/cpp/autosar/src/rules/A18-1-1/CStyleArraysUsed.ql @@ -30,5 +30,7 @@ class StaticConstExprArrayDataMember extends MemberVariable { from Variable v where not isExcluded(v, BannedSyntaxPackage::cStyleArraysUsedQuery()) and - exists(ArrayType a | v.getType() = a | not v instanceof StaticConstExprArrayDataMember) + exists(ArrayType a | v.getType() = a | not v instanceof StaticConstExprArrayDataMember) and + // Exclude the compiler generated __func__ as it is the only way to access the function name information + not v.getName() = "__func__" select v, "Variable " + v.getName() + " declares a c-style array." diff --git a/cpp/autosar/src/rules/A27-0-4/CStyleStringsUsed.ql b/cpp/autosar/src/rules/A27-0-4/CStyleStringsUsed.ql index be8bda1f0b..b24a4a96cf 100644 --- a/cpp/autosar/src/rules/A27-0-4/CStyleStringsUsed.ql +++ b/cpp/autosar/src/rules/A27-0-4/CStyleStringsUsed.ql @@ -36,5 +36,6 @@ where e = any(FunctionCall fc).getArgument(_) and e.getUnspecifiedType().(PointerType).getBaseType*() instanceof CharType ) and - DataFlow::localFlow(DataFlow::exprNode(cs), DataFlow::exprNode(e)) + DataFlow::localFlow(DataFlow::exprNode(cs), DataFlow::exprNode(e)) and + not cs = any(LocalVariable lv | lv.getName() = "__func__").getInitializer().getExpr() select cs, "Usage of C-style string in $@.", e, "expression" diff --git a/cpp/autosar/test/rules/A18-1-1/test.cpp b/cpp/autosar/test/rules/A18-1-1/test.cpp index 21eb783717..90596780d9 100644 --- a/cpp/autosar/test/rules/A18-1-1/test.cpp +++ b/cpp/autosar/test/rules/A18-1-1/test.cpp @@ -10,5 +10,7 @@ int test_c_arrays() { int x[100]; // NON_COMPLIANT constexpr int a[]{0, 1, 2}; // NON_COMPLIANT + + __func__; // COMPLAINT return 0; } \ No newline at end of file diff --git a/cpp/autosar/test/rules/A27-0-4/test.cpp b/cpp/autosar/test/rules/A27-0-4/test.cpp index 10ee885979..e73c37ea64 100644 --- a/cpp/autosar/test/rules/A27-0-4/test.cpp +++ b/cpp/autosar/test/rules/A27-0-4/test.cpp @@ -26,4 +26,5 @@ void f2() { f1(a1); f1(a2); f1(s.c_str()); // NON_COMPLIANT + __func__; } \ No newline at end of file