Skip to content

Commit d0c65bd

Browse files
committed
Rule 21.10.2: Detect more complex type use of jmp_buf
1 parent 215a96c commit d0c65bd

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

cpp/misra/src/rules/RULE-21-10-2/NoCsetjmpHeader.ql

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
import cpp
1717
import codingstandards.cpp.misra
1818
import codingstandards.cpp.BannedFunctions
19+
import codingstandards.cpp.types.Uses
1920

2021
class CSetJmpHeader extends Include {
2122
CSetJmpHeader() { this.getIncludeText().regexpMatch("[<\\\"](csetjmp|setjmp.h)[>\\\"]") }
2223
}
2324

24-
class JmpBufVariable extends Variable {
25-
JmpBufVariable() { this.getType().(UserType).hasGlobalOrStdName("jmp_buf") }
25+
class JmpBufType extends UserType {
26+
JmpBufType() { this.hasGlobalOrStdName("jmp_buf") }
2627
}
2728

2829
class LongjmpFunction extends Function {
@@ -39,9 +40,15 @@ where
3940
(
4041
message = "Use of banned header " + element.(CSetJmpHeader).getIncludeText() + "."
4142
or
42-
message =
43-
"Declaration of variable '" + element.(JmpBufVariable).getName() +
44-
"' with banned type 'jmp_buf'."
43+
(
44+
element = getATypeUse(any(JmpBufType jbt)) and
45+
if element instanceof Variable
46+
then
47+
message =
48+
"Declaration of variable '" + element.(Variable).getName() +
49+
"' with banned type 'jmp_buf'."
50+
else message = "Use of banned type 'jmp_buf'."
51+
)
4552
or
4653
message =
4754
element.(BannedFunctions<LongjmpFunction>::Use).getAction() + " banned function '" +

cpp/misra/test/rules/RULE-21-10-2/NoCsetjmpHeader.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
| test.cpp:21:5:21:16 | call to longjmp | Call to banned function 'longjmp'. |
1313
| test.cpp:26:11:26:12 | l1 | Declaration of variable 'l1' with banned type 'jmp_buf'. |
1414
| test.cpp:27:16:27:17 | l2 | Declaration of variable 'l2' with banned type 'jmp_buf'. |
15+
| test.cpp:40:38:40:39 | l1 | Declaration of variable 'l1' with banned type 'jmp_buf'. |

cpp/misra/test/rules/RULE-21-10-2/test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ void test_compliant_alternative() {
3535
} catch (const std::runtime_error &) { // COMPLIANT
3636
// Handle error properly
3737
}
38-
}
38+
}
39+
40+
void test_jmp_buf_usage() { jmp_buf *l1; } // NON_COMPLIANT - pointer to jmp_buf

0 commit comments

Comments
 (0)