From a51ea675b227cf36a411068036f3787b4897ea30 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 9 Jun 2022 07:59:31 +0200 Subject: [PATCH] opts: fix potential panic in trimQuotes Signed-off-by: Sebastiaan van Stijn --- opts/quotedstring.go | 3 +++ opts/quotedstring_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/opts/quotedstring.go b/opts/quotedstring.go index 09c68a5261..741f450b14 100644 --- a/opts/quotedstring.go +++ b/opts/quotedstring.go @@ -22,6 +22,9 @@ 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 95b56f2b83..f56868b1c6 100644 --- a/opts/quotedstring_test.go +++ b/opts/quotedstring_test.go @@ -28,3 +28,13 @@ func TestQuotedStringSetWithNoQuotes(t *testing.T) { assert.NilError(t, qs.Set("something")) assert.Check(t, is.Equal("something", qs.String())) } + +func TestQuotedStringShort(t *testing.T) { + value := "" + qs := NewQuotedString(&value) + assert.NilError(t, qs.Set(`"`)) + assert.Check(t, is.Equal(`"`, qs.String())) + + assert.NilError(t, qs.Set(`'`)) + assert.Check(t, is.Equal(`'`, qs.String())) +}