From 5a70811dae3f7914086ac8bf484dbe06e01b7ccd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 7 Aug 2023 21:49:11 +0200 Subject: [PATCH] Fix a possible division by zero MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Zero-layer artifacts are discouraged but allowed. Signed-off-by: Miloslav Trmač --- copy/single.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/copy/single.go b/copy/single.go index dff7aad22f..bb58ae6531 100644 --- a/copy/single.go +++ b/copy/single.go @@ -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 {