Skip to content
This repository was archived by the owner on May 4, 2021. It is now read-only.

Commit 27daadc

Browse files
author
Yiran Wang
authored
Fix parsing panic if variables are not defined (#269)
* Fix parsing panic if variables are not defined * fix * fix
1 parent e2aabf9 commit 27daadc

File tree

3 files changed

+6
-0
lines changed

3 files changed

+6
-0
lines changed

lib/parser/dockerfile/base.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package dockerfile
1616

1717
import (
18+
"errors"
1819
"fmt"
1920
"regexp"
2021
"strings"
@@ -70,6 +71,9 @@ func (d *baseDirective) replaceVars(vars map[string]string) error {
7071
if err != nil {
7172
return d.err(fmt.Errorf("Failed to replace variables in input: %s", err))
7273
}
74+
if len(replaced) == 0 {
75+
return d.err(errors.New("Empty args after replacing variables"))
76+
}
7377
d.Args = replaced
7478
return nil
7579
}

lib/parser/dockerfile/copy_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func TestNewCopyDirective(t *testing.T) {
3434
chown string
3535
}{
3636
{"missing args", false, "copy ", nil, "", "", ""},
37+
{"missing args after replace", false, "copy \\", nil, "", "", ""},
3738
{"shell single source", true, `copy src dst`, []string{"src"}, "dst", "", ""},
3839
{"shell multi source", true, `copy src1 src2 dst`, []string{"src1", "src2"}, "dst", "", ""},
3940
{"shell substitution", true, `copy src1 ${prefix}src2 dst$suffix`, []string{"src1", "test_src2"}, "dst_test", "", ""},

lib/parser/dockerfile/from_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func TestNewFromDirective(t *testing.T) {
4343
{"bad 'as'", false, "from test_image:trusty sa test_alias", "", ""},
4444
{"substitution", true, "from ${prefix}image:trusty as alias$suffix", "test_image:trusty", "alias_test"},
4545
{"bad substitution", false, "from ${prefiximage:trusty as alias$suffix", "", ""},
46+
{"empty substitution", false, "from ${0:+0}", "test_image", ""},
4647
}
4748

4849
for _, test := range tests {

0 commit comments

Comments
 (0)