From 0f832d3052fa203ef5fe898fa0ea360addf1215d Mon Sep 17 00:00:00 2001 From: "rakesh.pothengil" Date: Thu, 18 Jul 2024 16:07:12 +0900 Subject: [PATCH 1/7] Fix for issue: 646 --- .../test/rules/M0-1-10/test_main_variant.cpp | 44 +++++++++++++++++++ .../cpp/EncapsulatingFunctions.qll | 5 ++- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 cpp/autosar/test/rules/M0-1-10/test_main_variant.cpp diff --git a/cpp/autosar/test/rules/M0-1-10/test_main_variant.cpp b/cpp/autosar/test/rules/M0-1-10/test_main_variant.cpp new file mode 100644 index 0000000000..ccc38c95cc --- /dev/null +++ b/cpp/autosar/test/rules/M0-1-10/test_main_variant.cpp @@ -0,0 +1,44 @@ +#include + +// @brief func1 +// @return exit code +int32_t func1(void) noexcept { // COMPLIANT + int32_t x; // CAUTION: uninitialized!! + int32_t ret; + ret = func2(x); + return ret; +} + +// @brief func2 +// @param arg parameter +// @return exit code +int32_t func2(const int32_t arg) // COMPLIANT +{ + int32_t ret; + ret = arg * arg; + return ret; +} + +namespace mains { + static int32_t var; + + // @brief namespace_func + static void namespace_func(void) noexcept { // COMPLIANT + mains::var = -1; + return; + } +} // namespace + +// @brief main +// @return exit code +int32_t main(void) { + int32_t ret {0}; + try { + ret = func1(); + mains::var += ret; + } + catch(...) { + mains::namespace_func(); + } + return ret; +} diff --git a/cpp/common/src/codingstandards/cpp/EncapsulatingFunctions.qll b/cpp/common/src/codingstandards/cpp/EncapsulatingFunctions.qll index d8d9739033..5492f95041 100644 --- a/cpp/common/src/codingstandards/cpp/EncapsulatingFunctions.qll +++ b/cpp/common/src/codingstandards/cpp/EncapsulatingFunctions.qll @@ -14,7 +14,10 @@ abstract class EncapsulatingFunction extends Function { } class MainFunction extends MainLikeFunction { MainFunction() { hasGlobalName("main") and - getType() instanceof IntType + ( + getType() instanceof IntType or + getType() instanceof Int32_t + ) } } From 8c28f1e35ca565013883be1717f07183768537b6 Mon Sep 17 00:00:00 2001 From: "rakesh.pothengil" Date: Tue, 23 Jul 2024 11:24:01 +0900 Subject: [PATCH 2/7] Added tests and generalized fix --- .../rules/M0-1-10.1/UnusedFunction.expected | 0 .../test/rules/M0-1-10.1/UnusedFunction.qlref | 1 + cpp/autosar/test/rules/M0-1-10.1/test.cpp | 34 +++++++++++++++++++ .../cpp/EncapsulatingFunctions.qll | 5 +-- .../typedefint/MainLikeFunction.expected | 1 + .../typedefint/MainLikeFunction.ql | 5 +++ .../cpp/mainlikefunctions/typedefint/test.cpp | 8 +++++ 7 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.expected create mode 100644 cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.qlref create mode 100644 cpp/autosar/test/rules/M0-1-10.1/test.cpp create mode 100644 cpp/common/test/library/codingstandards/cpp/mainlikefunctions/typedefint/MainLikeFunction.expected create mode 100644 cpp/common/test/library/codingstandards/cpp/mainlikefunctions/typedefint/MainLikeFunction.ql create mode 100644 cpp/common/test/library/codingstandards/cpp/mainlikefunctions/typedefint/test.cpp diff --git a/cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.expected b/cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.expected new file mode 100644 index 0000000000..e69de29bb2 diff --git a/cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.qlref b/cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.qlref new file mode 100644 index 0000000000..519660f289 --- /dev/null +++ b/cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.qlref @@ -0,0 +1 @@ +rules/M0-1-10/UnusedFunction.ql \ No newline at end of file diff --git a/cpp/autosar/test/rules/M0-1-10.1/test.cpp b/cpp/autosar/test/rules/M0-1-10.1/test.cpp new file mode 100644 index 0000000000..f85bf8bfe7 --- /dev/null +++ b/cpp/autosar/test/rules/M0-1-10.1/test.cpp @@ -0,0 +1,34 @@ +#include + +namespace mains { +static std::int32_t var; + +// @brief namespace_func +static void +namespace_func(void) noexcept { // COMPLIANT: Called from "main" below. + mains::var = -1; + return; +} +} // namespace mains + +std::int32_t func2() // COMPLIANT: Called from func1 +{ + return mains::var + 20; +} + +std::int32_t func1() { // COMPLIANT: Called from main + return mains::var + func2(); // func2 called here. +} + +// @brief main +// @return exit code +std::int32_t main(void) { + std::int32_t ret{0}; + try { + ret = func1(); // func1 called here. + mains::var += ret; + } catch (...) { + mains::namespace_func(); // namespace_func called here. + } + return ret; +} diff --git a/cpp/common/src/codingstandards/cpp/EncapsulatingFunctions.qll b/cpp/common/src/codingstandards/cpp/EncapsulatingFunctions.qll index 5492f95041..f82705e2c7 100644 --- a/cpp/common/src/codingstandards/cpp/EncapsulatingFunctions.qll +++ b/cpp/common/src/codingstandards/cpp/EncapsulatingFunctions.qll @@ -14,10 +14,7 @@ abstract class EncapsulatingFunction extends Function { } class MainFunction extends MainLikeFunction { MainFunction() { hasGlobalName("main") and - ( - getType() instanceof IntType or - getType() instanceof Int32_t - ) + getType().resolveTypedefs() instanceof IntType } } diff --git a/cpp/common/test/library/codingstandards/cpp/mainlikefunctions/typedefint/MainLikeFunction.expected b/cpp/common/test/library/codingstandards/cpp/mainlikefunctions/typedefint/MainLikeFunction.expected new file mode 100644 index 0000000000..fa98ca7648 --- /dev/null +++ b/cpp/common/test/library/codingstandards/cpp/mainlikefunctions/typedefint/MainLikeFunction.expected @@ -0,0 +1 @@ +| test.cpp:5:9:5:12 | main | diff --git a/cpp/common/test/library/codingstandards/cpp/mainlikefunctions/typedefint/MainLikeFunction.ql b/cpp/common/test/library/codingstandards/cpp/mainlikefunctions/typedefint/MainLikeFunction.ql new file mode 100644 index 0000000000..ed1757631e --- /dev/null +++ b/cpp/common/test/library/codingstandards/cpp/mainlikefunctions/typedefint/MainLikeFunction.ql @@ -0,0 +1,5 @@ +import cpp +import codingstandards.cpp.EncapsulatingFunctions + +from MainFunction m +select m diff --git a/cpp/common/test/library/codingstandards/cpp/mainlikefunctions/typedefint/test.cpp b/cpp/common/test/library/codingstandards/cpp/mainlikefunctions/typedefint/test.cpp new file mode 100644 index 0000000000..7b514505f1 --- /dev/null +++ b/cpp/common/test/library/codingstandards/cpp/mainlikefunctions/typedefint/test.cpp @@ -0,0 +1,8 @@ +typedef signed int int32_t; + +// @brief main +// @return exit code +int32_t main(void) { + int32_t ret{0}; + return ret; +} From 9f88e856ba7c8488bea0a0921b940fafce496790 Mon Sep 17 00:00:00 2001 From: "rakesh.pothengil" Date: Tue, 23 Jul 2024 12:50:45 +0900 Subject: [PATCH 3/7] Added change note for #646 --- change_notes/2024-07-23-fix-fp-646-M0-1-10.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 change_notes/2024-07-23-fix-fp-646-M0-1-10.md diff --git a/change_notes/2024-07-23-fix-fp-646-M0-1-10.md b/change_notes/2024-07-23-fix-fp-646-M0-1-10.md new file mode 100644 index 0000000000..8854c7b59a --- /dev/null +++ b/change_notes/2024-07-23-fix-fp-646-M0-1-10.md @@ -0,0 +1,2 @@ +- `M0-1-10` - `EncapsulatingFunctions.qll`: + - Fixes #646. Consider typedef'd `int` return types for `main()` function as MainFunction. From aed7701d65f7e085cb53c1be83c0cc7b2512a373 Mon Sep 17 00:00:00 2001 From: Rakesh Pothengil <122329100+rak3-sh@users.noreply.github.com> Date: Wed, 24 Jul 2024 05:00:54 +0900 Subject: [PATCH 4/7] Delete cpp/autosar/test/rules/M0-1-10/test_main_variant.cpp --- .../test/rules/M0-1-10/test_main_variant.cpp | 44 ------------------- 1 file changed, 44 deletions(-) delete mode 100644 cpp/autosar/test/rules/M0-1-10/test_main_variant.cpp diff --git a/cpp/autosar/test/rules/M0-1-10/test_main_variant.cpp b/cpp/autosar/test/rules/M0-1-10/test_main_variant.cpp deleted file mode 100644 index ccc38c95cc..0000000000 --- a/cpp/autosar/test/rules/M0-1-10/test_main_variant.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include - -// @brief func1 -// @return exit code -int32_t func1(void) noexcept { // COMPLIANT - int32_t x; // CAUTION: uninitialized!! - int32_t ret; - ret = func2(x); - return ret; -} - -// @brief func2 -// @param arg parameter -// @return exit code -int32_t func2(const int32_t arg) // COMPLIANT -{ - int32_t ret; - ret = arg * arg; - return ret; -} - -namespace mains { - static int32_t var; - - // @brief namespace_func - static void namespace_func(void) noexcept { // COMPLIANT - mains::var = -1; - return; - } -} // namespace - -// @brief main -// @return exit code -int32_t main(void) { - int32_t ret {0}; - try { - ret = func1(); - mains::var += ret; - } - catch(...) { - mains::namespace_func(); - } - return ret; -} From d0807db3b6d63e0fea88ff8c0a1a62a71c033253 Mon Sep 17 00:00:00 2001 From: "rakesh.pothengil" Date: Wed, 24 Jul 2024 09:56:32 +0900 Subject: [PATCH 5/7] Corrected formatting --- cpp/autosar/test/rules/M0-1-10.1/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/autosar/test/rules/M0-1-10.1/test.cpp b/cpp/autosar/test/rules/M0-1-10.1/test.cpp index f85bf8bfe7..272f295a35 100644 --- a/cpp/autosar/test/rules/M0-1-10.1/test.cpp +++ b/cpp/autosar/test/rules/M0-1-10.1/test.cpp @@ -16,7 +16,7 @@ std::int32_t func2() // COMPLIANT: Called from func1 return mains::var + 20; } -std::int32_t func1() { // COMPLIANT: Called from main +std::int32_t func1() { // COMPLIANT: Called from main return mains::var + func2(); // func2 called here. } From 0c200662d4ccb8de83792676e9d2f3d0030d7cd2 Mon Sep 17 00:00:00 2001 From: "rakesh.pothengil" Date: Fri, 26 Jul 2024 09:14:17 +0900 Subject: [PATCH 6/7] Add a non-compliant case. --- cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.expected | 1 + cpp/autosar/test/rules/M0-1-10.1/test.cpp | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.expected b/cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.expected index e69de29bb2..3f58065520 100644 --- a/cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.expected +++ b/cpp/autosar/test/rules/M0-1-10.1/UnusedFunction.expected @@ -0,0 +1 @@ +| test.cpp:23:14:23:26 | uncalled_func | Function uncalled_func is never called. | diff --git a/cpp/autosar/test/rules/M0-1-10.1/test.cpp b/cpp/autosar/test/rules/M0-1-10.1/test.cpp index 272f295a35..6c176f95d1 100644 --- a/cpp/autosar/test/rules/M0-1-10.1/test.cpp +++ b/cpp/autosar/test/rules/M0-1-10.1/test.cpp @@ -20,6 +20,11 @@ std::int32_t func1() { // COMPLIANT: Called from main return mains::var + func2(); // func2 called here. } +std::int32_t uncalled_func() // NON COMPLIANT: Not called. +{ + return mains::var + func1(); // func1 called here. +} + // @brief main // @return exit code std::int32_t main(void) { From 6a1b283118eccb5c386eff6ebbacb6dbc72397f9 Mon Sep 17 00:00:00 2001 From: "rakesh.pothengil" Date: Fri, 26 Jul 2024 09:16:21 +0900 Subject: [PATCH 7/7] Modify comment to follow standard convention. --- cpp/autosar/test/rules/M0-1-10.1/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/autosar/test/rules/M0-1-10.1/test.cpp b/cpp/autosar/test/rules/M0-1-10.1/test.cpp index 6c176f95d1..5b9c68a827 100644 --- a/cpp/autosar/test/rules/M0-1-10.1/test.cpp +++ b/cpp/autosar/test/rules/M0-1-10.1/test.cpp @@ -20,7 +20,7 @@ std::int32_t func1() { // COMPLIANT: Called from main return mains::var + func2(); // func2 called here. } -std::int32_t uncalled_func() // NON COMPLIANT: Not called. +std::int32_t uncalled_func() // NON_COMPLIANT: Not called. { return mains::var + func1(); // func1 called here. }