Fix Timestamptz.DecodeText with too short text

fixes #74
non-blocking
Jack Christensen 2020-11-03 08:28:53 -06:00
parent af0ca3a39b
commit 36a8da55cc
4 changed files with 26 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)
}