mirror of https://github.com/docker/cli.git
Merge pull request #3670 from thaJeztah/fix_quotes_panic
opts: fix potential panic in trimQuotes
This commit is contained in:
commit
2280a9d5a7
|
@ -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 {
|
||||||
|
|
|
@ -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()))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue