From 10355c3fb69db2e5740525c261fad49e0f9a451e Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 19 Apr 2024 20:48:43 +0200 Subject: [PATCH] repository: Better error message if blob is larger than 4GB --- internal/index/index.go | 5 ++--- internal/repository/repository.go | 5 +++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/index/index.go b/internal/index/index.go index 1fb2c155edd..73128f7bb21 100644 --- a/internal/index/index.go +++ b/internal/index/index.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "math" "sync" "time" @@ -69,11 +70,9 @@ func (idx *Index) addToPacks(id restic.ID) int { return len(idx.packs) - 1 } -const maxuint32 = 1<<32 - 1 - func (idx *Index) store(packIndex int, blob restic.Blob) { // assert that offset and length fit into uint32! - if blob.Offset > maxuint32 || blob.Length > maxuint32 || blob.UncompressedLength > maxuint32 { + if blob.Offset > math.MaxUint32 || blob.Length > math.MaxUint32 || blob.UncompressedLength > math.MaxUint32 { panic("offset or length does not fit in uint32. You have packs > 4GB!") } diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 8e34c712559..4198e574f84 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -6,6 +6,7 @@ import ( "context" "fmt" "io" + "math" "os" "runtime" "sort" @@ -917,6 +918,10 @@ func (r *Repository) Close() error { // occupies in the repo (compressed or not, including encryption overhead). func (r *Repository) SaveBlob(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID, storeDuplicate bool) (newID restic.ID, known bool, size int, err error) { + if int64(len(buf)) > math.MaxUint32 { + return restic.ID{}, false, 0, fmt.Errorf("blob is larger than 4GB") + } + // compute plaintext hash if not already set if id.IsNull() { // Special case the hash calculation for all zero chunks. This is especially