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
... ...
@@ -438,12 +438,11 @@ func run(req dispatchRequest) error {
438 438
 	// these args are transparent so resulting image should be the same regardless of the value
439 439
 	if len(cmdBuildEnv) > 0 {
440 440
 		saveCmd = config.Cmd
441
-		tmpBuildEnv := make([]string, len(cmdBuildEnv))
442
-		copy(tmpBuildEnv, cmdBuildEnv)
443
-		for i, env := range tmpBuildEnv {
441
+		var tmpBuildEnv []string
442
+		for _, env := range cmdBuildEnv {
444 443
 			key := strings.SplitN(env, "=", 2)[0]
445
-			if req.builder.buildArgs.IsUnreferencedBuiltin(key) {
446
-				tmpBuildEnv = append(tmpBuildEnv[:i], tmpBuildEnv[i+1:]...)
444
+			if !req.builder.buildArgs.IsUnreferencedBuiltin(key) {
445
+				tmpBuildEnv = append(tmpBuildEnv, env)
447 446
 			}
448 447
 		}
449 448
 		sort.Strings(tmpBuildEnv)
... ...
@@ -4355,7 +4355,8 @@ func (s *DockerSuite) TestBuildTimeArgHistoryExclusions(c *check.C) {
4355 4355
 		ARG %s
4356 4356
 		RUN echo "Testing Build Args!"`, envKey, explicitProxyKey)
4357 4357
 	buildImage(imgName,
4358
-		cli.WithFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
4358
+		cli.WithFlags("--build-arg", "https_proxy=https://proxy.example.com",
4359
+			"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
4359 4360
 			"--build-arg", fmt.Sprintf("%s=%s", explicitProxyKey, explicitProxyVal),
4360 4361
 			"--build-arg", proxy),
4361 4362
 		build.WithDockerfile(dockerfile),
... ...
@@ -4365,6 +4366,9 @@ func (s *DockerSuite) TestBuildTimeArgHistoryExclusions(c *check.C) {
4365 4365
 	if strings.Contains(out, proxy) {
4366 4366
 		c.Fatalf("failed to exclude proxy settings from history!")
4367 4367
 	}
4368
+	if strings.Contains(out, "https_proxy") {
4369
+		c.Fatalf("failed to exclude proxy settings from history!")
4370
+	}
4368 4371
 	if !strings.Contains(out, fmt.Sprintf("%s=%s", envKey, envVal)) {
4369 4372
 		c.Fatalf("explicitly defined ARG %s is not in output", explicitProxyKey)
4370 4373
 	}