From 36a8da55cc3dffea7318d45c4b8c7f8ac5dd1dde Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Tue, 3 Nov 2020 08:28:53 -0600 Subject: [PATCH] Fix Timestamptz.DecodeText with too short text fixes #74 --- timestamp_test.go | 8 ++++++++ timestamptz.go | 4 ++-- timestamptz_test.go | 8 ++++++++ tstzrange_test.go | 8 ++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/timestamp_test.go b/timestamp_test.go index b2fbda94..74cb1221 100644 --- a/timestamp_test.go +++ b/timestamp_test.go @@ -7,6 +7,7 @@ import ( "github.com/jackc/pgtype" "github.com/jackc/pgtype/testutil" + "github.com/stretchr/testify/require" ) func TestTimestampTranscode(t *testing.T) { @@ -77,6 +78,13 @@ func TestTimestampNanosecondsTruncated(t *testing.T) { } } +// https://github.com/jackc/pgtype/issues/74 +func TestTimestampDecodeTextInvalid(t *testing.T) { + tstz := &pgtype.Timestamp{} + err := tstz.DecodeText(nil, []byte(`eeeee`)) + require.Error(t, err) +} + func TestTimestampSet(t *testing.T) { type _time time.Time diff --git a/timestamptz.go b/timestamptz.go index d54974af..a79bd66e 100644 --- a/timestamptz.go +++ b/timestamptz.go @@ -111,9 +111,9 @@ func (dst *Timestamptz) DecodeText(ci *ConnInfo, src []byte) error { *dst = Timestamptz{Status: Present, InfinityModifier: -Infinity} default: var format string - if sbuf[len(sbuf)-9] == '-' || sbuf[len(sbuf)-9] == '+' { + if len(sbuf) >= 9 && (sbuf[len(sbuf)-9] == '-' || sbuf[len(sbuf)-9] == '+') { format = pgTimestamptzSecondFormat - } else if sbuf[len(sbuf)-6] == '-' || sbuf[len(sbuf)-6] == '+' { + } else if len(sbuf) >= 6 && (sbuf[len(sbuf)-6] == '-' || sbuf[len(sbuf)-6] == '+') { format = pgTimestamptzMinuteFormat } else { format = pgTimestamptzHourFormat diff --git a/timestamptz_test.go b/timestamptz_test.go index 828184b7..769c9239 100644 --- a/timestamptz_test.go +++ b/timestamptz_test.go @@ -7,6 +7,7 @@ import ( "github.com/jackc/pgtype" "github.com/jackc/pgtype/testutil" + "github.com/stretchr/testify/require" ) func TestTimestamptzTranscode(t *testing.T) { @@ -77,6 +78,13 @@ func TestTimestamptzNanosecondsTruncated(t *testing.T) { } } +// https://github.com/jackc/pgtype/issues/74 +func TestTimestamptzDecodeTextInvalid(t *testing.T) { + tstz := &pgtype.Timestamptz{} + err := tstz.DecodeText(nil, []byte(`eeeee`)) + require.Error(t, err) +} + func TestTimestamptzSet(t *testing.T) { type _time time.Time diff --git a/tstzrange_test.go b/tstzrange_test.go index b3d3ff6c..f8e2c2c5 100644 --- a/tstzrange_test.go +++ b/tstzrange_test.go @@ -6,6 +6,7 @@ import ( "github.com/jackc/pgtype" "github.com/jackc/pgtype/testutil" + "github.com/stretchr/testify/require" ) func TestTstzrangeTranscode(t *testing.T) { @@ -39,3 +40,10 @@ func TestTstzrangeTranscode(t *testing.T) { a.Upper.InfinityModifier == b.Upper.InfinityModifier }) } + +// https://github.com/jackc/pgtype/issues/74 +func TestTstzRangeDecodeTextInvalid(t *testing.T) { + tstzrange := &pgtype.Tstzrange{} + err := tstzrange.DecodeText(nil, []byte(`[eeee,)`)) + require.Error(t, err) +}