Skip to content

Commit

Permalink
fix: possible race condition when applying templates to flags/ldflags
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Dec 21, 2022
1 parent ac2df47 commit 6a5b474
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,22 +699,23 @@ func createTemplateData() map[string]interface{} {
}
}

func applyTemplating(list []string, data map[string]interface{}) error {
for i, entry := range list {
func applyTemplating(list []string, data map[string]interface{}) ([]string, error) {
result := make([]string, len(list), 0)
for _, entry := range list {
tmpl, err := template.New("argsTmpl").Option("missingkey=error").Parse(entry)
if err != nil {
return err
return nil, err
}

var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return err
return nil, err
}

list[i] = buf.String()
result = append(result, buf.String())
}

return nil
return result, nil
}

func createBuildArgs(buildCfg Config) ([]string, error) {
Expand All @@ -723,19 +724,21 @@ func createBuildArgs(buildCfg Config) ([]string, error) {
data := createTemplateData()

if len(buildCfg.Flags) > 0 {
if err := applyTemplating(buildCfg.Flags, data); err != nil {
flags, err := applyTemplating(buildCfg.Flags, data)
if err != nil {
return nil, err
}

args = append(args, buildCfg.Flags...)
args = append(args, flags...)
}

if len(buildCfg.Ldflags) > 0 {
if err := applyTemplating(buildCfg.Ldflags, data); err != nil {
ldflags, err := applyTemplating(buildCfg.Ldflags, data)
if err != nil {
return nil, err
}

args = append(args, fmt.Sprintf("-ldflags=%s", strings.Join(buildCfg.Ldflags, " ")))
args = append(args, fmt.Sprintf("-ldflags=%s", strings.Join(ldflags, " ")))
}

// Reject any flags that attempt to set --toolexec (with or
Expand Down

0 comments on commit 6a5b474

Please sign in to comment.