From 5d96e09628ac93d2f92903d940546dcc95e11cbb Mon Sep 17 00:00:00 2001 From: Sylvain Baubeau Date: Tue, 11 Jan 2022 18:33:39 +0100 Subject: [PATCH] Do not panic on empty quoted string argument Signed-off-by: Sylvain Baubeau --- opts/quotedstring.go | 4 ++++ opts/quotedstring_test.go | 12 ++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/opts/quotedstring.go b/opts/quotedstring.go index 6c889070e8..34f30971e4 100644 --- a/opts/quotedstring.go +++ b/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 { diff --git a/opts/quotedstring_test.go b/opts/quotedstring_test.go index 8ee603adac..fc7b7747b1 100644 --- a/opts/quotedstring_test.go +++ b/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("'")) +}