Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3536d84

Browse files
committed
C++: Use [, ...] syntax more widely.
1 parent fce76e2 commit 3536d84

23 files changed

Lines changed: 87 additions & 200 deletions

cpp/ql/src/Critical/OverflowDestination.ql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ import semmle.code.cpp.security.TaintTracking
2323
* ```
2424
*/
2525
predicate sourceSized(FunctionCall fc, Expr src) {
26-
exists(string name |
27-
(name = "strncpy" or name = "strncat" or name = "memcpy" or name = "memmove") and
28-
fc.getTarget().hasGlobalOrStdName(name)
29-
) and
26+
fc.getTarget().hasGlobalOrStdName(["strncpy", "strncat", "memcpy", "memmove"]) and
3027
exists(Expr dest, Expr size, Variable v |
3128
fc.getArgument(0) = dest and
3229
fc.getArgument(1) = src and

cpp/ql/src/Critical/SizeCheck2.ql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ import cpp
1616

1717
class Allocation extends FunctionCall {
1818
Allocation() {
19-
exists(string name |
20-
this.getTarget().hasGlobalOrStdName(name) and
21-
(name = "malloc" or name = "calloc" or name = "realloc")
22-
)
19+
this.getTarget().hasGlobalOrStdName(["malloc", "calloc", "realloc"])
2320
}
2421

2522
private string getName() { this.getTarget().hasGlobalOrStdName(result) }

cpp/ql/src/JPL_C/LOC-2/Rule 11/SimpleControlFlowJmp.ql

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@
1313
import cpp
1414

1515
class ForbiddenFunction extends Function {
16-
ForbiddenFunction() {
17-
exists(string name | name = this.getName() |
18-
name = "setjmp" or
19-
name = "longjmp" or
20-
name = "sigsetjmp" or
21-
name = "siglongjmp"
22-
)
23-
}
16+
ForbiddenFunction() { this.getName() = ["setjmp", "longjmp", "sigsetjmp", "siglongjmp"] }
2417
}
2518

2619
from FunctionCall call

cpp/ql/src/Likely Bugs/Leap Year/UncheckedReturnValueForTimeFunctions.ql

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ class DateStructModifiedFieldAccess extends LeapYearFieldAccess {
4040
*/
4141
class SafeTimeGatheringFunction extends Function {
4242
SafeTimeGatheringFunction() {
43-
this.getQualifiedName() = "GetFileTime" or
44-
this.getQualifiedName() = "GetSystemTime" or
45-
this.getQualifiedName() = "NtQuerySystemTime"
43+
this.getQualifiedName() = ["GetFileTime", "GetSystemTime", "NtQuerySystemTime"]
4644
}
4745
}
4846

@@ -51,15 +49,11 @@ class SafeTimeGatheringFunction extends Function {
5149
*/
5250
class TimeConversionFunction extends Function {
5351
TimeConversionFunction() {
54-
this.getQualifiedName() = "FileTimeToSystemTime" or
55-
this.getQualifiedName() = "SystemTimeToFileTime" or
56-
this.getQualifiedName() = "SystemTimeToTzSpecificLocalTime" or
57-
this.getQualifiedName() = "SystemTimeToTzSpecificLocalTimeEx" or
58-
this.getQualifiedName() = "TzSpecificLocalTimeToSystemTime" or
59-
this.getQualifiedName() = "TzSpecificLocalTimeToSystemTimeEx" or
60-
this.getQualifiedName() = "RtlLocalTimeToSystemTime" or
61-
this.getQualifiedName() = "RtlTimeToSecondsSince1970" or
62-
this.getQualifiedName() = "_mkgmtime"
52+
this.getQualifiedName() =
53+
["FileTimeToSystemTime", "SystemTimeToFileTime", "SystemTimeToTzSpecificLocalTime",
54+
"SystemTimeToTzSpecificLocalTimeEx", "TzSpecificLocalTimeToSystemTime",
55+
"TzSpecificLocalTimeToSystemTimeEx", "RtlLocalTimeToSystemTime",
56+
"RtlTimeToSecondsSince1970", "_mkgmtime"]
6357
}
6458
}
6559

cpp/ql/src/Microsoft/SAL.qll

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ import cpp
1010
*/
1111
class SALMacro extends Macro {
1212
SALMacro() {
13-
exists(string filename | filename = this.getFile().getBaseName() |
14-
filename = "sal.h" or
15-
filename = "specstrings_strict.h" or
16-
filename = "specstrings.h" or
17-
filename = "w32p.h" or
18-
filename = "minwindef.h"
19-
) and
13+
this.getFile().getBaseName() =
14+
["sal.h", "specstrings_strict.h", "specstrings.h", "w32p.h", "minwindef.h"] and
2015
(
2116
// Dialect for Windows 8 and above
2217
this.getName().matches("\\_%\\_")
@@ -58,10 +53,7 @@ class SALAnnotation extends MacroInvocation {
5853
*/
5954
class SALCheckReturn extends SALAnnotation {
6055
SALCheckReturn() {
61-
exists(SALMacro m | m = this.getMacro() |
62-
m.getName() = "_Check_return_" or
63-
m.getName() = "_Must_inspect_result_"
64-
)
56+
this.getMacro().(SALMacro).getName() = ["_Check_return_", "_Must_inspect_result_"]
6557
}
6658
}
6759

cpp/ql/src/Security/CWE/CWE-121/UnterminatedVarargsCall.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class VarargsFunction extends Function {
5656
}
5757

5858
string normalTerminator(int cnt) {
59-
(result = "0" or result = "-1") and
59+
result = ["0", "-1"] and
6060
cnt = trailingArgValueCount(result) and
6161
2 * cnt > totalCount() and
6262
not exists(FunctionCall fc, int index |

cpp/ql/src/Security/CWE/CWE-676/DangerousUseOfCin.ql

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,14 @@ class IFStream extends Type {
6666
*/
6767
class CinVariable extends NamespaceVariable {
6868
CinVariable() {
69-
(
70-
getName() = "cin" or
71-
getName() = "wcin"
72-
) and
73-
getNamespace().getName() = "std"
69+
this.hasQualifiedName("std", ["cin", "wcin"])
7470
}
7571
}
7672

7773
/** A call to `std::operator>>`. */
7874
class OperatorRShiftCall extends FunctionCall {
7975
OperatorRShiftCall() {
80-
getTarget().getNamespace().getName() = "std" and
81-
getTarget().hasName("operator>>")
76+
getTarget().hasQualifiedName("std", "operator>>")
8277
}
8378

8479
/*

cpp/ql/src/Security/CWE/CWE-676/PotentiallyDangerousFunction.ql

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ import cpp
1414

1515
predicate potentiallyDangerousFunction(Function f, string message) {
1616
exists(string name | f.hasGlobalName(name) |
17-
(
18-
name = "gmtime" or
19-
name = "localtime" or
20-
name = "ctime" or
21-
name = "asctime"
22-
) and
17+
name = ["gmtime", "localtime", "ctime", "asctime"] and
2318
message = "Call to " + name + " is potentially dangerous"
2419
)
2520
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ predicate worldWritableCreation(FileCreationExpr fc, int mode) {
1919
}
2020

2121
predicate setWorldWritable(FunctionCall fc, int mode) {
22-
exists(string name | fc.getTarget().getName() = name |
23-
name = "chmod" or
24-
name = "fchmod" or
25-
name = "_chmod" or
26-
name = "_wchmod"
27-
) and
22+
fc.getTarget().getName() = ["chmod", "fchmod", "_chmod", "_wchmod"] and
2823
mode = fc.getArgument(1).getValue().toInt() and
2924
sets(mode, s_iwoth())
3025
}

cpp/ql/src/Security/CWE/CWE-732/FilePermissions.qll

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ predicate sets(int mask, int fields) { mask.bitAnd(fields) != 0 }
3131
* one of the `umask` family of functions.
3232
*/
3333
private int umask(FunctionCall fc) {
34-
exists(string name | name = fc.getTarget().getName() |
35-
name = "umask" or
36-
name = "_umask" or
37-
name = "_umask_s"
38-
) and
34+
fc.getTarget().getName() = ["umask", "_umask", "_umask_s"] and
3935
result = fc.getArgument(0).getValue().toInt()
4036
}
4137

@@ -89,11 +85,7 @@ abstract class FileCreationExpr extends FunctionCall {
8985

9086
class OpenCreationExpr extends FileCreationExpr {
9187
OpenCreationExpr() {
92-
exists(string name | name = this.getTarget().getName() |
93-
name = "open" or
94-
name = "_open" or
95-
name = "_wopen"
96-
) and
88+
this.getTarget().getName() = ["open", "_open", "_wopen"] and
9789
sets(this.getArgument(1).getValue().toInt(), o_creat())
9890
}
9991

@@ -134,14 +126,9 @@ private int fopenMode() {
134126

135127
class FopenCreationExpr extends FileCreationExpr {
136128
FopenCreationExpr() {
137-
exists(string name | name = this.getTarget().getName() |
138-
name = "fopen" or
139-
name = "_wfopen" or
140-
name = "fsopen" or
141-
name = "_wfsopen"
142-
) and
129+
this.getTarget().getName() = ["fopen", "_wfopen", "fsopen", "_wfsopen"] and
143130
exists(string mode |
144-
(mode = "w" or mode = "a") and
131+
mode = ["w", "a"] and
145132
this.getArgument(1).getValue().matches(mode + "%")
146133
)
147134
}

0 commit comments

Comments
 (0)