Skip to content

Commit 38355b2

Browse files
authored
Merge pull request #2592 from greymoth-jp/fix-interval-text-panic
fix(pgtype): return error instead of panicking on malformed interval text
2 parents bfde1ff + 1b5e6f8 commit 38355b2

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

pgtype/interval.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (scanPlanTextAnyToIntervalScanner) Scan(src []byte, dst any) error {
232232
}
233233

234234
var negative bool
235-
if timeParts[0][0] == '-' {
235+
if len(timeParts[0]) > 0 && timeParts[0][0] == '-' {
236236
negative = true
237237
timeParts[0] = timeParts[0][1:]
238238
}

pgtype/interval_malformed_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package pgtype_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/jackc/pgx/v5/pgtype"
7+
)
8+
9+
// A malformed interval whose time component has an empty hours segment
10+
// (for example ":05:06") must return an error instead of panicking with an
11+
// index out of range on timeParts[0][0].
12+
func TestIntervalScanMalformedTimeReturnsError(t *testing.T) {
13+
for _, src := range []string{":05:06", "1 day :30:00"} {
14+
var v pgtype.Interval
15+
if err := v.Scan(src); err == nil {
16+
t.Errorf("Scan(%q): expected error, got nil", src)
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)