From fa56f48dafbcbb560f5959149e64683eccac6341 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Thu, 9 Sep 2021 11:59:37 -0400 Subject: [PATCH] parser: don't overflow when parsing bad times (#593) Fixes #585 --- parser.go | 5 +++++ unmarshaler_test.go | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/parser.go b/parser.go index 829f89e..da3e5a3 100644 --- a/parser.go +++ b/parser.go @@ -894,6 +894,11 @@ byteLoop: case c == ' ': if !seenSpace && i+1 < len(b) && isDigit(b[i+1]) { i += 2 + // Avoid reaching past the end of the document in case the time + // is malformed. See TestIssue585. + if i >= len(b) { + i-- + } seenSpace = true hasTime = true } else { diff --git a/unmarshaler_test.go b/unmarshaler_test.go index 30fc53d..e634a48 100644 --- a/unmarshaler_test.go +++ b/unmarshaler_test.go @@ -1746,6 +1746,12 @@ func TestIssue588(t *testing.T) { require.Error(t, err) } +func TestIssue585(t *testing.T) { + var v interface{} + err := toml.Unmarshal([]byte(`a=1979-05127T 0`), &v) + require.Error(t, err) +} + //nolint:funlen func TestUnmarshalDecodeErrors(t *testing.T) { examples := []struct {