diff --git a/image/tarexport/load.go b/image/tarexport/load.go index 473eb18e56..695ddba3ee 100644 --- a/image/tarexport/load.go +++ b/image/tarexport/load.go @@ -112,12 +112,12 @@ func (l *tarexporter) loadLayer(filename string, rootFS image.RootFS) (layer.Lay logrus.Debugf("Error reading embedded tar: %v", err) return nil, err } + defer rawTar.Close() + inflatedLayerData, err := archive.DecompressStream(rawTar) if err != nil { return nil, err } - - defer rawTar.Close() defer inflatedLayerData.Close() return l.ls.Register(inflatedLayerData, rootFS.ChainID()) diff --git a/integration-cli/docker_cli_save_load_test.go b/integration-cli/docker_cli_save_load_test.go index b81a03319e..0c2f8f32bb 100644 --- a/integration-cli/docker_cli_save_load_test.go +++ b/integration-cli/docker_cli_save_load_test.go @@ -291,3 +291,12 @@ func (s *DockerSuite) TestSaveDirectoryPermissions(c *check.C) { c.Assert(found, checker.Equals, true, check.Commentf("failed to find the layer with the right content listing")) } + +// Test loading a weird image where one of the layers is of zero size. +// The layer.tar file is actually zero bytes, no padding or anything else. +// See issue: 18170 +func (s *DockerSuite) TestLoadZeroSizeLayer(c *check.C) { + testRequires(c, DaemonIsLinux) + + dockerCmd(c, "load", "-i", "fixtures/load/emptyLayer.tar") +} diff --git a/integration-cli/fixtures/load/emptyLayer.tar b/integration-cli/fixtures/load/emptyLayer.tar new file mode 100644 index 0000000000..beabb569ac Binary files /dev/null and b/integration-cli/fixtures/load/emptyLayer.tar differ diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go index 1c8e1153b3..453777865f 100644 --- a/pkg/archive/archive.go +++ b/pkg/archive/archive.go @@ -128,7 +128,13 @@ func DecompressStream(archive io.Reader) (io.ReadCloser, error) { p := pools.BufioReader32KPool buf := p.Get(archive) bs, err := buf.Peek(10) - if err != nil { + if err != nil && err != io.EOF { + // Note: we'll ignore any io.EOF error because there are some odd + // cases where the layer.tar file will be empty (zero bytes) and + // that results in an io.EOF from the Peek() call. So, in those + // cases we'll just treat it as a non-compressed stream and + // that means just create an empty layer. + // See Issue 18170 return nil, err } diff --git a/pkg/archive/archive_test.go b/pkg/archive/archive_test.go index 6c54c02d18..0b5dbb17bb 100644 --- a/pkg/archive/archive_test.go +++ b/pkg/archive/archive_test.go @@ -216,11 +216,16 @@ func TestUntarPathWithInvalidDest(t *testing.T) { invalidDestFolder := path.Join(tempFolder, "invalidDest") // Create a src file srcFile := path.Join(tempFolder, "src") - _, err = os.Create(srcFile) + tarFile := path.Join(tempFolder, "src.tar") + os.Create(srcFile) + os.Create(invalidDestFolder) // being a file (not dir) should cause an error + cmd := exec.Command("/bin/sh", "-c", "tar cf "+tarFile+" "+srcFile) + _, err = cmd.CombinedOutput() if err != nil { - t.Fatalf("Fail to create the source file") + t.Fatal(err) } - err = UntarPath(srcFile, invalidDestFolder) + + err = UntarPath(tarFile, invalidDestFolder) if err == nil { t.Fatalf("UntarPath with invalid destination path should throw an error.") }