Skip to content

Commit 5f94f68

Browse files
author
Ibrahim Jarif
authored
Limit manifest's change set size (#1119)
This PR limits the amount of memory we allocated for reading the manifest changes set's size. When a manifest file is corrupted, in the worst case we might end up allocating more than 4GB. This PR ensures we don't over-allocate the byte slice. Fixes #490
1 parent ffb3450 commit 5f94f68

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

manifest.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,11 @@ func ReplayManifestFile(fp *os.File) (Manifest, int64, error) {
353353
version, magicVersion)
354354
}
355355

356+
stat, err := fp.Stat()
357+
if err != nil {
358+
return Manifest{}, 0, err
359+
}
360+
356361
build := createManifest()
357362
var offset int64
358363
for {
@@ -366,6 +371,12 @@ func ReplayManifestFile(fp *os.File) (Manifest, int64, error) {
366371
return Manifest{}, 0, err
367372
}
368373
length := y.BytesToU32(lenCrcBuf[0:4])
374+
// Sanity check to ensure we don't over-allocate memory.
375+
if length > uint32(stat.Size()) {
376+
return Manifest{}, 0, errors.Errorf(
377+
"Buffer length: %d greater than file size: %d. Manifest file might be corrupted",
378+
length, stat.Size())
379+
}
369380
var buf = make([]byte, length)
370381
if _, err := io.ReadFull(&r, buf); err != nil {
371382
if err == io.EOF || err == io.ErrUnexpectedEOF {

0 commit comments

Comments
 (0)