Skip to content

Commit 513da82

Browse files
committed
Model data flow for min and max
1 parent d189a15 commit 513da82

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,32 @@ private class CopyFunction extends TaintTracking::FunctionModel {
7070
}
7171
}
7272

73+
/**
74+
* A model of the built-in `min` function, which computes the smallest value of a fixed number of
75+
* arguments of ordered types. There is at least one argument and "ordered types" includes e.g.
76+
* strings, so we care about data flow through `min`.
77+
*/
78+
private class MinFunction extends DataFlow::FunctionModel {
79+
MinFunction() { this = Builtin::min_() }
80+
81+
override predicate hasDataFlow(FunctionInput inp, FunctionOutput outp) {
82+
inp.isParameter(_) and outp.isResult()
83+
}
84+
}
85+
86+
/**
87+
* A model of the built-in `max` function, which computes the largest value of a fixed number of
88+
* arguments of ordered types. There is at least one argument and "ordered types" includes e.g.
89+
* strings, so we care about data flow through `max`.
90+
*/
91+
private class MaxFunction extends DataFlow::FunctionModel {
92+
MaxFunction() { this = Builtin::max_() }
93+
94+
override predicate hasDataFlow(FunctionInput inp, FunctionOutput outp) {
95+
inp.isParameter(_) and outp.isResult()
96+
}
97+
}
98+
7399
/** Provides a class for modeling functions which convert strings into integers. */
74100
module IntegerParser {
75101
/**

go/ql/test/library-tests/semmle/go/dataflow/FlowSteps/LocalFlowStep.expected

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
| file://:0:0:0:0 | function append | main.go:40:8:40:13 | append |
4545
| file://:0:0:0:0 | function copy | main.go:42:2:42:5 | copy |
4646
| file://:0:0:0:0 | function make | main.go:41:8:41:11 | make |
47+
| file://:0:0:0:0 | function max | main.go:65:7:65:9 | max |
48+
| file://:0:0:0:0 | function min | main.go:64:7:64:9 | min |
4749
| main.go:3:6:3:10 | function test1 | main.go:34:2:34:6 | test1 |
4850
| main.go:3:12:3:12 | argument corresponding to x | main.go:3:12:3:12 | definition of x |
4951
| main.go:3:12:3:12 | definition of x | main.go:5:5:5:5 | x |
@@ -112,6 +114,25 @@
112114
| main.go:55:6:55:7 | definition of ch | main.go:56:2:56:3 | ch |
113115
| main.go:55:6:55:7 | definition of ch | main.go:57:4:57:5 | ch |
114116
| main.go:55:6:55:7 | zero value for ch | main.go:55:6:55:7 | definition of ch |
117+
| main.go:61:2:61:2 | definition of x | main.go:64:11:64:11 | x |
118+
| main.go:61:2:61:2 | definition of x | main.go:65:11:65:11 | x |
119+
| main.go:61:7:61:7 | 1 | main.go:61:2:61:2 | definition of x |
120+
| main.go:62:2:62:2 | definition of y | main.go:64:14:64:14 | y |
121+
| main.go:62:2:62:2 | definition of y | main.go:65:14:65:14 | y |
122+
| main.go:62:7:62:7 | 2 | main.go:62:2:62:2 | definition of y |
123+
| main.go:63:2:63:2 | definition of z | main.go:64:17:64:17 | z |
124+
| main.go:63:2:63:2 | definition of z | main.go:65:17:65:17 | z |
125+
| main.go:63:7:63:7 | 3 | main.go:63:2:63:2 | definition of z |
126+
| main.go:64:2:64:2 | definition of a | main.go:66:9:66:9 | a |
127+
| main.go:64:7:64:18 | call to min | main.go:64:2:64:2 | definition of a |
128+
| main.go:64:11:64:11 | x | main.go:64:7:64:18 | call to min |
129+
| main.go:64:14:64:14 | y | main.go:64:7:64:18 | call to min |
130+
| main.go:64:17:64:17 | z | main.go:64:7:64:18 | call to min |
131+
| main.go:65:2:65:2 | definition of b | main.go:66:12:66:12 | b |
132+
| main.go:65:7:65:18 | call to max | main.go:65:2:65:2 | definition of b |
133+
| main.go:65:11:65:11 | x | main.go:65:7:65:18 | call to max |
134+
| main.go:65:14:65:14 | y | main.go:65:7:65:18 | call to max |
135+
| main.go:65:17:65:17 | z | main.go:65:7:65:18 | call to max |
115136
| strings.go:8:12:8:12 | argument corresponding to s | strings.go:8:12:8:12 | definition of s |
116137
| strings.go:8:12:8:12 | definition of s | strings.go:9:24:9:24 | s |
117138
| strings.go:8:12:8:12 | definition of s | strings.go:10:27:10:27 | s |

go/ql/test/library-tests/semmle/go/dataflow/FlowSteps/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,12 @@ func testch() {
5656
ch <- true
5757
<-ch
5858
}
59+
60+
func testMinMax() (int, int) {
61+
x := 1
62+
y := 2
63+
z := 3
64+
a := min(x, y, z)
65+
b := max(x, y, z)
66+
return a, b
67+
}

0 commit comments

Comments
 (0)