@@ -6,12 +6,12 @@ import (
6
6
)
7
7
8
8
func closeFileDeferred (f * os.File ) {
9
- defer f .Close () // NOT OK, if `f` is writable
9
+ defer f .Close () // $ Alert=w Alert=rw
10
10
}
11
11
12
12
func closeFileDeferredIndirect (f * os.File ) {
13
13
var cont = func () {
14
- f .Close () // NOT OK, if `f` is writable
14
+ f .Close () // $ Alert=w Alert=rw
15
15
}
16
16
17
17
defer cont ()
@@ -28,7 +28,7 @@ func closeFileDeferredIndirectReturn(f *os.File) {
28
28
29
29
func deferredCalls () {
30
30
// open file for writing
31
- if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil {
31
+ if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil { // $ Source=w
32
32
closeFileDeferred (f ) // NOT OK
33
33
closeFileDeferredIndirect (f ) // NOT OK
34
34
closeFileDeferredIndirectReturn (f ) // OK - the error is not discarded at the call to Close (though it is discarded later)
@@ -42,7 +42,7 @@ func deferredCalls() {
42
42
}
43
43
44
44
// open file for reading and writing
45
- if f , err := os .OpenFile ("foo.txt" , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil {
45
+ if f , err := os .OpenFile ("foo.txt" , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil { // $ Source=rw
46
46
closeFileDeferred (f ) // NOT OK
47
47
closeFileDeferredIndirect (f ) // NOT OK
48
48
closeFileDeferredIndirectReturn (f ) // OK - the error is not discarded at the call to Close (though it is discarded later)
@@ -51,9 +51,9 @@ func deferredCalls() {
51
51
52
52
func notDeferred () {
53
53
// open file for writing
54
- if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil {
54
+ if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil { // $ Source
55
55
// the handle is write-only and we don't check if `Close` succeeds
56
- f .Close () // NOT OK
56
+ f .Close () // $ Alert
57
57
}
58
58
59
59
// open file for reading
@@ -63,9 +63,9 @@ func notDeferred() {
63
63
}
64
64
65
65
// open file for reading and writing
66
- if f , err := os .OpenFile ("foo.txt" , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil {
66
+ if f , err := os .OpenFile ("foo.txt" , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil { // $ Source
67
67
// the handle is read-write and we don't check if `Close` succeeds
68
- f .Close () // NOT OK
68
+ f .Close () // $ Alert
69
69
}
70
70
}
71
71
@@ -105,9 +105,9 @@ func deferredCloseWithSync() {
105
105
106
106
func deferredCloseWithSyncEarlyReturn (n int ) {
107
107
// open file for writing
108
- if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil {
108
+ if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil { // $ Source
109
109
// a call to `Close` is deferred
110
- defer f .Close () // NOT OK
110
+ defer f .Close () // $ Alert
111
111
112
112
if n > 100 {
113
113
return
@@ -122,10 +122,10 @@ func deferredCloseWithSyncEarlyReturn(n int) {
122
122
123
123
func unhandledSync () {
124
124
// open file for writing
125
- if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil {
125
+ if f , err := os .OpenFile ("foo.txt" , os .O_WRONLY | os .O_TRUNC | os .O_CREATE , 0666 ); err != nil { // $ Source
126
126
// we have a call to `Sync` which precedes the call to `Close`, but there is no check
127
127
// to see if `Sync` may have failed
128
128
f .Sync ()
129
- f .Close () // NOT OK
129
+ f .Close () // $ Alert
130
130
}
131
131
}
0 commit comments