File tree Expand file tree Collapse file tree 4 files changed +91
-0
lines changed Expand file tree Collapse file tree 4 files changed +91
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @id cpp/misra/banned-system-function
3
+ * @name RULE-21-2-3: The library function system from <cstdlib> shall not be used
4
+ * @description Using the system() function from cstdlib or stdlib.h causes undefined behavior and
5
+ * potential security vulnerabilities.
6
+ * @kind problem
7
+ * @precision very-high
8
+ * @problem.severity error
9
+ * @tags external/misra/id/rule-21-2-3
10
+ * scope/single-translation-unit
11
+ * external/misra/enforcement/decidable
12
+ * external/misra/obligation/required
13
+ */
14
+
15
+ import cpp
16
+ import codingstandards.cpp.misra
17
+ import codingstandards.cpp.BannedFunctions
18
+
19
+ class SystemFunction extends Function {
20
+ SystemFunction ( ) { this .hasGlobalName ( "system" ) or this .hasQualifiedName ( "std" , "system" ) }
21
+ }
22
+
23
+ from Element element , string message
24
+ where
25
+ not isExcluded ( element , BannedAPIsPackage:: bannedSystemFunctionQuery ( ) ) and
26
+ (
27
+ element instanceof BannedFunctions< SystemFunction > :: Use and
28
+ message =
29
+ element .( BannedFunctions< SystemFunction > :: Use ) .getAction ( ) + " banned function '" +
30
+ element .( BannedFunctions< SystemFunction > :: Use ) .getFunctionName ( ) + "'."
31
+ or
32
+ element instanceof MacroInvocation and
33
+ element .( MacroInvocation ) .getMacroName ( ) = "system" and
34
+ message = "Use of banned macro 'system'."
35
+ )
36
+ select element , message
Original file line number Diff line number Diff line change
1
+ | test.cpp:4:3:4:13 | call to system | Call to banned function 'system'. |
2
+ | test.cpp:8:14:8:24 | system | Address taken for banned function 'system'. |
3
+ | test.cpp:9:29:9:39 | system | Address taken for banned function 'system'. |
4
+ | test.cpp:13:40:13:50 | system | Address taken for banned function 'system'. |
5
+ | test.cpp:17:3:17:13 | call to system | Call to banned function 'system'. |
6
+ | test.cpp:22:3:22:13 | call to system | Call to banned function 'system'. |
7
+ | test.cpp:35:3:35:8 | call to system | Call to banned function 'system'. |
8
+ | test.cpp:39:29:39:34 | system | Address taken for banned function 'system'. |
9
+ | test.cpp:44:3:44:21 | system(x) | Use of banned macro 'system'. |
Original file line number Diff line number Diff line change
1
+ rules/RULE-21-2-3/BannedSystemFunction.ql
Original file line number Diff line number Diff line change
1
+ #include < cstdlib>
2
+
3
+ void test_direct_call_to_system () {
4
+ std::system (" echo hello" ); // NON_COMPLIANT
5
+ }
6
+
7
+ void test_system_function_pointer () {
8
+ auto l1 = &std::system; // NON_COMPLIANT
9
+ int (*l2)(const char *) = std::system; // NON_COMPLIANT
10
+ }
11
+
12
+ void test_system_address_taken () {
13
+ void *l1 = reinterpret_cast <void *>(&std::system); // NON_COMPLIANT
14
+ }
15
+
16
+ void test_system_call_with_null () {
17
+ std::system (nullptr ); // NON_COMPLIANT
18
+ }
19
+
20
+ void test_system_call_with_variable () {
21
+ const char *l1 = " ls" ;
22
+ std::system (l1); // NON_COMPLIANT
23
+ }
24
+
25
+ void test_compliant_alternative () {
26
+ // Using compliant alternatives instead of system()
27
+ const char *l1 = " some command" ; // COMPLIANT
28
+ // Implementation-specific alternatives would be used here
29
+ }
30
+
31
+ // Test with C-style header (rule also applies to <stdlib.h>)
32
+ #include < stdlib.h>
33
+
34
+ void test_c_style_header_system () {
35
+ system (" echo hello" ); // NON_COMPLIANT
36
+ }
37
+
38
+ void test_c_style_header_function_pointer () {
39
+ int (*l1)(const char *) = system; // NON_COMPLIANT
40
+ }
41
+
42
+ #define system (x ) 0
43
+ void test_system_macro_expansion () {
44
+ system (" echo test" ); // NON_COMPLIANT
45
+ }
You can’t perform that action at this time.
0 commit comments