From 9ddb3b667ce1ed0a66c361ad6767ee72d952ad66 Mon Sep 17 00:00:00 2001 From: Yiran Wang Date: Thu, 19 Sep 2019 21:07:41 -0700 Subject: [PATCH 1/3] Fix parsing panic if variables are not defined --- lib/parser/dockerfile/base.go | 3 +++ lib/parser/dockerfile/copy_test.go | 1 + lib/parser/dockerfile/from_test.go | 1 + 3 files changed, 5 insertions(+) diff --git a/lib/parser/dockerfile/base.go b/lib/parser/dockerfile/base.go index 68682b59..83ccb161 100644 --- a/lib/parser/dockerfile/base.go +++ b/lib/parser/dockerfile/base.go @@ -70,6 +70,9 @@ func (d *baseDirective) replaceVars(vars map[string]string) error { if err != nil { return d.err(fmt.Errorf("Failed to replace variables in input: %s", err)) } + if len(replaced) == 0 { + return d.err(fmt.Errorf("Empty args after replacing variables: %s", err)) + } d.Args = replaced return nil } diff --git a/lib/parser/dockerfile/copy_test.go b/lib/parser/dockerfile/copy_test.go index 01ebfbe8..c4869332 100644 --- a/lib/parser/dockerfile/copy_test.go +++ b/lib/parser/dockerfile/copy_test.go @@ -34,6 +34,7 @@ func TestNewCopyDirective(t *testing.T) { chown string }{ {"missing args", false, "copy ", nil, "", "", ""}, + {"missing args after replace", false, "copy \\", nil, "", "", ""}, {"shell single source", true, `copy src dst`, []string{"src"}, "dst", "", ""}, {"shell multi source", true, `copy src1 src2 dst`, []string{"src1", "src2"}, "dst", "", ""}, {"shell substitution", true, `copy src1 ${prefix}src2 dst$suffix`, []string{"src1", "test_src2"}, "dst_test", "", ""}, diff --git a/lib/parser/dockerfile/from_test.go b/lib/parser/dockerfile/from_test.go index 0b78a2a7..75dd3d35 100644 --- a/lib/parser/dockerfile/from_test.go +++ b/lib/parser/dockerfile/from_test.go @@ -43,6 +43,7 @@ func TestNewFromDirective(t *testing.T) { {"bad 'as'", false, "from test_image:trusty sa test_alias", "", ""}, {"substitution", true, "from ${prefix}image:trusty as alias$suffix", "test_image:trusty", "alias_test"}, {"bad substitution", false, "from ${prefiximage:trusty as alias$suffix", "", ""}, + {"empty substitution", false, "from ${0:+0}", "test_image", ""}, } for _, test := range tests { From bd5bcb827a13ed3d3c8484f530e4c3390dd028f1 Mon Sep 17 00:00:00 2001 From: Yiran Wang Date: Thu, 19 Sep 2019 21:14:42 -0700 Subject: [PATCH 2/3] fix --- lib/parser/dockerfile/base.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/parser/dockerfile/base.go b/lib/parser/dockerfile/base.go index 83ccb161..a777d51c 100644 --- a/lib/parser/dockerfile/base.go +++ b/lib/parser/dockerfile/base.go @@ -15,6 +15,7 @@ package dockerfile import ( + "errors" "fmt" "regexp" "strings" @@ -71,7 +72,7 @@ func (d *baseDirective) replaceVars(vars map[string]string) error { return d.err(fmt.Errorf("Failed to replace variables in input: %s", err)) } if len(replaced) == 0 { - return d.err(fmt.Errorf("Empty args after replacing variables: %s", err)) + return d.err(errors.New("Empty args after replacing variables: %s")) } d.Args = replaced return nil From c4b5232f2daebea63a8aa0f19c2057738e25182d Mon Sep 17 00:00:00 2001 From: Yiran Wang Date: Thu, 19 Sep 2019 21:15:27 -0700 Subject: [PATCH 3/3] fix --- lib/parser/dockerfile/base.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parser/dockerfile/base.go b/lib/parser/dockerfile/base.go index a777d51c..cae09019 100644 --- a/lib/parser/dockerfile/base.go +++ b/lib/parser/dockerfile/base.go @@ -72,7 +72,7 @@ func (d *baseDirective) replaceVars(vars map[string]string) error { return d.err(fmt.Errorf("Failed to replace variables in input: %s", err)) } if len(replaced) == 0 { - return d.err(errors.New("Empty args after replacing variables: %s")) + return d.err(errors.New("Empty args after replacing variables")) } d.Args = replaced return nil