diff --git a/pgtype/hstore.go b/pgtype/hstore.go index 5de87da98..52fe23060 100644 --- a/pgtype/hstore.go +++ b/pgtype/hstore.go @@ -428,12 +428,21 @@ func parseHstore(s string) (k []string, v []Text, err error) { r, end = p.Consume() switch { case end: - err = errors.New("Found EOS after ',', expcting space") + err = errors.New("Found EOS after ',', expecting space") case (unicode.IsSpace(r)): + // after space is a doublequote to start the key r, end = p.Consume() + if end { + err = errors.New("Found EOS after space, expecting \"") + return + } + if r != '"' { + err = fmt.Errorf("Invalid character '%c' after space, expecting \"", r) + return + } state = hsKey default: - err = fmt.Errorf("Invalid character '%c' after ', ', expecting \"", r) + err = fmt.Errorf("Invalid character '%c' after ',', expecting space", r) } } else { err = fmt.Errorf("Invalid character '%c' after value, expecting ','", r) diff --git a/pgtype/hstore_test.go b/pgtype/hstore_test.go index 5b1b5859c..8ad5884a8 100644 --- a/pgtype/hstore_test.go +++ b/pgtype/hstore_test.go @@ -230,3 +230,18 @@ func TestHstoreCodec(t *testing.T) { } }) } + +func TestParseInvalidInputs(t *testing.T) { + // these inputs should be invalid, but previously were considered correct + invalidInputs := []string{ + `"a"=>"1", ,b"=>"2"`, + `""=>"", 0"=>""`, + } + for i, input := range invalidInputs { + var hstore pgtype.Hstore + err := hstore.Scan(input) + if err == nil { + t.Errorf("test %d: input=%s (%#v) should fail; parsed correctly", i, input, input) + } + } +}