Skip to content

Commit

Permalink
Fix a possible division by zero
Browse files Browse the repository at this point in the history
Zero-layer artifacts are discouraged but allowed.

Signed-off-by: Miloslav Trmač <[email protected]>
  • Loading branch information
mtrmac committed Aug 7, 2023
1 parent 5497eae commit 5a70811
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions copy/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,14 @@ func (ic *imageCopier) copyLayers(ctx context.Context) ([]compressiontypes.Algor
encryptAll = len(*ic.c.options.OciEncryptLayers) == 0
totalLayers := len(srcInfos)
for _, l := range *ic.c.options.OciEncryptLayers {
// if layer is negative, it is reverse indexed.
layersToEncrypt.Add((totalLayers + l) % totalLayers)
switch {
case l >= 0 && l < totalLayers:
layersToEncrypt.Add(l)
case l < 0 && l+totalLayers >= 0: // Implies (l + totalLayers) < totalLayers
layersToEncrypt.Add(l + totalLayers) // If l is negative, it is reverse indexed.
default:
return nil, fmt.Errorf("when choosing layers to encrypt, layer index %d out of range (%d layers exist)", l, totalLayers)
}
}

if encryptAll {
Expand Down

0 comments on commit 5a70811

Please sign in to comment.