From bbe7d436ce5d444ecdb6a74056b2dc06244bb1f0 Mon Sep 17 00:00:00 2001 From: William Murphy Date: Wed, 4 Jul 2018 11:32:11 -0400 Subject: [PATCH] Improve bounds checking and EOF handling (#9) Some fields that were never empty in our original test image can apparently be empty in images that one finds in the wild. This adds appropriate checking so that blank file lists and missing image IDs no longer cause crashes. --- image/image.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/image/image.go b/image/image.go index 6faf921..f46fd0a 100644 --- a/image/image.go +++ b/image/image.go @@ -39,7 +39,7 @@ func NewManifest(reader *tar.Reader, header *tar.Header) ImageManifest { size := header.Size manifestBytes := make([]byte, size) _, err := reader.Read(manifestBytes) - if err != nil { + if err != nil && err != io.EOF { panic(err) } var m []ImageManifest @@ -56,7 +56,11 @@ type Layer struct { } func (layer *Layer) Id() string { - id := layer.History.ID[0:25] + rangeBound := 25 + if length := len(layer.History.ID); length < 25 { + rangeBound = length + } + id := layer.History.ID[0:rangeBound] if len(layer.History.Tags) > 0 { id = "[" + strings.Join(layer.History.Tags, ",") + "]" } @@ -140,7 +144,9 @@ func InitializeData(imageID string) ([]*Layer, []*filetree.FileTree) { for idx := 0; idx < len(layers); idx++ { layers[idx] = new(Layer) layers[idx].History = history[idx] - layers[idx].TarPath = manifest.LayerTarPaths[idx] + if len(manifest.LayerTarPaths) > idx { + layers[idx].TarPath = manifest.LayerTarPaths[idx] + } } return layers, trees @@ -198,7 +204,7 @@ func getFileList(parentReader *tar.Reader, header *tar.Header) []filetree.FileIn var tarredBytes = make([]byte, header.Size) _, err := parentReader.Read(tarredBytes) - if err != nil { + if err != nil && err != io.EOF { panic(err) } reader := bytes.NewReader(tarredBytes)