Skip to content

Commit 37f01fe

Browse files
committed
Go: Re-factor CSV validation into separate file.
1 parent 9f9129d commit 37f01fe

11 files changed

Lines changed: 122 additions & 105 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/** Provides a query predicate to check the CSV data for validation errors. */
2+
3+
private import go
4+
private import ExternalFlow
5+
private import internal.FlowSummaryImpl::Private::External
6+
private import internal.AccessPathSyntax
7+
8+
private string getInvalidModelInput() {
9+
exists(string pred, AccessPath input, string part |
10+
sinkModel(_, _, _, _, _, _, input, _) and pred = "sink"
11+
or
12+
summaryModel(_, _, _, _, _, _, input, _, _) and pred = "summary"
13+
|
14+
(
15+
invalidSpecComponent(input, part) and
16+
not part = "" and
17+
not parseArg(part, _)
18+
or
19+
part = input.getToken(_) and
20+
parseParam(part, _)
21+
) and
22+
result = "Unrecognized input specification \"" + part + "\" in " + pred + " model."
23+
)
24+
}
25+
26+
private string getInvalidModelOutput() {
27+
exists(string pred, string output, string part |
28+
sourceModel(_, _, _, _, _, _, output, _) and pred = "source"
29+
or
30+
summaryModel(_, _, _, _, _, _, _, output, _) and pred = "summary"
31+
|
32+
invalidSpecComponent(output, part) and
33+
not part = "" and
34+
not (part = "Parameter" and pred = "source") and
35+
result = "Unrecognized output specification \"" + part + "\" in " + pred + " model."
36+
)
37+
}
38+
39+
private string getInvalidModelKind() { none() }
40+
41+
private string getInvalidModelSubtype() {
42+
exists(string pred, string row, int expect |
43+
sourceModel(row) and expect = 8 and pred = "source"
44+
or
45+
sinkModel(row) and expect = 8 and pred = "sink"
46+
or
47+
summaryModel(row) and expect = 9 and pred = "summary"
48+
|
49+
exists(string b |
50+
b = row.splitAt(";", 2) and
51+
not b = ["true", "false"] and
52+
result = "Invalid boolean \"" + b + "\" in " + pred + " model."
53+
)
54+
)
55+
}
56+
57+
private string getInvalidModelColumnCount() {
58+
exists(string pred, string row, int expect |
59+
sourceModel(row) and expect = 8 and pred = "source"
60+
or
61+
sinkModel(row) and expect = 8 and pred = "sink"
62+
or
63+
summaryModel(row) and expect = 9 and pred = "summary"
64+
|
65+
exists(int cols |
66+
cols = 1 + max(int n | exists(row.splitAt(";", n))) and
67+
cols != expect and
68+
result =
69+
"Wrong number of columns in " + pred + " model row, expected " + expect + ", got " + cols +
70+
"."
71+
)
72+
)
73+
}
74+
75+
private string getInvalidModelSignature() {
76+
exists(string pred, string namespace, string type, string name, string signature, string ext |
77+
sourceModel(namespace, type, _, name, signature, ext, _, _) and pred = "source"
78+
or
79+
sinkModel(namespace, type, _, name, signature, ext, _, _) and pred = "sink"
80+
or
81+
summaryModel(namespace, type, _, name, signature, ext, _, _, _) and pred = "summary"
82+
|
83+
not namespace.regexpMatch("[a-zA-Z0-9_\\./]*") and
84+
result = "Dubious namespace \"" + namespace + "\" in " + pred + " model."
85+
or
86+
not type.regexpMatch("[a-zA-Z0-9_\\$<>]*") and
87+
result = "Dubious type \"" + type + "\" in " + pred + " model."
88+
or
89+
not name.regexpMatch("[a-zA-Z0-9_]*") and
90+
result = "Dubious name \"" + name + "\" in " + pred + " model."
91+
or
92+
not signature.regexpMatch("|\\([a-zA-Z0-9_\\.\\$<>,\\[\\]]*\\)") and
93+
result = "Dubious signature \"" + signature + "\" in " + pred + " model."
94+
or
95+
not ext.regexpMatch("|Annotated") and
96+
result = "Unrecognized extra API graph element \"" + ext + "\" in " + pred + " model."
97+
)
98+
}
99+
100+
/** Holds if some row in a CSV-based flow model appears to contain typos. */
101+
query predicate invalidModelRow(string msg) {
102+
msg =
103+
[
104+
getInvalidModelSignature(), getInvalidModelInput(), getInvalidModelOutput(),
105+
getInvalidModelSubtype(), getInvalidModelColumnCount(), getInvalidModelKind()
106+
]
107+
}

go/ql/lib/semmle/go/dataflow/ExternalFlow.qll

Lines changed: 6 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ private class BuiltinModel extends SummaryModelCsv {
8585
}
8686
}
8787

88-
private predicate sourceModelCsv(string row) { none() }
89-
90-
private predicate sinkModelCsv(string row) { none() }
91-
92-
private predicate summaryModelCsv(string row) { none() }
93-
9488
/**
9589
* A unit class for adding additional source model rows.
9690
*
@@ -121,20 +115,14 @@ class SummaryModelCsv extends Unit {
121115
abstract predicate row(string row);
122116
}
123117

124-
private predicate sourceModel(string row) {
125-
sourceModelCsv(row) or
126-
any(SourceModelCsv s).row(row)
127-
}
118+
/** Holds if `row` is a source model. */
119+
predicate sourceModel(string row) { any(SourceModelCsv s).row(row) }
128120

129-
private predicate sinkModel(string row) {
130-
sinkModelCsv(row) or
131-
any(SinkModelCsv s).row(row)
132-
}
121+
/** Holds if `row` is a sink model. */
122+
predicate sinkModel(string row) { any(SinkModelCsv s).row(row) }
133123

134-
private predicate summaryModel(string row) {
135-
summaryModelCsv(row) or
136-
any(SummaryModelCsv s).row(row)
137-
}
124+
/** Holds if `row` is a summary model. */
125+
predicate summaryModel(string row) { any(SummaryModelCsv s).row(row) }
138126

139127
/** Holds if a source model exists for the given parameters. */
140128
predicate sourceModel(
@@ -269,84 +257,6 @@ predicate modelCoverage(string package, int pkgs, string kind, string part, int
269257
)
270258
}
271259

272-
/** Provides a query predicate to check the CSV data for validation errors. */
273-
module CsvValidation {
274-
/** Holds if some row in a CSV-based flow model appears to contain typos. */
275-
query predicate invalidModelRow(string msg) {
276-
exists(string pred, string namespace, string type, string name, string signature, string ext |
277-
sourceModel(namespace, type, _, name, signature, ext, _, _) and pred = "source"
278-
or
279-
sinkModel(namespace, type, _, name, signature, ext, _, _) and pred = "sink"
280-
or
281-
summaryModel(namespace, type, _, name, signature, ext, _, _, _) and pred = "summary"
282-
|
283-
not namespace.regexpMatch("[a-zA-Z0-9_\\./]*") and
284-
msg = "Dubious namespace \"" + namespace + "\" in " + pred + " model."
285-
or
286-
not type.regexpMatch("[a-zA-Z0-9_\\$<>]*") and
287-
msg = "Dubious type \"" + type + "\" in " + pred + " model."
288-
or
289-
not name.regexpMatch("[a-zA-Z0-9_]*") and
290-
msg = "Dubious name \"" + name + "\" in " + pred + " model."
291-
or
292-
not signature.regexpMatch("|\\([a-zA-Z0-9_\\.\\$<>,\\[\\]]*\\)") and
293-
msg = "Dubious signature \"" + signature + "\" in " + pred + " model."
294-
or
295-
not ext.regexpMatch("|Annotated") and
296-
msg = "Unrecognized extra API graph element \"" + ext + "\" in " + pred + " model."
297-
)
298-
or
299-
exists(string pred, AccessPath input, string part |
300-
sinkModel(_, _, _, _, _, _, input, _) and pred = "sink"
301-
or
302-
summaryModel(_, _, _, _, _, _, input, _, _) and pred = "summary"
303-
|
304-
(
305-
invalidSpecComponent(input, part) and
306-
not part = "" and
307-
not parseArg(part, _)
308-
or
309-
part = input.getToken(_) and
310-
parseParam(part, _)
311-
) and
312-
msg = "Unrecognized input specification \"" + part + "\" in " + pred + " model."
313-
)
314-
or
315-
exists(string pred, string output, string part |
316-
sourceModel(_, _, _, _, _, _, output, _) and pred = "source"
317-
or
318-
summaryModel(_, _, _, _, _, _, _, output, _) and pred = "summary"
319-
|
320-
invalidSpecComponent(output, part) and
321-
not part = "" and
322-
not (part = "Parameter" and pred = "source") and
323-
msg = "Unrecognized output specification \"" + part + "\" in " + pred + " model."
324-
)
325-
or
326-
exists(string pred, string row, int expect |
327-
sourceModel(row) and expect = 8 and pred = "source"
328-
or
329-
sinkModel(row) and expect = 8 and pred = "sink"
330-
or
331-
summaryModel(row) and expect = 9 and pred = "summary"
332-
|
333-
exists(int cols |
334-
cols = 1 + max(int n | exists(row.splitAt(";", n))) and
335-
cols != expect and
336-
msg =
337-
"Wrong number of columns in " + pred + " model row, expected " + expect + ", got " + cols +
338-
"."
339-
)
340-
or
341-
exists(string b |
342-
b = row.splitAt(";", 2) and
343-
not b = ["true", "false"] and
344-
msg = "Invalid boolean \"" + b + "\" in " + pred + " model."
345-
)
346-
)
347-
}
348-
}
349-
350260
pragma[nomagic]
351261
private predicate elementSpec(
352262
string namespace, string type, boolean subtypes, string name, string signature, string ext
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
failures
21
invalidModelRow
2+
failures

go/ql/test/library-tests/semmle/go/dataflow/ExternalFlow/completetest.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*/
44

55
import go
6+
import semmle.go.dataflow.CsvValidation
67
import semmle.go.dataflow.ExternalFlow
78
import semmle.go.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
8-
import CsvValidation
99
import TestUtilities.InlineFlowTest
1010

1111
class SummaryModelTest extends SummaryModelCsv {

go/ql/test/library-tests/semmle/go/dataflow/ExternalFlow/sinks.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import go
2+
import semmle.go.dataflow.CsvValidation
23
import semmle.go.dataflow.ExternalFlow
3-
import CsvValidation
44

55
class SinkModelTest extends SinkModelCsv {
66
override predicate row(string row) {

go/ql/test/library-tests/semmle/go/dataflow/ExternalFlow/srcs.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import go
22
import semmle.go.dataflow.ExternalFlow
3-
import CsvValidation
3+
import semmle.go.dataflow.CsvValidation
44

55
class SourceModelTest extends SourceModelCsv {
66
override predicate row(string row) {

go/ql/test/library-tests/semmle/go/dataflow/ExternalFlow/steps.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import go
2+
import semmle.go.dataflow.CsvValidation
23
import semmle.go.dataflow.ExternalFlow
34
import semmle.go.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
4-
import CsvValidation
55

66
class SummaryModelTest extends SummaryModelCsv {
77
override predicate row(string row) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
failures
21
invalidModelRow
2+
failures

go/ql/test/library-tests/semmle/go/dataflow/ExternalFlowVarArgs/Flows.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import go
2+
import semmle.go.dataflow.CsvValidation
23
import semmle.go.dataflow.ExternalFlow
3-
import CsvValidation
44
import TestUtilities.InlineExpectationsTest
55

66
class SummaryModelTest extends SummaryModelCsv {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
failures
21
invalidModelRow
2+
failures

0 commit comments

Comments
 (0)