Skip to content

Commit c6bcdf9

Browse files
committed
Improve fix for FP for issue 216
1 parent 507d34e commit c6bcdf9

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

cpp/autosar/src/rules/A5-1-1/LiteralValueUsedOutsideTypeInit.ql

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,6 @@ import cpp
1818
import codingstandards.cpp.autosar
1919
import codingstandards.cpp.LoggingOperation
2020
import codingstandards.cpp.Literals
21-
import codingstandards.cpp.standardlibrary.FileStreams
22-
23-
/**
24-
* In a wrapper `Function`, all accesses of all `Parameters`
25-
* are in located in logging or stream calls
26-
*/
27-
class LoggerOrStreamWrapperFunction extends Function {
28-
LoggerOrStreamWrapperFunction() {
29-
forall(Parameter p | p.getFunction() = this |
30-
forall(VariableAccess va | va = p.getAnAccess() |
31-
(
32-
any(FileStreamFunctionCall fc).getAnArgument().getAChild*() = va
33-
or
34-
any(LoggingOperation logOp).getALoggedExpr().getAChild*() = va
35-
)
36-
)
37-
)
38-
}
39-
}
4021

4122
from Literal l
4223
where
@@ -45,11 +26,8 @@ where
4526
not exists(ConstructorCall cc | cc.getAnArgument() = l) and
4627
not exists(ConstructorFieldInit cf | cf.getExpr() = l) and
4728
not l = any(LoggingOperation logOp).getALoggedExpr().getAChild*() and
48-
not l = any(FileStreamFunctionCall fsc).getAnArgument().getAChild*() and
49-
// Exclude arguments to wrapper functions
50-
not exists(FunctionCall fc, LoggerOrStreamWrapperFunction w |
51-
fc.getAnArgument() = l and w.getACallToThisFunction() = fc
52-
) and
29+
// Exclude Macros with names like *LOG
30+
not exists(MacroInvocation m | m.getMacroName().matches("%LOG") and m.getAnAffectedElement() = l) and
5331
// Exclude literal 0
5432
not l.getValue() = "0" and
5533
// Exclude character literals
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
| test.cpp:5:9:5:25 | constant string | Literal value "constant string" used outside of type initialization StringLiteral |
22
| test.cpp:14:23:14:25 | 100 | Literal value 100 used outside of type initialization Literal |
33
| test.cpp:54:7:54:7 | 1 | Literal value 1 used outside of type initialization Literal |
4-
| test.cpp:75:23:75:28 | test | Literal value "test" used outside of type initialization StringLiteral |
5-
| test.cpp:75:31:75:40 | not okay | Literal value "not okay" used outside of type initialization StringLiteral |

cpp/autosar/test/rules/A5-1-1/test.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ void test_stream_two(std::ostream &os, const char *str,
7272
}
7373

7474
void test_not_wrapper_stream(std::ostream &os, const char *str) noexcept {
75-
test_stream_two(os, "test", "not okay"); // NON_COMPLIANT - test_stream_two is
76-
// not actually exclusively a wrapper
75+
test_stream_two(
76+
os, "test",
77+
"not okay"); // NON_COMPLIANT[FALSE_NEGATIVE] - test_stream_two is
78+
// not actually exclusively a wrapper
7779
}
80+
81+
#define MACRO_LOG(test_str) do \
82+
{\
83+
struct test_struct {\
84+
static const char* get_str(){\
85+
return static_cast<char *>(test_str);\
86+
}\
87+
};\
88+
} while (false)
89+
90+
void f(){
91+
MACRO_LOG("test"); //COMPLIANT - exclusion
92+
}

cpp/common/src/codingstandards/cpp/LoggingOperation.qll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import cpp
22
import semmle.code.cpp.security.OutputWrite
3+
import codingstandards.cpp.standardlibrary.FileStreams
34

45
/**
56
* A operation which may perform logging.
@@ -16,9 +17,36 @@ class OutputWriteLogging extends LoggingOperation, OutputWrite {
1617
override Expr getALoggedExpr() { result = getASource() }
1718
}
1819

20+
/**
21+
* A `FileStreamFunctionCall` operation is considered a log operation for Coding Standards purposes.
22+
*/
23+
class FileStreamLogging extends LoggingOperation, FileStreamFunctionCall {
24+
override Expr getALoggedExpr() { result = getAnArgument() }
25+
26+
override Expr getFStream() { result = this.getQualifier() }
27+
}
28+
1929
/** A call which looks like `printf`. */
2030
class PrintfLikeCall extends LoggingOperation, Call {
2131
PrintfLikeCall() { getTarget().getName().toLowerCase().matches("%printf%") }
2232

2333
override Expr getALoggedExpr() { result = getAnArgument() }
2434
}
35+
36+
/**
37+
* In a wrapper `Function`, all accesses of all `Parameters`
38+
* are in located in logging or stream calls
39+
*/
40+
class LoggerOrStreamWrapperFunction extends Function {
41+
LoggerOrStreamWrapperFunction() {
42+
forall(Parameter p | p.getFunction() = this |
43+
forall(VariableAccess va | va = p.getAnAccess() |
44+
(
45+
any(FileStreamFunctionCall fc).getAnArgument().getAChild*() = va
46+
or
47+
any(LoggingOperation logOp).getALoggedExpr().getAChild*() = va
48+
)
49+
)
50+
)
51+
}
52+
}

0 commit comments

Comments
 (0)