Skip to content

Commit

Permalink
Merge pull request #4772 from MichaelEischer/better-error-on-too-larg…
Browse files Browse the repository at this point in the history
…e-blob

repository: Better error message if blob is larger than 4GB
  • Loading branch information
MichaelEischer committed Apr 22, 2024
2 parents 228b35f + 10355c3 commit f7632de
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 2 additions & 3 deletions internal/index/index.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"math"
"sync"
"time"

Expand Down Expand Up @@ -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!")
}

Expand Down
5 changes: 5 additions & 0 deletions internal/repository/repository.go
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"fmt"
"io"
"math"
"os"
"runtime"
"sort"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f7632de

Please sign in to comment.