Skip to content

Commit 7aa9434

Browse files
author
Brendan Chang
committed
Use strict unmarshaling for metav1 fuzz targets
1 parent 862e814 commit 7aa9434

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

test/fuzz/yaml/yaml.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,42 @@ import (
2727
sigyaml "sigs.k8s.io/yaml"
2828
)
2929

30-
// FuzzDuration is a fuzz target for unmarshaling Duration defined in "k8s.io/apimachinery/pkg/apis/meta/v1".
31-
// This target also checks that the unmarshaled result can be marshaled back to the input.
32-
func FuzzDuration(b []byte) int {
33-
var unmarshalResult struct {
30+
// FuzzDuration is a fuzz target for strict-unmarshaling Duration defined in
31+
// "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the
32+
// unmarshaled result can be marshaled back to the input.
33+
func FuzzDurationStrict(b []byte) int {
34+
var durationHolder struct {
3435
D metav1.Duration `json:"d"`
3536
}
36-
if err := yaml.Unmarshal(b, &unmarshalResult); err != nil {
37+
if err := sigyaml.UnmarshalStrict(b, &durationHolder); err != nil {
3738
return 0
3839
}
39-
marshalResult, err := sigyaml.Marshal(&unmarshalResult)
40+
result, err := sigyaml.Marshal(&durationHolder)
4041
if err != nil {
4142
panic(err)
4243
}
43-
if !bytes.Equal(marshalResult, b) {
44-
panic("marshalResult != input")
44+
if !bytes.Equal(result, b) {
45+
panic("result != input")
4546
}
4647
return 1
4748
}
4849

49-
// FuzzMicroTime is a fuzz target for unmarshaling MicroTime defined in "k8s.io/apimachinery/pkg/apis/meta/v1".
50-
// This target also checks that the unmarshaled result can be marshaled back to the input.
51-
func FuzzMicroTime(b []byte) int {
52-
var unmarshalResult struct {
50+
// FuzzMicroTime is a fuzz target for strict-unmarshaling MicroTime defined in
51+
// "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the
52+
// unmarshaled result can be marshaled back to the input.
53+
func FuzzMicroTimeStrict(b []byte) int {
54+
var microTimeHolder struct {
5355
T metav1.MicroTime `json:"t"`
5456
}
55-
if err := yaml.Unmarshal(b, &unmarshalResult); err != nil {
57+
if err := sigyaml.UnmarshalStrict(b, &microTimeHolder); err != nil {
5658
return 0
5759
}
58-
marshalResult, err := sigyaml.Marshal(&unmarshalResult)
60+
result, err := sigyaml.Marshal(&microTimeHolder)
5961
if err != nil {
6062
panic(err)
6163
}
62-
if !bytes.Equal(marshalResult, b) {
63-
panic("marshalResult != input")
64+
if !bytes.Equal(result, b) {
65+
panic("result != input")
6466
}
6567
return 1
6668
}
@@ -79,21 +81,22 @@ func FuzzSigYaml(b []byte) int {
7981
return out
8082
}
8183

82-
// FuzzTime is a fuzz target for unmarshaling Time defined in "k8s.io/apimachinery/pkg/apis/meta/v1".
83-
// This target also checks that the unmarshaled result can be marshaled back to the input.
84+
// FuzzTime is a fuzz target for strict-unmarshaling Time defined in
85+
// "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the
86+
// unmarshaled result can be marshaled back to the input.
8487
func FuzzTime(b []byte) int {
85-
var unmarshalResult struct {
88+
var timeHolder struct {
8689
T metav1.Time `json:"t"`
8790
}
88-
if err := sigyaml.Unmarshal(b, &unmarshalResult); err != nil {
91+
if err := sigyaml.UnmarshalStrict(b, &timeHolder); err != nil {
8992
return 0
9093
}
91-
marshalResult, err := yaml.Marshal(&unmarshalResult)
94+
result, err := sigyaml.Marshal(&timeHolder)
9295
if err != nil {
9396
panic(err)
9497
}
95-
if !bytes.Equal(marshalResult, b) {
96-
panic("marshalResult != input")
98+
if !bytes.Equal(result, b) {
99+
panic("result != input")
97100
}
98101
return 1
99102
}

0 commit comments

Comments
 (0)