Skip to content

Commit 6855686

Browse files
authored
Merge pull request #647 from rak3-sh/rp/m0-1-10-646
Consider "main()" with typedef'd int return type as MainFunction
2 parents 01a73a0 + 6a1b283 commit 6855686

File tree

8 files changed

+58
-1
lines changed

8 files changed

+58
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `M0-1-10` - `EncapsulatingFunctions.qll`:
2+
- Fixes #646. Consider typedef'd `int` return types for `main()` function as MainFunction.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.cpp:23:14:23:26 | uncalled_func | Function uncalled_func is never called. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/M0-1-10/UnusedFunction.ql
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <cstdint>
2+
3+
namespace mains {
4+
static std::int32_t var;
5+
6+
// @brief namespace_func
7+
static void
8+
namespace_func(void) noexcept { // COMPLIANT: Called from "main" below.
9+
mains::var = -1;
10+
return;
11+
}
12+
} // namespace mains
13+
14+
std::int32_t func2() // COMPLIANT: Called from func1
15+
{
16+
return mains::var + 20;
17+
}
18+
19+
std::int32_t func1() { // COMPLIANT: Called from main
20+
return mains::var + func2(); // func2 called here.
21+
}
22+
23+
std::int32_t uncalled_func() // NON_COMPLIANT: Not called.
24+
{
25+
return mains::var + func1(); // func1 called here.
26+
}
27+
28+
// @brief main
29+
// @return exit code
30+
std::int32_t main(void) {
31+
std::int32_t ret{0};
32+
try {
33+
ret = func1(); // func1 called here.
34+
mains::var += ret;
35+
} catch (...) {
36+
mains::namespace_func(); // namespace_func called here.
37+
}
38+
return ret;
39+
}

cpp/common/src/codingstandards/cpp/EncapsulatingFunctions.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract class EncapsulatingFunction extends Function { }
1414
class MainFunction extends MainLikeFunction {
1515
MainFunction() {
1616
hasGlobalName("main") and
17-
getType() instanceof IntType
17+
getType().resolveTypedefs() instanceof IntType
1818
}
1919
}
2020

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.cpp:5:9:5:12 | main |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import cpp
2+
import codingstandards.cpp.EncapsulatingFunctions
3+
4+
from MainFunction m
5+
select m
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
typedef signed int int32_t;
2+
3+
// @brief main
4+
// @return exit code
5+
int32_t main(void) {
6+
int32_t ret{0};
7+
return ret;
8+
}

0 commit comments

Comments
 (0)