From b219a68ad957462470695bcd0333e24ea9bdf08c Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Mon, 4 Jun 2018 15:12:45 -0400 Subject: [PATCH] strconv: check for empty string in UnquoteChar 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 TryBot-Result: Gobot Gobot Reviewed-by: Robert Griesemer --- src/go/constant/value_test.go | 1 + src/strconv/quote.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/go/constant/value_test.go b/src/go/constant/value_test.go index 5ec4f4c418..e6fca76e18 100644 --- a/src/go/constant/value_test.go +++ b/src/go/constant/value_test.go @@ -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)), diff --git a/src/strconv/quote.go b/src/strconv/quote.go index d514b5f552..9b7194a0f0 100644 --- a/src/strconv/quote.go +++ b/src/strconv/quote.go @@ -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 -- 2.50.0