Create large buffer when encrypting a large tree

This fixes #80
This commit is contained in:
Alexander Neumann 2015-01-31 21:18:51 +01:00
parent 56105489a8
commit e2a407babb
2 changed files with 35 additions and 2 deletions

View File

@ -80,6 +80,26 @@ func TestEncryptDecrypt(t *testing.T) {
} }
} }
func TestSmallBuffer(t *testing.T) {
s := setupBackend(t)
defer teardownBackend(t, s)
k := setupKey(t, s, testPassword)
size := 600
data := make([]byte, size)
f, err := os.Open("/dev/urandom")
ok(t, err)
_, err = io.ReadFull(f, data)
ok(t, err)
ciphertext := make([]byte, size/2)
_, err = k.Encrypt(ciphertext, data)
// this must throw an error, since the target slice is too small
assert(t, err != nil && err == restic.ErrBufferTooSmall,
"expected restic.ErrBufferTooSmall, got %#v", err)
}
func TestLargeEncrypt(t *testing.T) { func TestLargeEncrypt(t *testing.T) {
if !*testLargeCrypto { if !*testLargeCrypto {
t.SkipNow() t.SkipNow()

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"github.com/restic/restic/backend" "github.com/restic/restic/backend"
"github.com/restic/restic/debug"
) )
type Server struct { type Server struct {
@ -143,8 +144,20 @@ func (s Server) Save(t backend.Type, data []byte, id backend.ID) (Blob, error) {
Size: uint64(len(data)), Size: uint64(len(data)),
} }
ciphertext := GetChunkBuf("ch.Save()") var ciphertext []byte
defer FreeChunkBuf("ch.Save()", ciphertext)
// if the data is small enough, use a slice from the pool
if len(data) <= maxCiphertextSize-ivSize-hmacSize {
ciphertext = GetChunkBuf("ch.Save()")
defer FreeChunkBuf("ch.Save()", ciphertext)
} else {
l := len(data) + ivSize + hmacSize
debug.Log("Server.Save", "create large slice of %d bytes for ciphertext", l)
// use a new slice
ciphertext = make([]byte, l)
}
// encrypt blob // encrypt blob
n, err := s.Encrypt(ciphertext, data) n, err := s.Encrypt(ciphertext, data)