Skip to content

Commit

Permalink
Merge pull request #880 from howardjohn/json/unmarshal-pointer
Browse files Browse the repository at this point in the history
Fix incorrect pointer inputs to `json.Unmarshal`
  • Loading branch information
dcbw authored Feb 23, 2022
2 parents b92c836 + 3e49ce1 commit 96a1883
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 96a1883

Please sign in to comment.