From 6b5bd4b4372b8ba8f5ec0251ddffba8f360f5cf1 Mon Sep 17 00:00:00 2001 From: "Clarence \"Sparr\" Risher" Date: Tue, 27 Jun 2023 07:36:07 -0400 Subject: [PATCH] fix: improve errors for `ReadAll` and `loadIndexfile` (#526) NewWithContext refers to both the `oci-layout` and `index.json` as "OCI Image Layout", this PR fixes the latter. ReadAll is a deeper change that will improve error reporting for a lot of cases, at the cost of error volume. Some or all of that change could happen higher up to satisfy this particular issue. I think the expected/actual size output belongs here. There seem to be more than a few code paths in this and other repos that end up calling ReadAll multiple times without reporting which call produced an error so I think it's appropriate to also output the digest here, but that's less compelling. The Verify comment clears up confusion I encountered while working on this issue. "verifies size" implies checking for both too big AND too small, but it only checks if the file is bigger (or reader is longer) than expected. Fixes #432 Signed-off-by: Clarence "Sparr" Risher --- content/oci/oci.go | 2 +- content/oci/readonlyoci.go | 2 +- content/reader.go | 7 +++++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/content/oci/oci.go b/content/oci/oci.go index cf9c3f21..5b473eeb 100644 --- a/content/oci/oci.go +++ b/content/oci/oci.go @@ -102,7 +102,7 @@ func NewWithContext(ctx context.Context, root string) (*Store, error) { return nil, fmt.Errorf("invalid OCI Image Layout: %w", err) } if err := store.loadIndexFile(ctx); err != nil { - return nil, fmt.Errorf("invalid OCI Image Layout: %w", err) + return nil, fmt.Errorf("invalid OCI Image Index: %w", err) } return store, nil diff --git a/content/oci/readonlyoci.go b/content/oci/readonlyoci.go index b70b7675..f908d169 100644 --- a/content/oci/readonlyoci.go +++ b/content/oci/readonlyoci.go @@ -57,7 +57,7 @@ func NewFromFS(ctx context.Context, fsys fs.FS) (*ReadOnlyStore, error) { return nil, fmt.Errorf("invalid OCI Image Layout: %w", err) } if err := store.loadIndexFile(ctx); err != nil { - return nil, fmt.Errorf("invalid OCI Image Layout: %w", err) + return nil, fmt.Errorf("invalid OCI Image Index: %w", err) } return store, nil diff --git a/content/reader.go b/content/reader.go index 11d27b23..e575378e 100644 --- a/content/reader.go +++ b/content/reader.go @@ -70,7 +70,7 @@ func (vr *VerifyReader) Read(p []byte) (n int, err error) { return } -// Verify verifies the read content against the size and the digest. +// Verify checks for remaining unread content and verifies the read content against the digest func (vr *VerifyReader) Verify() error { if vr.verified { return nil @@ -120,7 +120,10 @@ func ReadAll(r io.Reader, desc ocispec.Descriptor) ([]byte, error) { buf := make([]byte, desc.Size) vr := NewVerifyReader(r, desc) - if _, err := io.ReadFull(vr, buf); err != nil { + if n, err := io.ReadFull(vr, buf); err != nil { + if errors.Is(err, io.ErrUnexpectedEOF) { + return nil, fmt.Errorf("read failed: expected content size of %d, got %d, for digest %s: %w", desc.Size, n, desc.Digest.String(), err) + } return nil, fmt.Errorf("read failed: %w", err) } if err := vr.Verify(); err != nil {