Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ func (d *Decimal) setString(c *Context, s string) (Condition, error) {
exps = append(exps, -exp)
s = s[:i] + s[i+1:]
}
for _, ch := range s {
if ch < '0' || ch > '9' {
return 0, fmt.Errorf("parse mantissa: %s", s)
}
}
if _, ok := d.Coeff.SetString(s, 10); !ok {
return 0, fmt.Errorf("parse mantissa: %s", s)
}
Expand Down
30 changes: 30 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,3 +964,33 @@ func TestJSONEncoding(t *testing.T) {
}
}
}

// TestNewFromStringInvalid tests that NewFromString produces
// an error when parsing an invalid decimal.
func TestNewFromStringInvalid(t *testing.T) {
tests := []struct {
input string
}{
{input: ".-5"},
{input: ".+5"},
{input: ".-0"},
{input: ".+0"},
{input: ".-123"},
{input: ".+123"},
{input: "1.-2"},
{input: "1.+2"},
{input: ".1-2"},
{input: ".1+2"},
{input: ".-5e1"},
{input: ".+5e1"},
}
for _, tc := range tests {
t.Run(tc.input, func(t *testing.T) {
d, _, err := NewFromString(tc.input)
if err == nil {
t.Fatalf("expected error for %q, but parsed as: %s (Negative=%v, Coeff=%s)",
tc.input, d.String(), d.Negative, d.Coeff.String())
}
})
}
}
Loading