remotecache: fix possible panic when merging loop candidates

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2025-09-02 21:21:39 -07:00
parent 051818cf38
commit e4ee7622d1
2 changed files with 12 additions and 2 deletions

View File

@@ -116,6 +116,12 @@ func (c *CacheChains) Add(dgst digest.Digest, deps [][]solver.CacheLink, results
continue
}
for l, m := range it.children {
if main.children == nil {
main.children = map[unique.Handle[linkv2]]map[*item]struct{}{}
}
if _, ok := main.children[l]; !ok {
main.children[l] = map[*item]struct{}{}
}
for ch := range m {
main.children[l][ch] = struct{}{}
for i, links := range ch.parents {
@@ -126,7 +132,7 @@ func (c *CacheChains) Add(dgst digest.Digest, deps [][]solver.CacheLink, results
}
newlinks[l] = struct{}{}
}
main.parents[i] = newlinks
ch.parents[i] = newlinks
}
}
}
@@ -143,7 +149,7 @@ func (c *CacheChains) Add(dgst digest.Digest, deps [][]solver.CacheLink, results
r.addResult(rr)
}
// make sure that none of the deps are childeren of r
// make sure that none of the deps are children of r
allChildren := map[*item]struct{}{}
if err := r.walkChildren(func(i *item) error {
allChildren[i] = struct{}{}

View File

@@ -179,6 +179,7 @@ func marshalItem(ctx context.Context, it *item, state *marshalState) error {
if _, ok := state.recordsByItem[it]; ok {
return nil
}
state.recordsByItem[it] = -1
rec := CacheRecord{
Digest: it.dgst,
@@ -194,6 +195,9 @@ func marshalItem(ctx context.Context, it *item, state *marshalState) error {
if !ok {
return errors.Errorf("invalid source record: %v", l.src)
}
if idx == -1 {
continue
}
rec.Inputs[i] = append(rec.Inputs[i], CacheInput{
Selector: l.selector,
LinkIndex: idx,