Skip to content

Commit 286c877

Browse files
committed
configarg{,_test}, header_test: fix crasher found by go-fuzz, add marshal/unmarshal tests to ensure consistency going forward
1 parent 962d643 commit 286c877

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

configarg.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,12 @@ const (
8585

8686
// MarshalBinary allocates a byte slice containing the data from a ConfigArg.
8787
//
88-
// If c.Version does not equal Version (1), ErrorUnsupportedVersion is
89-
// returned.
90-
//
9188
// If any of the following conditions occur, ErrorBadArgumentParameter is
9289
// returned:
9390
// - c.Command is larger than a 4-bit integer (0xf)
9491
// - c.StringLength does not indicate the actual length of c.String
9592
// - c.StringLength is greater than 1024
9693
func (c *ConfigArg) MarshalBinary() ([]byte, error) {
97-
// Version must be 1
98-
if c.Version != Version {
99-
return nil, ErrorUnsupportedVersion
100-
}
101-
10294
// Command must be a 4-bit integer
10395
if c.Command > 0xf {
10496
return nil, ErrorBadArgumentParameter

configarg_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ func TestConfigArgMarshalBinary(t *testing.T) {
1414
b []byte
1515
err error
1616
}{
17-
{
18-
desc: "empty ConfigArg",
19-
c: &ConfigArg{},
20-
err: ErrorUnsupportedVersion,
21-
},
2217
{
2318
desc: "command greater than 4-bit integer",
2419
c: &ConfigArg{

header_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,65 @@ func TestHeaderMarshalBinary(t *testing.T) {
8080
}
8181
}
8282

83+
func TestHeaderUnmarshalAndMarshalBinary(t *testing.T) {
84+
var tests = []struct {
85+
desc string
86+
b []byte
87+
}{
88+
{
89+
desc: "go-fuzz crasher: ConfigArg.Version accepted in unmarshal, but rejected in marshal",
90+
b: []byte("\x100000\x010000000000\x00\x00"),
91+
},
92+
{
93+
desc: "header with CommandIssueATACommand, ATAArg OK",
94+
b: []byte{
95+
0x10, 0, 0, 1, 2, 0, 0, 0, 0, 10,
96+
0x53, 1, 2, 3, 6, 6, 6, 6, 6, 6, 0, 0, 'f', 'o', 'o',
97+
},
98+
},
99+
{
100+
desc: "header with CommandQueryConfigInformation, ConfigArg OK",
101+
b: []byte{
102+
0x10, 0, 0, 1, 2, 1, 0, 0, 0, 10,
103+
0, 10, 0, 1, 2, 0x11, 0, 3, 'f', 'o', 'o',
104+
},
105+
},
106+
{
107+
desc: "header with CommandMACMaskList, MACMaskArg OK",
108+
b: []byte{
109+
0x10, 0, 0, 1, 2, 2, 0, 0, 0, 10,
110+
0, 0, 0, 1,
111+
0, 1, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
112+
},
113+
},
114+
{
115+
desc: "header with CommandReserveRelease, ReserveReleaseArg OK",
116+
b: []byte{
117+
0x10, 0, 0, 1, 2, 3, 0, 0, 0, 10,
118+
0, 1,
119+
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
120+
},
121+
},
122+
}
123+
124+
for i, tt := range tests {
125+
h := new(Header)
126+
if err := h.UnmarshalBinary(tt.b); err != nil {
127+
t.Fatalf("[%02d] unmarshal test %q, %v", i, tt.desc, err)
128+
}
129+
130+
b, err := h.MarshalBinary()
131+
if err != nil {
132+
t.Fatalf("[%02d] marshal test %q, %v", i, tt.desc, err)
133+
}
134+
135+
if want, got := tt.b, b; !bytes.Equal(want, got) {
136+
t.Fatalf("[%02d] test %q, unexpected bytes:\n- want: %v\n- got: %v",
137+
i, tt.desc, want, got)
138+
}
139+
}
140+
}
141+
83142
func TestHeaderUnmarshalBinary(t *testing.T) {
84143
var tests = []struct {
85144
desc string

0 commit comments

Comments
 (0)