-
Notifications
You must be signed in to change notification settings - Fork 5
Skipping error checks for json.Encoder.Encode or json.MarshalIndent is not safe #14
Copy link
Copy link
Closed
Description
$ cat t.go
package main
import (
"encoding/json"
"os"
)
type Foo struct {
X string
}
func main() {
err := json.NewEncoder(os.Stdout).Encode(Foo{"bar"})
if err != nil {
panic("meh")
}
}
$ ./errchkjson t.go
t.go:13:9: Error return value of `(*encoding/json.Encoder).Encode` is checked but passed argument is safe
$ ./errchkjson -V
errchkjson version 0.2.3
commit: 3aae21891fb6867e0ae247403f8eb67c0573c5d5
built at: 2022-02-08T16:17:38Z
module version: v0.2.3, checksum: h1:97eGTmR/w0paL2SwfRPI1jaAZHaH/fXnxWTw2eEIqE0=
goos: linux
goarch: amd64json.Encoder.Encode can return an error due to a number of reasons unrelated to the argument's general JSON marshal safety:
- https://cs.opensource.google/go/go/+/refs/tags/go1.17.6:src/encoding/json/stream.go;l=202
- https://cs.opensource.google/go/go/+/refs/tags/go1.17.6:src/encoding/json/stream.go;l=225
- https://cs.opensource.google/go/go/+/refs/tags/go1.17.6:src/encoding/json/stream.go;l=231
The same applies to json.MarshalIndent:
I think errchkjson should not encourage skipping error checks for these functions at all.
(Haven't thought through if there are other messages in errchkjson that should be adjusted due to these considerations, but I suspect there might be some.)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels