-
Notifications
You must be signed in to change notification settings - Fork 60
decimal: incorrect MP_DECIMAL decoding with scale < 0 #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -121,6 +121,18 @@ var correctnessSamples = []struct { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"c7150113012345678912345678900987654321987654321d", false}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var correctnessDecodeSamples = []struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
numString string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mpBuf string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fixExt bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{"1e2", "d501fe1c", true}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{"1e33", "c70301d0df1c", false}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{"1.1e31", "c70301e2011c", false}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add limits. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have tests for min/max: go-tarantool/decimal/decimal_test.go Lines 251 to 278 in 337ca73
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{"13e-2", "c7030102013c", false}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{"-1e3", "d501fd1d", true}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// There is a difference between encoding result from a raw string and from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// decimal.Decimal. It's expected because decimal.Decimal simplifies decimals: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 0.00010000 -> 0.0001 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -397,18 +409,22 @@ func TestEncodeStringToBCD(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
func TestDecodeStringFromBCD(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
samples := correctnessSamples | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
samples = append(samples, correctnessDecodeSamples...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
samples = append(samples, rawSamples...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
samples = append(samples, benchmarkSamples...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for _, testcase := range samples { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
t.Run(testcase.numString, func(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
b, _ := hex.DecodeString(testcase.mpBuf) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bcdBuf := trimMPHeader(b, testcase.fixExt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
s, err := DecodeStringFromBCD(bcdBuf) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
s, exp, err := DecodeStringFromBCD(bcdBuf) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
t.Fatalf("Failed to decode BCD '%x' to decimal: %s", bcdBuf, err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
decActual, err := decimal.NewFromString(s) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if exp != 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
decActual = decActual.Shift(int32(exp)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
t.Fatalf("Failed to msgpack.Encoder string ('%s') to decimal", s) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -551,6 +567,37 @@ func TestSelect(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tupleValueIsDecimal(t, resp.Data, number) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
func TestUnmarshal_from_decimal_new(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
skipIfDecimalUnsupported(t) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
conn := test_helpers.ConnectWithValidation(t, server, opts) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
defer conn.Close() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
samples := correctnessSamples | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
samples = append(samples, correctnessDecodeSamples...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
samples = append(samples, benchmarkSamples...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for _, testcase := range samples { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
str := testcase.numString | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
t.Run(str, func(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
number, err := decimal.NewFromString(str) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
t.Fatalf("Failed to prepare test decimal: %s", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
call := NewEvalRequest("return require('decimal').new(...)"). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Args([]interface{}{str}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
resp, err := conn.Do(call).Get() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
t.Fatalf("Decimal create failed: %s", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if resp == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
t.Fatalf("Response is nil after Call") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tupleValueIsDecimal(t, []interface{}{resp.Data}, number) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
func assertInsert(t *testing.T, conn *Connection, numString string) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
number, err := decimal.NewFromString(numString) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.