diff --git a/decimal.go b/decimal.go index 7686af12..62edc05e 100644 --- a/decimal.go +++ b/decimal.go @@ -1353,10 +1353,10 @@ func (d Decimal) MarshalJSON() ([]byte, error) { // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. As a string representation // is already used when encoding to text, this method stores that string as []byte func (d *Decimal) UnmarshalBinary(data []byte) error { - // Verify we have at least 5 bytes, 4 for the exponent and at least 1 more - // for the GOB encoded value. - if len(data) < 5 { - return fmt.Errorf("error decoding binary %v: expected at least 5 bytes, got %d", data, len(data)) + // Verify we have at least 4 bytes for the exponent. The GOB encoded value + // may be empty. + if len(data) < 4 { + return fmt.Errorf("error decoding binary %v: expected at least 4 bytes, got %d", data, len(data)) } // Extract the exponent diff --git a/decimal_test.go b/decimal_test.go index b53b68b9..cd7f00b1 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -3002,6 +3002,25 @@ func TestBinary(t *testing.T) { } } +func TestBinary_Zero(t *testing.T) { + var d1 Decimal + + b, err := d1.MarshalBinary() + if err != nil { + t.Fatalf("error marshalling %v to binary: %v", d1, err) + } + + var d2 Decimal + err = (&d2).UnmarshalBinary(b) + if err != nil { + t.Errorf("error unmarshalling from binary: %v", err) + } + + if !d1.Equals(d2) { + t.Errorf("expected %v when restoring, got %v", d1, d2) + } +} + func TestBinary_DataTooShort(t *testing.T) { var d Decimal