From e15e0ddbf5a8e7e11408ed1ecac14a6a8495227a Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 25 May 2018 17:31:14 +0200 Subject: [PATCH] Fix detection for missing parameter in substitution `${}`, `${:}` and so on are invalid because there's no parameter within the brackets; fix detection for this situation and add/update tests. There were some existing test-cases that were testing for the wrong behavior, which are now updated. Signed-off-by: Sebastiaan van Stijn rewritten from github.com/moby/moby 334bf3ea76004d0abe02dd1698989f9eaf87a86a --- frontend/dockerfile/shell/envVarTest | 15 +++++++++++---- frontend/dockerfile/shell/lex.go | 17 ++++++++--------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/frontend/dockerfile/shell/envVarTest b/frontend/dockerfile/shell/envVarTest index 677b33d9..2c0cc1fc 100644 --- a/frontend/dockerfile/shell/envVarTest +++ b/frontend/dockerfile/shell/envVarTest @@ -29,10 +29,14 @@ A|he\$PWD | he$PWD A|he\\$PWD | he\/home A|"he\$PWD" | he$PWD A|"he\\$PWD" | he\/home +A|\${} | ${} +A|\${}aaa | ${}aaa A|he\${} | he${} A|he\${}xx | he${}xx -A|he${} | he -A|he${}xx | hexx +A|${} | error +A|${}aaa | error +A|he${} | error +A|he${}xx | error A|he${hi} | he A|he${hi}xx | hexx A|he${PWD} | he/home @@ -88,8 +92,8 @@ A|안녕\$PWD | 안녕$PWD A|안녕\\$PWD | 안녕\/home A|안녕\${} | 안녕${} A|안녕\${}xx | 안녕${}xx -A|안녕${} | 안녕 -A|안녕${}xx | 안녕xx +A|안녕${} | error +A|안녕${}xx | error A|안녕${hi} | 안녕 A|안녕${hi}xx | 안녕xx A|안녕${PWD} | 안녕/home @@ -129,4 +133,7 @@ A|${aaa:-bbb} | bbb A|${aaa:-${bbb:-ccc}} | ccc A|${aaa:-bbb ${foo} | error A|${aaa:-bbb {foo} | bbb {foo +A|${:} | error +A|${:-bbb} | error +A|${:+bbb} | error diff --git a/frontend/dockerfile/shell/lex.go b/frontend/dockerfile/shell/lex.go index d0d6f53e..983e5c05 100644 --- a/frontend/dockerfile/shell/lex.go +++ b/frontend/dockerfile/shell/lex.go @@ -261,23 +261,22 @@ func (sw *shellWord) processDollar() (string, error) { } sw.scanner.Next() + switch sw.scanner.Peek() { + case scanner.EOF: + return "", errors.New("syntax error: missing '}'") + case '{', '}', ':': + // Invalid ${{xx}, ${:xx}, ${:}. ${} case + return "", errors.New("syntax error: bad substitution") + } name := sw.processName() - ch := sw.scanner.Peek() + ch := sw.scanner.Next() switch ch { case '}': // Normal ${xx} case - sw.scanner.Next() return sw.getEnv(name), nil - case '{': - // Invalid ${{xx} case - return "", errors.New("syntax error: bad substitution") - case scanner.EOF: - return "", errors.New("syntax error: missing '}'") case ':': // Special ${xx:...} format processing // Yes it allows for recursive $'s in the ... spot - - sw.scanner.Next() // skip over : modifier := sw.scanner.Next() word, _, err := sw.processStopOn('}')