Skip to content

Commit

Permalink
Fix incorrect pointer inputs to json.Unmarshal
Browse files Browse the repository at this point in the history
See https://github.com/howardjohn/go-unmarshal-double-pointer for more
info on why this is not safe and how this is detected.

Signed-off-by: John Howard <howardjohn@google.com>
  • Loading branch information
howardjohn committed Feb 22, 2022
1 parent b92c836 commit 3e49ce1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
6 changes: 4 additions & 2 deletions libcni/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"os"
"path/filepath"
"sort"

"github.com/containernetworking/cni/pkg/types"
)

type NotFoundError struct {
Expand All @@ -41,8 +43,8 @@ func (e NoConfigsFoundError) Error() string {
}

func ConfFromBytes(bytes []byte) (*NetworkConfig, error) {
conf := &NetworkConfig{Bytes: bytes}
if err := json.Unmarshal(bytes, &conf.Network); err != nil {
conf := &NetworkConfig{Bytes: bytes, Network: &types.NetConf{}}
if err := json.Unmarshal(bytes, conf.Network); err != nil {
return nil, fmt.Errorf("error parsing configuration: %w", err)
}
if conf.Network.Type == "" {
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/040/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ var _ = Describe("040 types operations", func() {
}`))

recovered := &types040.IPConfig{}
Expect(json.Unmarshal(jsonBytes, &recovered)).To(Succeed())
Expect(json.Unmarshal(jsonBytes, recovered)).To(Succeed())
Expect(recovered).To(Equal(ipc))
})

Expand All @@ -363,7 +363,7 @@ var _ = Describe("040 types operations", func() {
Context("when unmarshalling json fails", func() {
It("returns an error", func() {
recovered := &types040.IPConfig{}
err := json.Unmarshal([]byte(`{"address": 5}`), &recovered)
err := json.Unmarshal([]byte(`{"address": 5}`), recovered)
Expect(err).To(MatchError(HavePrefix("json: cannot unmarshal")))
})
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/100/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,14 @@ var _ = Describe("Current types operations", func() {
}`))

recovered := &current.IPConfig{}
Expect(json.Unmarshal(jsonBytes, &recovered)).To(Succeed())
Expect(json.Unmarshal(jsonBytes, recovered)).To(Succeed())
Expect(recovered).To(Equal(ipc))
})

Context("when unmarshalling json fails", func() {
It("returns an error", func() {
recovered := &current.IPConfig{}
err := json.Unmarshal([]byte(`{"address": 5}`), &recovered)
err := json.Unmarshal([]byte(`{"address": 5}`), recovered)
Expect(err).To(MatchError(HavePrefix("json: cannot unmarshal")))
})
})
Expand Down

0 comments on commit 3e49ce1

Please sign in to comment.