From b300989ca3c9c8581d5b3457e6584cdf89ec0e23 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 30 Sep 2024 14:02:38 +0200 Subject: [PATCH] frontend/dockerfile: BFlags.Parse: return earlier on "--" terminator Minor optimization; put the check for a literal "--" first, instead of matching for "--" as prefix. Also add some documentation to the code, to outline why we return early. Signed-off-by: Sebastiaan van Stijn --- frontend/dockerfile/instructions/bflag.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/frontend/dockerfile/instructions/bflag.go b/frontend/dockerfile/instructions/bflag.go index 66e50d8aad2f..42f2fdcc50ee 100644 --- a/frontend/dockerfile/instructions/bflag.go +++ b/frontend/dockerfile/instructions/bflag.go @@ -148,14 +148,21 @@ func (bf *BFlags) Parse() error { } for _, arg := range bf.Args { - if !strings.HasPrefix(arg, "--") { - return errors.Errorf("arg should start with -- : %s", arg) - } - if arg == "--" { + // Stop processing further arguments as flags. We're matching + // the POSIX Utility Syntax Guidelines here; + // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02 + // + // > The first -- argument that is not an option-argument should be accepted + // > as a delimiter indicating the end of options. Any following arguments + // > should be treated as operands, even if they begin with the '-' character. return nil } + if !strings.HasPrefix(arg, "--") { + return errors.Errorf("arg should start with -- : %s", arg) + } + arg, value, hasValue := strings.Cut(arg[2:], "=") flag, ok := bf.flags[arg]