diff --git a/xdr2/decode.go b/xdr2/decode.go index eea39cf..6a8712a 100644 --- a/xdr2/decode.go +++ b/xdr2/decode.go @@ -518,7 +518,7 @@ func (d *Decoder) decodeStruct(v reflect.Value) (int, error) { hasopt, n2, err := d.DecodeBool() n += n2 if err != nil { - return n2, err + return n, err } if !hasopt { continue diff --git a/xdr2/decode_test.go b/xdr2/decode_test.go index 1d34497..c7c2641 100644 --- a/xdr2/decode_test.go +++ b/xdr2/decode_test.go @@ -399,6 +399,9 @@ func TestUnmarshal(t *testing.T) { {[]byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00}, optionalDataStruct{1, &optionalDataStruct{2, nil}}, 16, nil}, + {[]byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, + optionalDataStruct{1, nil}, + 7, &UnmarshalError{ErrorCode: ErrIO}}, {[]byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, invalidOptionalDataStruct{}, 0, &UnmarshalError{ErrorCode: ErrBadOptional}}, diff --git a/xdr2/encode.go b/xdr2/encode.go index 6813036..73a2ff0 100644 --- a/xdr2/encode.go +++ b/xdr2/encode.go @@ -467,7 +467,7 @@ func (enc *Encoder) encodeStruct(v reflect.Value) (int, error) { n2, err := enc.EncodeBool(hasopt) n += n2 if err != nil { - return n2, err + return n, err } if !hasopt { continue diff --git a/xdr2/encode_test.go b/xdr2/encode_test.go index 25e6096..7e59f30 100644 --- a/xdr2/encode_test.go +++ b/xdr2/encode_test.go @@ -327,8 +327,12 @@ func TestMarshal(t *testing.T) { {optionalDataStruct{1, &optionalDataStruct{2, nil}}, []byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00}, 16, nil}, + {optionalDataStruct{1, nil}, + []uint8{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, + 7, &MarshalError{ErrorCode: ErrIO}}, {invalidOptionalDataStruct{1}, - []byte{}, 0, &MarshalError{ErrorCode: ErrBadOptional}}, + []byte{}, + 0, &MarshalError{ErrorCode: ErrBadOptional}}, // Expected errors {nilInterface, []byte{}, 0, &MarshalError{ErrorCode: ErrNilInterface}},