Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pkg/workflow/workflow_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,11 @@ func mergeExcludedEnvVarNames(fromImports, fromMain []string) []string {
if len(fromImports) == 0 && len(fromMain) == 0 {
return nil
}
seen := make(map[string]bool, len(fromImports)+len(fromMain))
merged := make([]string, 0, len(fromImports)+len(fromMain))
// Use max() for capacity hints: overflow-safe (no addition) and a tighter
// lower-bound than either length alone.
hint := max(len(fromImports), len(fromMain))
seen := make(map[string]bool, hint)
merged := make([]string, 0, hint)
for _, name := range fromImports {
if !seen[name] {
seen[name] = true
Expand Down
21 changes: 21 additions & 0 deletions pkg/workflow/workflow_builder_model_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package workflow

import (
"fmt"
"sort"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -161,3 +163,22 @@ func TestMergeExcludedEnvVarNames_DeduplicatesAcrossSources(t *testing.T) {
)
assert.Equal(t, []string{"IMPORT_ONLY", "MAIN_ONLY", "SHARED"}, got)
}

func TestMergeExcludedEnvVarNames_LargeInputs(t *testing.T) {
// Exercises the code path that triggered CWE-190 alerts #648/#649.
n := 100_000
fromImports := make([]string, n)
fromMain := make([]string, n)
for i := range fromImports {
fromImports[i] = fmt.Sprintf("IMPORT_%d", i)
fromMain[i] = fmt.Sprintf("MAIN_%d", i)
}
got := mergeExcludedEnvVarNames(fromImports, fromMain)
assert.Len(t, got, 2*n)
assert.True(t, sort.StringsAreSorted(got))
// Confirm both sources are represented in the result.
assert.Contains(t, got, "IMPORT_0")
assert.Contains(t, got, fmt.Sprintf("IMPORT_%d", n-1))
assert.Contains(t, got, "MAIN_0")
assert.Contains(t, got, fmt.Sprintf("MAIN_%d", n-1))
}
Loading