Skip to content

PRE32-C : Does not detect presence of preprocessor directives in function calls correctly #654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions c/cert/src/rules/PRE32-C/MacroOrFunctionArgsContainHashToken.ql
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,38 @@ predicate isFunctionSuccessorLocation(ControlFlowNode node, File f, int endline)
PreprocessorDirective isLocatedInAFunctionInvocation(FunctionCall c) {
exists(PreprocessorDirective p, File f, int startCall, int endCall |
isFunctionInvocationLocation(c, f, startCall, endCall) and
exists(int startLine, int endLine | isPreprocDirectiveLine(p, f, startLine, endLine) |
startCall < startLine and
startCall < endLine and
endLine <= endCall and
endLine <= endCall
exists(Expr arg, int preprocStartLine, int preprocEndLine |
c.getAnArgument() = arg and
isPreprocDirectiveLine(p, f, preprocStartLine, preprocEndLine) and
// function call begins before preprocessor directive
startCall < preprocStartLine and
(
// argument's location is after the preprocessor directive
arg.getLocation().getStartLine() > preprocStartLine
or
// arg's location is before an endif token that is part of a
// preprocessor directive defined before the argument.
// E.g.
// memcpy(dest, src,
// #ifdef SOMEMACRO
// 12
// #else
// 24 // 'arg' exists here
// #endif // endif after 'arg', but part of a preproc. branch before 'arg'
// );
p instanceof PreprocessorEndif and
// exists a preprocessor branch of which this is the endif
// and that preprocessor directive exists before
// the argument and after the function call begins.
exists(PreprocessorBranchDirective another |
another.getEndIf() = p and
another.getLocation().getFile() = f and
startCall < another.getLocation().getStartLine() and
arg.getLocation().getStartLine() > another.getLocation().getStartLine()
)
) and
// function call ends after preprocessor directive
endCall > preprocEndLine
) and
result = p
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@
| 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. |
| test.c:22:1:22:5 | #else | Invocation of function memcpy includes a token "#else" that could be confused for an argument preprocessor directive. |
| test.c:24:1:24:6 | #endif | Invocation of function memcpy includes a token "#endif" that could be confused for an argument preprocessor directive. |
| 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. |
| test.c:28:1:28:6 | #endif | Invocation of function memcpy includes a token "#endif" that could be confused for an argument preprocessor directive. |
6 changes: 3 additions & 3 deletions c/cert/test/rules/PRE32-C/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ void func(const char *src) {
#endif // NON_COMPLIANT
);

#if TEST // COMPLIANT[FALSE_POSITIVE]
#endif // COMPLIANT[FALSE_POSITIVE]
}
#if TEST // COMPLIANT
#endif // COMPLIANT
}
2 changes: 2 additions & 0 deletions change_notes/2024-07-30-fix-fp-650-PRE32-C.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `PRE32-C` - `MacroOrFunctionArgsContainHashToken.ql`:
- Fixes #650. Correctly identifies presence of preprocessor directives in function calls.
Loading