mirror of https://github.com/docker/cli.git
opts: fix potential panic in trimQuotes
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
c59773f155
commit
a51ea675b2
|
@ -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