Skip to content

Commit 12048ae

Browse files
authored
Merge pull request #18408 from jketema/config-silence
C++: Silence alerts coming from CMake test compilation files
2 parents 70a1a64 + 0ce409e commit 12048ae

File tree

13 files changed

+70
-8
lines changed

13 files changed

+70
-8
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: feature
3+
---
4+
* A new abstract class `ConfigurationTestFile` (`semmle.code.cpp.ConfigurationTestFile.ConfigurationTestFile`) was introduced, which represents files created to test the build configuration. A subclass `CmakeTryCompileFile` of `ConfigurationTestFile` was also introduced, which represents files created by CMake to test the build configuration.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Provides classes for identifying files that created to test the
3+
* build configuration. It is often desirable to exclude these files
4+
* from analysis.
5+
*/
6+
7+
import File
8+
9+
/**
10+
* A file created to test the system configuration.
11+
*/
12+
abstract class ConfigurationTestFile extends File { }
13+
14+
/**
15+
* A file created by CMake to test the system configuration.
16+
*/
17+
class CmakeTryCompileFile extends ConfigurationTestFile {
18+
CmakeTryCompileFile() {
19+
exists(Folder folder, Folder parent |
20+
folder = this.getParentContainer() and
21+
parent = folder.getParentContainer()
22+
|
23+
folder.getBaseName().matches("TryCompile-%") and
24+
parent.getBaseName() = "CMakeScratch" and
25+
parent.getParentContainer().getBaseName() = "CMakeFiles"
26+
)
27+
}
28+
}

cpp/ql/src/Best Practices/SloppyGlobal.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
*/
1010

1111
import cpp
12+
import semmle.code.cpp.ConfigurationTestFile
1213

1314
from GlobalVariable gv
1415
where
1516
gv.getName().length() <= 3 and
16-
not gv.isStatic()
17+
not gv.isStatic() and
18+
not gv.getFile() instanceof ConfigurationTestFile // variables in files generated during configuration are likely false positives
1719
select gv,
1820
"Poor global variable name '" + gv.getName() +
1921
"'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo)."

cpp/ql/src/Critical/OverflowStatic.ql

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import cpp
1717
import semmle.code.cpp.commons.Buffer
1818
import semmle.code.cpp.ir.dataflow.DataFlow
1919
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
20+
import semmle.code.cpp.ConfigurationTestFile
2021
import LoopBounds
2122

2223
private predicate staticBufferBase(VariableAccess access, Variable v) {
@@ -148,7 +149,10 @@ predicate outOfBounds(BufferAccess bufaccess, string msg) {
148149

149150
from Element error, string msg
150151
where
151-
overflowOffsetInLoop(error, msg) or
152-
wrongBufferSize(error, msg) or
153-
outOfBounds(error, msg)
152+
(
153+
overflowOffsetInLoop(error, msg) or
154+
wrongBufferSize(error, msg) or
155+
outOfBounds(error, msg)
156+
) and
157+
not error.getFile() instanceof ConfigurationTestFile // elements in files generated during configuration are likely false positives
154158
select error, msg

cpp/ql/src/Likely Bugs/Arithmetic/FloatComparison.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
import cpp
15+
import semmle.code.cpp.ConfigurationTestFile
1516

1617
from EqualityOperation ro, Expr left, Expr right
1718
where
@@ -20,5 +21,6 @@ where
2021
ro.getAnOperand().getExplicitlyConverted().getType().getUnderlyingType() instanceof
2122
FloatingPointType and
2223
not ro.getAnOperand().isConstant() and // comparisons to constants generate too many false positives
23-
not left.(VariableAccess).getTarget() = right.(VariableAccess).getTarget() // skip self comparison
24+
not left.(VariableAccess).getTarget() = right.(VariableAccess).getTarget() and // skip self comparison
25+
not ro.getFile() instanceof ConfigurationTestFile // expressions in files generated during configuration are likely false positives
2426
select ro, "Equality checks on floating point values can yield unexpected results."

cpp/ql/src/Likely Bugs/Likely Typos/ExprHasNoEffect.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
*/
1313

1414
import cpp
15-
private import semmle.code.cpp.commons.Exclusions
15+
import semmle.code.cpp.commons.Exclusions
16+
import semmle.code.cpp.ConfigurationTestFile
1617

1718
class PureExprInVoidContext extends ExprInVoidContext {
1819
PureExprInVoidContext() { this.isPure() }
@@ -90,6 +91,7 @@ where
9091
not peivc.getType() instanceof UnknownType and
9192
not functionContainsDisabledCodeRecursive(peivc.(FunctionCall).getTarget()) and
9293
not functionDefinedInIfDefRecursive(peivc.(FunctionCall).getTarget()) and
94+
not peivc.getFile() instanceof ConfigurationTestFile and // expressions in files generated during configuration are likely false positives
9395
if peivc instanceof FunctionCall
9496
then
9597
exists(Function target |

cpp/ql/src/Likely Bugs/Underspecified Functions/TooFewArguments.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919

2020
import cpp
2121
import TooFewArguments
22+
import semmle.code.cpp.ConfigurationTestFile
2223

2324
from FunctionCall fc, Function f
24-
where tooFewArguments(fc, f)
25+
where
26+
tooFewArguments(fc, f) and
27+
not fc.getFile() instanceof ConfigurationTestFile // calls in files generated during configuration are likely false positives
2528
select fc, "This call has fewer arguments than required by $@.", f, f.toString()

cpp/ql/src/Security/CWE/CWE-120/BadlyBoundedWrite.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import semmle.code.cpp.security.BufferWrite
19+
import semmle.code.cpp.ConfigurationTestFile
1920

2021
/*
2122
* See CWE-120/UnboundedWrite.ql for a summary of CWE-120 alert cases.
@@ -26,7 +27,8 @@ where
2627
bw.hasExplicitLimit() and // has an explicit size limit
2728
destSize = max(getBufferSize(bw.getDest(), _)) and
2829
bw.getExplicitLimit() > destSize and // but it's larger than the destination
29-
not bw.getDest().getType().stripType() instanceof ErroneousType // destSize may be incorrect
30+
not bw.getDest().getType().stripType() instanceof ErroneousType and // destSize may be incorrect
31+
not bw.getFile() instanceof ConfigurationTestFile // expressions in files generated during configuration are likely false positives
3032
select bw,
3133
"This '" + bw.getBWDesc() + "' operation is limited to " + bw.getExplicitLimit() +
3234
" bytes but the destination is only " + destSize + " bytes."

cpp/ql/src/Security/CWE/CWE-732/DoNotCreateWorldWritable.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import cpp
1414
import FilePermissions
15+
import semmle.code.cpp.ConfigurationTestFile
1516

1617
predicate worldWritableCreation(FileCreationExpr fc, int mode) {
1718
mode = localUmask(fc).mask(fc.getMode()) and
@@ -27,6 +28,7 @@ predicate setWorldWritable(FunctionCall fc, int mode) {
2728
from Expr fc, int mode, string message
2829
where
2930
worldWritableCreation(fc, mode) and
31+
not fc.getFile() instanceof ConfigurationTestFile and // expressions in files generated during configuration are likely false positives
3032
message =
3133
"A file may be created here with mode " + octalFileMode(mode) +
3234
", which would make it world-writable."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `cpp/badly-bounded-write`, `cpp/equality-on-floats`, `cpp/short-global-name`, `cpp/static-buffer-overflow`, `cpp/too-few-arguments`, `cpp/useless-expression`, `cpp/world-writable-file-creation` queries no longer produce alerts on files created by CMake to test the build configuration.

0 commit comments

Comments
 (0)