Skip to content

Commit f04a85e

Browse files
authored
Merge pull request github#16753 from owen-mc/go/misc-clean-up
Go: a few small clean ups
2 parents 94d12ed + 754fd8e commit f04a85e

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

go/ql/lib/semmle/go/Scopes.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ class Entity extends @object {
113113

114114
/** Gets the qualified name of this entity, if any. */
115115
string getQualifiedName() {
116-
exists(string pkg, string name | this.hasQualifiedName(pkg, name) | result = pkg + "." + name)
116+
exists(string pkg, string name | this.hasQualifiedName(pkg, name) |
117+
if pkg = "" then result = name else result = pkg + "." + name
118+
)
117119
}
118120

119121
/**

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ module BeegoOrm {
1818
DbSink() {
1919
exists(Method m, string methodName, int argNum |
2020
m.hasQualifiedName(packagePath(), "DB", methodName) and
21-
methodName in [
22-
"Exec", "ExecContext", "Prepare", "PrepareContext", "Query", "QueryContext", "QueryRow",
23-
"QueryRowContext"
24-
] and
25-
if methodName.matches("%Context") then argNum = 1 else argNum = 0
21+
(
22+
methodName = ["Exec", "Prepare", "Query", "QueryRow"] and
23+
argNum = 0
24+
or
25+
methodName = ["ExecContext", "PrepareContext", "QueryContext", "QueryRowContext"] and
26+
argNum = 1
27+
)
2628
|
2729
this = m.getACall().getArgument(argNum)
2830
)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ module Logrus {
1515
}
1616

1717
bindingset[result]
18-
private string getAnEntryUpdatingMethodName() { result.regexpMatch("With(Error|Fields?|Time)") }
18+
private string getAnEntryUpdatingMethodName() {
19+
result = ["WithError", "WithField", "WithFields", "WithTime"]
20+
}
1921

2022
private class LogFunction extends Function {
2123
LogFunction() {

go/ql/lib/semmle/go/frameworks/stdlib/Fmt.qll

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import go
88
/** Provides models of commonly used functions in the `fmt` package. */
99
module Fmt {
1010
/**
11-
* The `Sprint` or `Append` functions or one of their variants.
12-
*
1311
* DEPRECATED: Use AppenderOrSprinterFunc instead.
12+
*
13+
* The `Sprint` or `Append` functions or one of their variants.
1414
*/
1515
deprecated class AppenderOrSprinter extends TaintTracking::FunctionModel {
16-
AppenderOrSprinter() { this.hasQualifiedName("fmt", ["Append", "Sprint"] + ["", "f", "ln"]) }
16+
AppenderOrSprinter() {
17+
this.hasQualifiedName("fmt",
18+
["Append", "Appendf", "Appendln", "Sprint", "Sprintf", "Sprintln"])
19+
}
1720

1821
override predicate hasTaintFlow(FunctionInput inp, FunctionOutput outp) {
1922
inp.isParameter(_) and outp.isResult()
@@ -23,13 +26,14 @@ module Fmt {
2326
/** The `Sprint` or `Append` functions or one of their variants. */
2427
class AppenderOrSprinterFunc extends Function {
2528
AppenderOrSprinterFunc() {
26-
this.hasQualifiedName("fmt", ["Append", "Sprint"] + ["", "f", "ln"])
29+
this.hasQualifiedName("fmt",
30+
["Append", "Appendf", "Appendln", "Sprint", "Sprintf", "Sprintln"])
2731
}
2832
}
2933

3034
/** The `Sprint` function or one of its variants. */
3135
class Sprinter extends AppenderOrSprinterFunc {
32-
Sprinter() { this.getName().matches("Sprint%") }
36+
Sprinter() { this.getName() = ["Sprint", "Sprintf", "Sprintln"] }
3337
}
3438

3539
/** The `Print` function or one of its variants. */

go/ql/lib/semmle/go/frameworks/stdlib/HtmlTemplate.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ module HtmlTemplate {
1111

1212
TemplateEscape() {
1313
exists(string fn |
14-
fn.matches("HTMLEscape%") and kind = "html"
14+
fn = ["HTMLEscape", "HTMLEscapeString", "HTMLEscaper"] and kind = "html"
1515
or
16-
fn.matches("JSEscape%") and kind = "js"
16+
fn = ["JSEscape", "JSEscapeString", "JSEscaper"] and kind = "js"
1717
or
18-
fn.matches("URLQueryEscape%") and kind = "url"
18+
fn = "URLQueryEscaper" and kind = "url"
1919
|
2020
this.hasQualifiedName("html/template", fn)
2121
)

go/ql/lib/semmle/go/frameworks/stdlib/Log.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ module Log {
1111

1212
LogFunction() {
1313
exists(string fn |
14-
fn.matches(["Fatal%", "Panic%", "Print%"]) and firstPrintedArg = 0
14+
fn =
15+
["Fatal", "Fatalf", "Fatalln", "Panic", "Panicf", "Panicln", "Print", "Printf", "Println"] and
16+
firstPrintedArg = 0
1517
or
1618
fn = "Output" and firstPrintedArg = 1
1719
|
@@ -25,7 +27,7 @@ module Log {
2527
}
2628

2729
private class LogFormatter extends StringOps::Formatting::Range instanceof LogFunction {
28-
LogFormatter() { this.getName().matches("%f") }
30+
LogFormatter() { this.getName() = ["Fatalf", "Panicf", "Printf"] }
2931

3032
override int getFormatStringIndex() { result = 0 }
3133
}
@@ -42,9 +44,7 @@ module Log {
4244

4345
/** A fatal log function, which calls `os.Exit`. */
4446
private class FatalLogFunction extends Function {
45-
FatalLogFunction() {
46-
exists(string fn | fn.matches("Fatal%") | this.hasQualifiedName("log", fn))
47-
}
47+
FatalLogFunction() { this.hasQualifiedName("log", ["Fatal", "Fatalf", "Fatalln"]) }
4848

4949
override predicate mayReturnNormally() { none() }
5050
}

0 commit comments

Comments
 (0)