Browse code

builder: Make builtin arg pruning work with > 1 arg

The previous implementation would error out with "Unexpected EOF" which
was caused by an underlying "array index out-of-bounds" error.
The root cause was deleting items from the same array that was being
iterated over. The iteration was unaware that the array size had
changed, resulting in an error.

The new implementation builds a new array instead of mutating a copy of
the old one.

Fixes: #32744

Signed-off-by: Dave Tucker <dt@docker.com>

Dave Tucker authored on 2017/04/21 21:14:21
Showing 2 changed files
... ...
@@ -449,12 +449,11 @@ func run(b *Builder, args []string, attributes map[string]bool, original string)
449 449
 	// these args are transparent so resulting image should be the same regardless of the value
450 450
 	if len(cmdBuildEnv) > 0 {
451 451
 		saveCmd = config.Cmd
452
-		tmpBuildEnv := make([]string, len(cmdBuildEnv))
453
-		copy(tmpBuildEnv, cmdBuildEnv)
454
-		for i, env := range tmpBuildEnv {
452
+		var tmpBuildEnv []string
453
+		for _, env := range cmdBuildEnv {
455 454
 			key := strings.SplitN(env, "=", 2)[0]
456
-			if b.buildArgs.IsUnreferencedBuiltin(key) {
457
-				tmpBuildEnv = append(tmpBuildEnv[:i], tmpBuildEnv[i+1:]...)
455
+			if !b.buildArgs.IsUnreferencedBuiltin(key) {
456
+				tmpBuildEnv = append(tmpBuildEnv, env)
458 457
 			}
459 458
 		}
460 459
 		sort.Strings(tmpBuildEnv)
... ...
@@ -4345,7 +4345,8 @@ func (s *DockerSuite) TestBuildTimeArgHistoryExclusions(c *check.C) {
4345 4345
 		ARG %s
4346 4346
 		RUN echo "Testing Build Args!"`, envKey, explicitProxyKey)
4347 4347
 	buildImage(imgName,
4348
-		cli.WithFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
4348
+		cli.WithFlags("--build-arg", "https_proxy=https://proxy.example.com",
4349
+			"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
4349 4350
 			"--build-arg", fmt.Sprintf("%s=%s", explicitProxyKey, explicitProxyVal),
4350 4351
 			"--build-arg", proxy),
4351 4352
 		build.WithDockerfile(dockerfile),
... ...
@@ -4355,6 +4356,9 @@ func (s *DockerSuite) TestBuildTimeArgHistoryExclusions(c *check.C) {
4355 4355
 	if strings.Contains(out, proxy) {
4356 4356
 		c.Fatalf("failed to exclude proxy settings from history!")
4357 4357
 	}
4358
+	if strings.Contains(out, "https_proxy") {
4359
+		c.Fatalf("failed to exclude proxy settings from history!")
4360
+	}
4358 4361
 	if !strings.Contains(out, fmt.Sprintf("%s=%s", envKey, envVal)) {
4359 4362
 		c.Fatalf("explicitly defined ARG %s is not in output", explicitProxyKey)
4360 4363
 	}
... ...
@@ -6156,7 +6160,7 @@ func (s *DockerSuite) TestBuildCopyFromPreviousFromWindows(c *check.C) {
6156 6156
 func (s *DockerSuite) TestBuildCopyFromForbidWindowsSystemPaths(c *check.C) {
6157 6157
 	testRequires(c, DaemonIsWindows)
6158 6158
 	dockerfile := `
6159
-		FROM ` + testEnv.MinimalBaseImage() + `		
6159
+		FROM ` + testEnv.MinimalBaseImage() + `
6160 6160
 		FROM ` + testEnv.MinimalBaseImage() + `
6161 6161
 		COPY --from=0 %s c:\\oscopy
6162 6162
 		`
... ...
@@ -6173,7 +6177,7 @@ func (s *DockerSuite) TestBuildCopyFromForbidWindowsSystemPaths(c *check.C) {
6173 6173
 func (s *DockerSuite) TestBuildCopyFromForbidWindowsRelativePaths(c *check.C) {
6174 6174
 	testRequires(c, DaemonIsWindows)
6175 6175
 	dockerfile := `
6176
-		FROM ` + testEnv.MinimalBaseImage() + `		
6176
+		FROM ` + testEnv.MinimalBaseImage() + `
6177 6177
 		FROM ` + testEnv.MinimalBaseImage() + `
6178 6178
 		COPY --from=0 %s c:\\oscopy
6179 6179
 		`
... ...
@@ -6192,7 +6196,7 @@ func (s *DockerSuite) TestBuildCopyFromWindowsIsCaseInsensitive(c *check.C) {
6192 6192
 	testRequires(c, DaemonIsWindows)
6193 6193
 	dockerfile := `
6194 6194
 		FROM ` + testEnv.MinimalBaseImage() + `
6195
-		COPY foo /	
6195
+		COPY foo /
6196 6196
 		FROM ` + testEnv.MinimalBaseImage() + `
6197 6197
 		COPY --from=0 c:\\fOo c:\\copied
6198 6198
 		RUN type c:\\copied