Skip to content

Commit

Permalink
frontend/dockerfile: BFlags.Parse: return earlier on "--" terminator
Browse files Browse the repository at this point in the history
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 <github@gone.nl>
  • Loading branch information
thaJeztah committed Sep 30, 2024
1 parent 9cdd15e commit b300989
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions frontend/dockerfile/instructions/bflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit b300989

Please sign in to comment.