From 892df5c28eb5ed2a0431fccc326ab5b2fec1881a Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Wed, 29 Dec 2021 07:49:33 -0600 Subject: [PATCH] Decode: fix index out of range bug (#716) Fixes #715 --- decode.go | 4 ++++ unmarshaler_test.go | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/decode.go b/decode.go index 2bc5eda..8c2bbac 100644 --- a/decode.go +++ b/decode.go @@ -400,6 +400,10 @@ func checkAndRemoveUnderscoresIntegers(b []byte) ([]byte, error) { start++ } + if len(b) == start { + return b, nil + } + if b[start] == '_' { return nil, newDecodeError(b[start:start+1], "number cannot start with underscore") } diff --git a/unmarshaler_test.go b/unmarshaler_test.go index 59b6d51..7ba5526 100644 --- a/unmarshaler_test.go +++ b/unmarshaler_test.go @@ -2326,6 +2326,18 @@ func TestIssue710(t *testing.T) { require.Equal(t, map[string]toml.LocalTime{"0": {Nanosecond: 111111111, Precision: 9}}, v2) } +func TestIssue715(t *testing.T) { + var v interface{} + err := toml.Unmarshal([]byte("0=+"), &v) + require.Error(t, err) + + err = toml.Unmarshal([]byte("0=-"), &v) + require.Error(t, err) + + err = toml.Unmarshal([]byte("0=+A"), &v) + require.Error(t, err) +} + func TestUnmarshalDecodeErrors(t *testing.T) { examples := []struct { desc string