Bladeren bron

Merge pull request #43142 from lebauce/fix-panic-on-empty-quoted-strings

Do not panic on empty quoted string argument
Tianon Gravi 3 jaren geleden
bovenliggende
commit
c94596abc9
2 gewijzigde bestanden met toevoegingen van 12 en 4 verwijderingen
  1. 4 0
      opts/quotedstring.go
  2. 8 4
      opts/quotedstring_test.go

+ 4 - 0
opts/quotedstring.go

@@ -22,6 +22,10 @@ func (s *QuotedString) String() string {
 }
 
 func trimQuotes(value string) string {
+	if len(value) < 2 {
+		return value
+	}
+
 	lastIndex := len(value) - 1
 	for _, char := range []byte{'\'', '"'} {
 		if value[0] == char && value[lastIndex] == char {

+ 8 - 4
opts/quotedstring_test.go

@@ -16,15 +16,19 @@ func TestQuotedStringSetWithQuotes(t *testing.T) {
 }
 
 func TestQuotedStringSetWithMismatchedQuotes(t *testing.T) {
-	value := ""
-	qs := NewQuotedString(&value)
+	qs := NewQuotedString(new(string))
 	assert.Check(t, qs.Set(`"something'`))
 	assert.Check(t, is.Equal(`"something'`, qs.String()))
 }
 
 func TestQuotedStringSetWithNoQuotes(t *testing.T) {
-	value := ""
-	qs := NewQuotedString(&value)
+	qs := NewQuotedString(new(string))
 	assert.Check(t, qs.Set("something"))
 	assert.Check(t, is.Equal("something", qs.String()))
 }
+
+func TestQuotedStringEmptyOrSingleCharString(t *testing.T) {
+	qs := NewQuotedString(new(string))
+	assert.Check(t, qs.Set(""))
+	assert.Check(t, qs.Set("'"))
+}