From a4b51d1197ee9a5547491a2ce0f15b23daaf1113 Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Thu, 23 Sep 2021 10:29:52 -0700 Subject: [PATCH] Fix panic in metadata content writer on copy error The `createAndCopy` function is only called when `nw.w` is nil in order to create a new writer and prepare it. The current code is attempting to close `nw.w` when there is a copy error. The correct behavior would be to close the new writer and not touch `nw.w`. Signed-off-by: Derek McGowan (cherry picked from commit b9cf0d75a9f4a8a09ab55caa1983a8988ba4834d) Signed-off-by: Derek McGowan --- metadata/content.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metadata/content.go b/metadata/content.go index a3858afec8e7..ee68ccfe1e4e 100644 --- a/metadata/content.go +++ b/metadata/content.go @@ -551,13 +551,13 @@ func (nw *namespacedWriter) createAndCopy(ctx context.Context, desc ocispec.Desc if desc.Size > 0 { ra, err := nw.provider.ReaderAt(ctx, nw.desc) if err != nil { + w.Close() return err } defer ra.Close() if err := content.CopyReaderAt(w, ra, desc.Size); err != nil { - nw.w.Close() - nw.w = nil + w.Close() return err } }