opts: fix potential panic in trimQuotes

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-06-09 07:59:31 +02:00
parent c59773f155
commit a51ea675b2
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 13 additions and 0 deletions

View File

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

View File

@ -28,3 +28,13 @@ func TestQuotedStringSetWithNoQuotes(t *testing.T) {
assert.NilError(t, qs.Set("something")) assert.NilError(t, qs.Set("something"))
assert.Check(t, is.Equal("something", qs.String())) 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()))
}