]> Cypherpunks repositories - gostls13.git/commitdiff
strconv: check for empty string in UnquoteChar
authorRebecca Stambler <rstambler@golang.org>
Mon, 4 Jun 2018 19:12:45 +0000 (15:12 -0400)
committerHeschi Kreinick <heschi@google.com>
Mon, 4 Jun 2018 22:06:42 +0000 (22:06 +0000)
The existing implementation panics on malformed input of an empty
string. strconv.Unquote validates the length of the inputs, but calling
UnquoteChar directly with an empty string leads to a panic, so add a
check for length. Also, add a test to go/constant to ensure that
MakeFromLiteral does not panic on malformed input such as
"const x = ''".

Change-Id: I4217e38db48a09a21ec414bbfb3087709da62904
Reviewed-on: https://go-review.googlesource.com/116215
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/constant/value_test.go
src/strconv/quote.go

index 5ec4f4c41853499140c6e559beb17b2443222bb2..e6fca76e182bc8f15b866015c0e54c963329b35c 100644 (file)
@@ -431,6 +431,7 @@ func TestUnknown(t *testing.T) {
                MakeBool(false), // token.ADD ok below, operation is never considered
                MakeString(""),
                MakeInt64(1),
+               MakeFromLiteral("''", token.CHAR, 0),
                MakeFromLiteral("-1234567890123456789012345678901234567890", token.INT, 0),
                MakeFloat64(1.2),
                MakeImag(MakeFloat64(1.2)),
index d514b5f552795e3d0d66fa8574ab59054b023ddb..9b7194a0f041d456de6d51c6b16174f9b6062df8 100644 (file)
@@ -237,6 +237,10 @@ func unhex(b byte) (v rune, ok bool) {
 // If set to zero, it does not permit either escape and allows both quote characters to appear unescaped.
 func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error) {
        // easy cases
+       if len(s) == 0 {
+               err = ErrSyntax
+               return
+       }
        switch c := s[0]; {
        case c == quote && (quote == '\'' || quote == '"'):
                err = ErrSyntax