2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 14:56:29 +00:00

Merge pull request #3789 from greatroar/fix-loadblob

internal/repository: Fix LoadBlob + fuzz test
This commit is contained in:
Alexander Neumann 2022-06-06 19:33:42 +02:00 committed by GitHub
commit 07114ccb21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 1 deletions

2
.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
# Workaround for https://github.com/golang/go/issues/52268.
**/testdata/fuzz/*/* eol=lf

View File

@ -0,0 +1,43 @@
//go:build go1.18
// +build go1.18
package repository
import (
"context"
"testing"
"github.com/restic/restic/internal/backend/mem"
"github.com/restic/restic/internal/restic"
)
// Test saving a blob and loading it again, with varying buffer sizes.
// Also a regression test for #3783.
func FuzzSaveLoadBlob(f *testing.F) {
f.Fuzz(func(t *testing.T, blob []byte, buflen uint) {
if buflen > 64<<20 {
// Don't allocate enormous buffers. We're not testing the allocator.
t.Skip()
}
id := restic.Hash(blob)
repo, _ := TestRepositoryWithBackend(t, mem.New(), 2)
_, _, err := repo.SaveBlob(context.TODO(), restic.DataBlob, blob, id, false)
if err != nil {
t.Fatal(err)
}
err = repo.Flush(context.TODO())
if err != nil {
t.Fatal(err)
}
buf, err := repo.LoadBlob(context.TODO(), restic.DataBlob, id, make([]byte, buflen))
if err != nil {
t.Fatal(err)
}
if restic.Hash(buf) != id {
t.Fatal("mismatch")
}
})
}

View File

@ -303,8 +303,9 @@ func (r *Repository) LoadBlob(ctx context.Context, t restic.BlobType, id restic.
return plaintext, nil return plaintext, nil
} }
// move decrypted data to the start of the buffer // move decrypted data to the start of the buffer
buf = buf[:len(plaintext)]
copy(buf, plaintext) copy(buf, plaintext)
return buf[:len(plaintext)], nil return buf, nil
} }
if lastError != nil { if lastError != nil {

View File

@ -0,0 +1,3 @@
go test fuzz v1
[]byte("\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6\xc6")
uint(109)