Skip to content

Commit b77cea8

Browse files
committed
Fix incorrect pointer inputs to json.Unmarshal
See https://github.com/howardjohn/go-unmarshal-double-pointer for more info on why this is not safe and how this is detected.
1 parent b92c836 commit b77cea8

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

libcni/conf.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"os"
2222
"path/filepath"
2323
"sort"
24+
25+
"github.com/containernetworking/cni/pkg/types"
2426
)
2527

2628
type NotFoundError struct {
@@ -41,8 +43,8 @@ func (e NoConfigsFoundError) Error() string {
4143
}
4244

4345
func ConfFromBytes(bytes []byte) (*NetworkConfig, error) {
44-
conf := &NetworkConfig{Bytes: bytes}
45-
if err := json.Unmarshal(bytes, &conf.Network); err != nil {
46+
conf := &NetworkConfig{Bytes: bytes, Network: &types.NetConf{}}
47+
if err := json.Unmarshal(bytes, conf.Network); err != nil {
4648
return nil, fmt.Errorf("error parsing configuration: %w", err)
4749
}
4850
if conf.Network.Type == "" {

pkg/types/040/types_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ var _ = Describe("040 types operations", func() {
348348
}`))
349349

350350
recovered := &types040.IPConfig{}
351-
Expect(json.Unmarshal(jsonBytes, &recovered)).To(Succeed())
351+
Expect(json.Unmarshal(jsonBytes, recovered)).To(Succeed())
352352
Expect(recovered).To(Equal(ipc))
353353
})
354354

@@ -363,7 +363,7 @@ var _ = Describe("040 types operations", func() {
363363
Context("when unmarshalling json fails", func() {
364364
It("returns an error", func() {
365365
recovered := &types040.IPConfig{}
366-
err := json.Unmarshal([]byte(`{"address": 5}`), &recovered)
366+
err := json.Unmarshal([]byte(`{"address": 5}`), recovered)
367367
Expect(err).To(MatchError(HavePrefix("json: cannot unmarshal")))
368368
})
369369
})

pkg/types/100/types_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,14 @@ var _ = Describe("Current types operations", func() {
295295
}`))
296296

297297
recovered := &current.IPConfig{}
298-
Expect(json.Unmarshal(jsonBytes, &recovered)).To(Succeed())
298+
Expect(json.Unmarshal(jsonBytes, recovered)).To(Succeed())
299299
Expect(recovered).To(Equal(ipc))
300300
})
301301

302302
Context("when unmarshalling json fails", func() {
303303
It("returns an error", func() {
304304
recovered := &current.IPConfig{}
305-
err := json.Unmarshal([]byte(`{"address": 5}`), &recovered)
305+
err := json.Unmarshal([]byte(`{"address": 5}`), recovered)
306306
Expect(err).To(MatchError(HavePrefix("json: cannot unmarshal")))
307307
})
308308
})

0 commit comments

Comments
 (0)