fix panic on single-character volumes

Before this change, this would cause a panic:

    docker run -it --rm -v 1:/1 alpine
    panic: runtime error: index out of range

    goroutine 1 [running]:
    github.com/docker/cli/cli/compose/loader.isFilePath(0xc42027e058, 0x1, 0x557dcb978c20)
    ...

After this change, a correct error is returned:

    docker run -it --rm -v 1:/1 alpine
    docker: Error response from daemon: create 1: volume name is too short, names should be at least two alphanumeric characters.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-02-21 01:36:39 +01:00
parent 2a6caf214b
commit 11869fa42a
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 5 additions and 0 deletions

View File

@ -111,6 +111,9 @@ func isFilePath(source string) bool {
case '.', '/', '~': case '.', '/', '~':
return true return true
} }
if len([]rune(source)) == 1 {
return false
}
// windows named pipes // windows named pipes
if strings.HasPrefix(source, `\\`) { if strings.HasPrefix(source, `\\`) {

View File

@ -162,6 +162,8 @@ func TestParseVolumeWindowsNamedPipe(t *testing.T) {
func TestIsFilePath(t *testing.T) { func TestIsFilePath(t *testing.T) {
assert.Check(t, !isFilePath("a界")) assert.Check(t, !isFilePath("a界"))
assert.Check(t, !isFilePath("1"))
assert.Check(t, !isFilePath("c"))
} }
// Preserve the test cases for VolumeSplitN // Preserve the test cases for VolumeSplitN