Skip to content

Commit 45b1253

Browse files
committed
Improve glog and klog tests
1 parent c99dab1 commit 45b1253

6 files changed

Lines changed: 275 additions & 49 deletions

File tree

Lines changed: 180 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,181 @@
1-
//go:generate depstubber -vendor github.com/golang/glog "" Error,ErrorDepth,Errorf,Errorln,Exit,ExitDepth,Exitf,Exitln,Fatal,FatalDepth,Fatalf,Fatalln,Info,InfoDepth,Infof,Infoln,Warning,WarningDepth,Warningf,Warningln
2-
//go:generate depstubber -vendor k8s.io/klog "" Error,ErrorDepth,Errorf,Errorln,Exit,ExitDepth,Exitf,Exitln,Fatal,FatalDepth,Fatalf,Fatalln,Info,InfoDepth,Infof,Infoln,Warning,WarningDepth,Warningf,Warningln
1+
//go:generate depstubber -vendor github.com/golang/glog Level,Verbose Error,ErrorContext,ErrorContextDepth,ErrorContextDepthf,ErrorContextf,ErrorDepth,ErrorDepthf,Errorf,Errorln,Exit,ExitContext,ExitContextDepth,ExitContextDepthf,ExitContextf,ExitDepth,ExitDepthf,Exitf,Exitln,Fatal,FatalContext,FatalContextDepth,FatalContextDepthf,FatalContextf,FatalDepth,FatalDepthf,Fatalf,Fatalln,Info,InfoContext,InfoContextDepth,InfoContextDepthf,InfoContextf,InfoDepth,InfoDepthf,Infof,Infoln,V,VDepth,Warning,WarningContext,WarningContextDepth,WarningContextDepthf,WarningContextf,WarningDepth,WarningDepthf,Warningf,Warningln
2+
//go:generate depstubber -vendor k8s.io/klog Level,Verbose Error,ErrorDepth,Errorf,Errorln,Exit,ExitDepth,Exitf,Exitln,Fatal,FatalDepth,Fatalf,Fatalln,Info,InfoDepth,Infof,Infoln,V,Warning,WarningDepth,Warningf,Warningln
33

44
package main
55

66
import (
7+
"context"
8+
79
"github.com/golang/glog"
810
"k8s.io/klog"
911
)
1012

11-
func glogTest() {
12-
glog.Error(text) // $ logger=text
13-
glog.ErrorDepth(0, text) // $ logger=text
14-
glog.Errorf(fmt, text) // $ logger=fmt logger=text
15-
glog.Errorln(text) // $ logger=text
16-
glog.Exit(text) // $ logger=text
17-
glog.ExitDepth(0, text) // $ logger=text
18-
glog.Exitf(fmt, text) // $ logger=fmt logger=text
19-
glog.Exitln(text) // $ logger=text
20-
glog.Fatal(text) // $ logger=text
21-
glog.FatalDepth(0, text) // $ logger=text
22-
glog.Fatalf(fmt, text) // $ logger=fmt logger=text
23-
glog.Fatalln(text) // $ logger=text
24-
glog.Info(text) // $ logger=text
25-
glog.InfoDepth(0, text) // $ logger=text
26-
glog.Infof(fmt, text) // $ logger=fmt logger=text
27-
glog.Infoln(text) // $ logger=text
28-
glog.Warning(text) // $ logger=text
29-
glog.WarningDepth(0, text) // $ logger=text
30-
glog.Warningf(fmt, text) // $ logger=fmt logger=text
31-
glog.Warningln(text) // $ logger=text
13+
func glogTest(selector int) {
14+
ctx := context.Background()
15+
16+
glog.Error(text) // $ logger=text
17+
glog.ErrorContext(ctx, text) // $ logger=text
18+
glog.ErrorContextDepth(ctx, 0, text) // $ logger=text
19+
glog.ErrorContextDepthf(ctx, 0, fmt, text) // $ logger=fmt logger=text
20+
glog.ErrorContextf(ctx, fmt, text) // $ logger=fmt logger=text
21+
glog.ErrorDepth(0, text) // $ logger=text
22+
glog.ErrorDepthf(0, fmt, text) // $ logger=fmt logger=text
23+
glog.Errorf(fmt, text) // $ logger=fmt logger=text
24+
glog.Errorln(text) // $ logger=text
25+
if selector == 1 {
26+
glog.Exit(text) // $ logger=text
27+
}
28+
if selector == 2 {
29+
glog.ExitContext(ctx, text) // $ logger=text
30+
}
31+
if selector == 3 {
32+
glog.ExitContextDepth(ctx, 0, text) // $ logger=text
33+
}
34+
if selector == 4 {
35+
glog.ExitContextDepthf(ctx, 0, fmt, text) // $ logger=fmt logger=text
36+
}
37+
if selector == 5 {
38+
glog.ExitContextf(ctx, fmt, text) // $ logger=fmt logger=text
39+
}
40+
if selector == 6 {
41+
glog.ExitDepth(0, text) // $ logger=text
42+
}
43+
if selector == 7 {
44+
glog.ExitDepthf(0, fmt, text) // $ logger=fmt logger=text
45+
}
46+
if selector == 8 {
47+
glog.Exitf(fmt, text) // $ logger=fmt logger=text
48+
}
49+
if selector == 9 {
50+
glog.Exitln(text) // $ logger=text
51+
}
52+
if selector == 10 {
53+
glog.Fatal(text) // $ logger=text
54+
}
55+
if selector == 11 {
56+
glog.FatalContext(ctx, text) // $ logger=text
57+
}
58+
if selector == 12 {
59+
glog.FatalContextDepth(ctx, 0, text) // $ logger=text
60+
}
61+
if selector == 13 {
62+
glog.FatalContextDepthf(ctx, 0, fmt, text) // $ logger=fmt logger=text
63+
}
64+
if selector == 14 {
65+
glog.FatalContextf(ctx, fmt, text) // $ logger=fmt logger=text
66+
}
67+
if selector == 15 {
68+
glog.FatalDepth(0, text) // $ logger=text
69+
}
70+
if selector == 16 {
71+
glog.FatalDepthf(0, fmt, text) // $ logger=fmt logger=text
72+
}
73+
if selector == 17 {
74+
glog.Fatalf(fmt, text) // $ logger=fmt logger=text
75+
}
76+
if selector == 18 {
77+
glog.Fatalln(text) // $ logger=text
78+
}
79+
glog.Info(text) // $ logger=text
80+
glog.InfoContext(ctx, text) // $ logger=text
81+
glog.InfoContextDepth(ctx, 0, text) // $ logger=text
82+
glog.InfoContextDepthf(ctx, 0, fmt, text) // $ logger=fmt logger=text
83+
glog.InfoContextf(ctx, fmt, text) // $ logger=fmt logger=text
84+
glog.InfoDepth(0, text) // $ logger=text
85+
glog.InfoDepthf(0, fmt, text) // $ logger=fmt logger=text
86+
glog.Infof(fmt, text) // $ logger=fmt logger=text
87+
glog.Infoln(text) // $ logger=text
88+
glog.Warning(text) // $ logger=text
89+
glog.WarningContext(ctx, text) // $ logger=text
90+
glog.WarningContextDepth(ctx, 0, text) // $ logger=text
91+
glog.WarningContextDepthf(ctx, 0, fmt, text) // $ logger=fmt logger=text
92+
glog.WarningContextf(ctx, fmt, text) // $ logger=fmt logger=text
93+
glog.WarningDepth(0, text) // $ logger=text
94+
glog.WarningDepthf(0, fmt, text) // $ logger=fmt logger=text
95+
glog.Warningf(fmt, text) // $ logger=fmt logger=text
96+
glog.Warningln(text) // $ logger=text
97+
98+
glog.V(0).Info(text) // $ logger=text
99+
glog.V(0).InfoContext(ctx, text) // $ logger=text
100+
glog.V(0).InfoContextDepth(ctx, 0, text) // $ logger=text
101+
glog.V(0).InfoContextDepthf(ctx, 0, fmt, text) // $ logger=fmt logger=text
102+
glog.V(0).InfoContextf(ctx, fmt, text) // $ logger=fmt logger=text
103+
glog.V(0).InfoDepth(0, text) // $ logger=text
104+
glog.V(0).InfoDepthf(0, fmt, text) // $ logger=fmt logger=text
105+
glog.V(0).Infof(fmt, text) // $ logger=fmt logger=text
106+
glog.V(0).Infoln(text) // $ logger=text
107+
glog.VDepth(0, 0).Info(text) // $ logger=text
32108

33109
// components corresponding to the format specifier "%T" are not considered vulnerable
34-
glog.Errorf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
35-
glog.Exitf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
36-
glog.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
37-
glog.Infof("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
38-
glog.Warningf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
110+
glog.ErrorContextDepthf(ctx, 0, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
111+
glog.ErrorContextf(ctx, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
112+
glog.ErrorDepthf(0, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
113+
glog.Errorf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
114+
if selector == 19 {
115+
glog.ExitContextDepthf(ctx, 0, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
116+
}
117+
if selector == 20 {
118+
glog.ExitContextf(ctx, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
119+
}
120+
if selector == 21 {
121+
glog.ExitDepthf(0, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
122+
}
123+
if selector == 22 {
124+
glog.Exitf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
125+
}
126+
if selector == 23 {
127+
glog.FatalContextDepthf(ctx, 0, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
128+
}
129+
if selector == 24 {
130+
glog.FatalContextf(ctx, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
131+
}
132+
if selector == 25 {
133+
glog.FatalDepthf(0, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
134+
}
135+
if selector == 26 {
136+
glog.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
137+
}
138+
glog.InfoContextDepthf(ctx, 0, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
139+
glog.InfoContextf(ctx, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
140+
glog.InfoDepthf(0, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
141+
glog.Infof("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
142+
glog.WarningContextDepthf(ctx, 0, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
143+
glog.WarningContextf(ctx, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
144+
glog.WarningDepthf(0, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
145+
glog.Warningf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
146+
glog.V(0).InfoContextDepthf(ctx, 0, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
147+
glog.V(0).InfoContextf(ctx, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
148+
glog.V(0).InfoDepthf(0, "%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
149+
glog.V(0).Infof("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
39150

40-
klog.Error(text) // $ logger=text
41-
klog.ErrorDepth(0, text) // $ logger=text
42-
klog.Errorf(fmt, text) // $ logger=fmt logger=text
43-
klog.Errorln(text) // $ logger=text
44-
klog.Exit(text) // $ logger=text
45-
klog.ExitDepth(0, text) // $ logger=text
46-
klog.Exitf(fmt, text) // $ logger=fmt logger=text
47-
klog.Exitln(text) // $ logger=text
48-
klog.Fatal(text) // $ logger=text
49-
klog.FatalDepth(0, text) // $ logger=text
50-
klog.Fatalf(fmt, text) // $ logger=fmt logger=text
51-
klog.Fatalln(text) // $ logger=text
151+
klog.Error(text) // $ logger=text
152+
klog.ErrorDepth(0, text) // $ logger=text
153+
klog.Errorf(fmt, text) // $ logger=fmt logger=text
154+
klog.Errorln(text) // $ logger=text
155+
if selector == 27 {
156+
klog.Exit(text) // $ logger=text
157+
}
158+
if selector == 28 {
159+
klog.ExitDepth(0, text) // $ logger=text
160+
}
161+
if selector == 29 {
162+
klog.Exitf(fmt, text) // $ logger=fmt logger=text
163+
}
164+
if selector == 30 {
165+
klog.Exitln(text) // $ logger=text
166+
}
167+
if selector == 31 {
168+
klog.Fatal(text) // $ logger=text
169+
}
170+
if selector == 32 {
171+
klog.FatalDepth(0, text) // $ logger=text
172+
}
173+
if selector == 33 {
174+
klog.Fatalf(fmt, text) // $ logger=fmt logger=text
175+
}
176+
if selector == 34 {
177+
klog.Fatalln(text) // $ logger=text
178+
}
52179
klog.Info(text) // $ logger=text
53180
klog.InfoDepth(0, text) // $ logger=text
54181
klog.Infof(fmt, text) // $ logger=fmt logger=text
@@ -57,11 +184,19 @@ func glogTest() {
57184
klog.WarningDepth(0, text) // $ logger=text
58185
klog.Warningf(fmt, text) // $ logger=fmt logger=text
59186
klog.Warningln(text) // $ logger=text
187+
klog.V(0).Info(text) // $ logger=text
188+
klog.V(0).Infof(fmt, text) // $ logger=fmt logger=text
189+
klog.V(0).Infoln(text) // $ logger=text
60190

61191
// components corresponding to the format specifier "%T" are not considered vulnerable
62-
klog.Errorf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
63-
klog.Exitf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
64-
klog.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
65-
klog.Infof("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
66-
klog.Warningf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
192+
klog.Errorf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
193+
if selector == 35 {
194+
klog.Exitf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
195+
}
196+
if selector == 36 {
197+
klog.Fatalf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
198+
}
199+
klog.Infof("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
200+
klog.Warningf("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
201+
klog.V(0).Infof("%s: found type %T", text, v) // $ logger="%s: found type %T" logger=text type-logger=v
67202
}

go/ql/test/library-tests/semmle/go/concepts/LoggerCall/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module codeql-go-tests/concepts/loggercall
33
go 1.15
44

55
require (
6-
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
6+
github.com/golang/glog v1.2.5
77
github.com/sirupsen/logrus v1.7.0
88
k8s.io/klog v1.0.0
99
)

go/ql/test/library-tests/semmle/go/concepts/LoggerCall/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ const text = "test"
66
var v []byte
77

88
func main() {
9+
glogTest(len(v))
910
stdlib()
1011
}

go/ql/test/library-tests/semmle/go/concepts/LoggerCall/vendor/github.com/golang/glog/stub.go

Lines changed: 79 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)