Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid nil pointer dereference when copying from image with no layers #2197

Merged
merged 5 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions cache/contenthash/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ type cacheManager struct {
}

func (cm *cacheManager) Checksum(ctx context.Context, ref cache.ImmutableRef, p string, opts ChecksumOpts, s session.Group) (digest.Digest, error) {
if ref == nil {
if p == "/" {
return digest.FromBytes(nil), nil
}
return "", errors.Errorf("cannot checksum empty reference")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: probably better to return the same "not found" error that would happen if p does not exist. "empty reference" is an internal concept, users of build don't know what it means

}
cc, err := cm.GetCacheContext(ctx, ensureOriginMetadata(ref.Metadata()), ref.IdentityMapping())
if err != nil {
return "", nil
Expand Down
34 changes: 34 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestIntegration(t *testing.T) {
testBasicRegistryCacheImportExport,
testBasicLocalCacheImportExport,
testCachedMounts,
testCopyFromScratch,
testProxyEnv,
testLocalSymlinkEscape,
testTmpfsMounts,
Expand Down Expand Up @@ -2944,6 +2945,39 @@ func testCacheMountNoCache(t *testing.T, sb integration.Sandbox) {
require.NoError(t, err)
}

func testCopyFromScratch(t *testing.T, sb integration.Sandbox) {
requiresLinux(t)
c, err := New(sb.Context(), sb.Address())
require.NoError(t, err)
defer c.Close()

st := llb.Scratch().File(llb.Copy(llb.Scratch(), "/", "/"))
def, err := st.Marshal(sb.Context())
require.NoError(t, err)

_, err = c.Solve(sb.Context(), def, SolveOpt{}, nil)
require.NoError(t, err)

st = llb.Scratch().File(llb.Copy(llb.Scratch(), "/foo", "/"))
def, err = st.Marshal(sb.Context())
require.NoError(t, err)

_, err = c.Solve(sb.Context(), def, SolveOpt{}, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "/foo: no such file or directory")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regarding errors, if I change llb.Scratch() to llb.Image("tonistiigi/test:nolayers") I get "failed to solve: rpc error: code = Unknown desc = failed to compute cache key: cannot checksum empty reference" here.


busybox := llb.Image("busybox:latest")

out := busybox.Run(llb.Shlex(`sh -e -c '[ $(ls /scratch | wc -l) = '0' ]'`))
out.AddMount("/scratch", llb.Scratch(), llb.Readonly)

def, err = out.Marshal(sb.Context())
require.NoError(t, err)

_, err = c.Solve(sb.Context(), def, SolveOpt{}, nil)
require.NoError(t, err)
}

// containerd/containerd#2119
func testDuplicateWhiteouts(t *testing.T, sb integration.Sandbox) {
skipDockerd(t, sb)
Expand Down