Skip to content

Commit e03211b

Browse files
authored
Merge pull request #654 from rak3-sh/rp/pre32-c-650
PRE32-C : Does not detect presence of preprocessor directives in function calls correctly
2 parents 95f7a1a + cc72321 commit e03211b

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

c/cert/src/rules/PRE32-C/MacroOrFunctionArgsContainHashToken.ql

+32-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,38 @@ predicate isFunctionSuccessorLocation(ControlFlowNode node, File f, int endline)
3232
PreprocessorDirective isLocatedInAFunctionInvocation(FunctionCall c) {
3333
exists(PreprocessorDirective p, File f, int startCall, int endCall |
3434
isFunctionInvocationLocation(c, f, startCall, endCall) and
35-
exists(int startLine, int endLine | isPreprocDirectiveLine(p, f, startLine, endLine) |
36-
startCall < startLine and
37-
startCall < endLine and
38-
endLine <= endCall and
39-
endLine <= endCall
35+
exists(Expr arg, int preprocStartLine, int preprocEndLine |
36+
c.getAnArgument() = arg and
37+
isPreprocDirectiveLine(p, f, preprocStartLine, preprocEndLine) and
38+
// function call begins before preprocessor directive
39+
startCall < preprocStartLine and
40+
(
41+
// argument's location is after the preprocessor directive
42+
arg.getLocation().getStartLine() > preprocStartLine
43+
or
44+
// arg's location is before an endif token that is part of a
45+
// preprocessor directive defined before the argument.
46+
// E.g.
47+
// memcpy(dest, src,
48+
// #ifdef SOMEMACRO
49+
// 12
50+
// #else
51+
// 24 // 'arg' exists here
52+
// #endif // endif after 'arg', but part of a preproc. branch before 'arg'
53+
// );
54+
p instanceof PreprocessorEndif and
55+
// exists a preprocessor branch of which this is the endif
56+
// and that preprocessor directive exists before
57+
// the argument and after the function call begins.
58+
exists(PreprocessorBranchDirective another |
59+
another.getEndIf() = p and
60+
another.getLocation().getFile() = f and
61+
startCall < another.getLocation().getStartLine() and
62+
arg.getLocation().getStartLine() > another.getLocation().getStartLine()
63+
)
64+
) and
65+
// function call ends after preprocessor directive
66+
endCall > preprocEndLine
4067
) and
4168
result = p
4269
)

c/cert/test/rules/PRE32-C/MacroOrFunctionArgsContainHashToken.expected

-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@
44
| test.c:20:1:20:16 | #ifdef SOMEMACRO | Invocation of function memcpy includes a token "#ifdef SOMEMACRO" that could be confused for an argument preprocessor directive. |
55
| test.c:22:1:22:5 | #else | Invocation of function memcpy includes a token "#else" that could be confused for an argument preprocessor directive. |
66
| test.c:24:1:24:6 | #endif | Invocation of function memcpy includes a token "#endif" that could be confused for an argument preprocessor directive. |
7-
| test.c:27:1:27:8 | #if TEST | Invocation of function memcpy includes a token "#if TEST" that could be confused for an argument preprocessor directive. |
8-
| test.c:28:1:28:6 | #endif | Invocation of function memcpy includes a token "#endif" that could be confused for an argument preprocessor directive. |

c/cert/test/rules/PRE32-C/test.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ void func(const char *src) {
2424
#endif // NON_COMPLIANT
2525
);
2626

27-
#if TEST // COMPLIANT[FALSE_POSITIVE]
28-
#endif // COMPLIANT[FALSE_POSITIVE]
29-
}
27+
#if TEST // COMPLIANT
28+
#endif // COMPLIANT
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `PRE32-C` - `MacroOrFunctionArgsContainHashToken.ql`:
2+
- Fixes #650. Correctly identifies presence of preprocessor directives in function calls.

0 commit comments

Comments
 (0)