Skip to content

Commit 99bed0b

Browse files
authored
Merge pull request github#12127 from smowton/smowton/perf/golang-less-string-construction
Go: Consolidate repeated calls to `matches` and `regexpMatch`
2 parents 3abf321 + 99d3f68 commit 99bed0b

File tree

8 files changed

+12
-27
lines changed

8 files changed

+12
-27
lines changed

go/ql/lib/semmle/go/Comments.qll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,8 @@ class BuildConstraintComment extends LineComment {
208208
this = getInitialComment(f, i) and
209209
not getInitialComment(f, [0 .. i - 1]) instanceof BlockComment
210210
) and
211-
(
212-
// comment text starts with `+build` or `go:build`
213-
this.getText().regexpMatch("\\s*\\+build.*")
214-
or
215-
this.getText().regexpMatch("\\s*go:build.*")
216-
)
211+
// comment text starts with `+build` or `go:build`
212+
this.getText().regexpMatch("\\s*(\\+|go:)build.*")
217213
}
218214

219215
override string getAPrimaryQlClass() { result = "BuildConstraintComment" }

go/ql/lib/semmle/go/frameworks/Logrus.qll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ module Logrus {
1111

1212
bindingset[result]
1313
private string getALogResultName() {
14-
result
15-
.matches([
16-
"Debug%", "Error%", "Fatal%", "Info%", "Log%", "Panic%", "Print%", "Trace%", "Warn%"
17-
])
14+
result.regexpMatch("(Debug|Error|Fatal|Info|Log|Panic|Print|Trace|Warn).*")
1815
}
1916

2017
bindingset[result]

go/ql/lib/semmle/go/frameworks/Revel.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ module Revel {
9191
}
9292

9393
private string contentTypeFromFilename(DataFlow::Node filename) {
94-
if filename.getStringValue().toLowerCase().matches(["%.htm", "%.html"])
94+
if filename.getStringValue().regexpMatch("(?i).*\\.html?")
9595
then result = "text/html"
9696
else result = "application/octet-stream"
9797
// Actually Revel can figure out a variety of other content-types, but none of our analyses care to

go/ql/lib/semmle/go/security/SensitiveActions.qll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,9 @@ module PasswordHeuristics {
233233
predicate isDummyPassword(string password) {
234234
password.length() < 4
235235
or
236-
exists(string normalized | normalized = password.toLowerCase() |
237-
count(normalized.charAt(_)) = 1 or
238-
normalized
239-
.regexpMatch(".*(pass|test|sample|example|secret|root|admin|user|change|auth|redacted|0123456789).*")
240-
)
236+
count(password.charAt(_)) <= 2 // aaaaaaaa or bBbBbB or ghghghghghgh or the like
237+
or
238+
password
239+
.regexpMatch("(?i).*(pass|test|sample|example|secret|root|admin|user|change|auth|redacted|0123456789).*")
241240
}
242241
}

go/ql/src/Security/CWE-209/StackTraceExposure.ql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ class DebugModeFlag extends FlagKind {
2626

2727
bindingset[result]
2828
override string getAFlagName() {
29-
result
30-
.toLowerCase()
31-
.matches("%" + ["trace", "debug", "devel", "enablestack", "disablestack", "printstack"] +
32-
"%")
29+
result.regexpMatch("(?i).*(trace|debug|devel|((en|dis)able|print)stack).*")
3330
}
3431
}
3532

go/ql/src/Security/CWE-295/DisabledCertificateCheck.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class InsecureCertificateFlag extends FlagKind {
4545

4646
bindingset[result]
4747
override string getAFlagName() {
48-
result.toLowerCase().matches("%" + ["selfcert", "selfsign", "validat", "verif", "trust"] + "%")
48+
result.regexpMatch("(?i).*(selfcert|selfsign|validat|verif|trust).*")
4949
}
5050
}
5151

go/ql/src/Security/CWE-327/InsecureTLS.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,7 @@ class LegacyTlsVersionFlag extends FlagKind {
240240
LegacyTlsVersionFlag() { this = "legacyTlsVersion" }
241241

242242
bindingset[result]
243-
override string getAFlagName() {
244-
result.toLowerCase().matches("%" + ["old", "intermediate", "legacy"] + "%")
245-
}
243+
override string getAFlagName() { result.regexpMatch("(?i).*(old|intermediate|legacy).*") }
246244
}
247245

248246
/**

go/ql/src/experimental/CWE-942/CorsMisconfiguration.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ class AllowedFlag extends FlagKind {
2323

2424
bindingset[result]
2525
override string getAFlagName() {
26-
result
27-
.toLowerCase()
28-
.matches("%" + ["allow", "match", "check", "debug", "devel", "insecure"] + "%")
26+
result.regexpMatch("(?i).*(allow|match|check|debug|devel|insecure).*")
2927
}
3028
}
3129

0 commit comments

Comments
 (0)