Skip to content

Commit 9f4b0d1

Browse files
committed
dont let the dictionary be larger than the unpacked file size
1 parent 153fdf5 commit 9f4b0d1

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type fileBlockHeader struct {
8989
packedOff int64 // offset to data in packed file
9090
blocknum int // number for current block in file
9191
volnum int // archive volume number
92-
winSize int // decode window size
92+
winSize int64 // decode window size
9393
hash func() hash.Hash // hash used for file checksum
9494
hashKey []byte // optional hmac key to be used calculate file checksum
9595
sum []byte // expected checksum for file contents

archive50.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"hash"
99
"hash/crc32"
1010
"io"
11-
"math"
1211
"math/bits"
1312
"slices"
1413
"time"
@@ -374,28 +373,21 @@ func (a *archive50) parseFileHeader(h *blockHeader50) (*fileBlockHeader, error)
374373
method := (flags >> 7) & 7 // compression method (0 == none)
375374
if f.first && method != 0 {
376375
unpackver := flags & file5CompAlgorithm
377-
var winSize int64
378-
if unpackver == 0 {
376+
switch unpackver {
377+
case 0:
379378
f.decVer = decode50Ver
380-
winSize = 0x20000 << ((flags >> 10) & 0x0F)
381-
} else if unpackver == 1 {
379+
f.winSize = 0x20000 << ((flags >> 10) & 0x0F)
380+
case 1:
382381
if flags&file5CompV5Compat > 0 {
383382
f.decVer = decode50Ver
384383
} else {
385384
f.decVer = decode70Ver
386385
}
387-
winSize = 0x20000 << ((flags >> 10) & 0x1F)
388-
winSize += winSize / 32 * int64((flags>>15)&0x1F)
389-
if winSize > maxDictSize {
390-
return nil, ErrDictionaryTooLarge
391-
}
392-
} else {
386+
f.winSize = 0x20000 << ((flags >> 10) & 0x1F)
387+
f.winSize += f.winSize / 32 * int64((flags>>15)&0x1F)
388+
default:
393389
return nil, ErrUnknownDecoder
394390
}
395-
if winSize > math.MaxInt {
396-
return nil, ErrPlatformIntSize
397-
}
398-
f.winSize = int(winSize)
399391
}
400392
switch h.data.uvarint() {
401393
case 0:

reader.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"hash"
99
"io"
1010
"io/fs"
11+
"math"
1112
"sync"
1213
"time"
1314
)
@@ -331,7 +332,17 @@ func (pr *packedFileReader) newArchiveFileFrom(r archiveFile, blocks *fileBlockL
331332
if pr.dr == nil {
332333
pr.dr = new(decodeReader)
333334
}
334-
err := pr.dr.init(r, h.decVer, h.winSize, !h.Solid, h.arcSolid, h.UnPackedSize)
335+
// doesn't make sense for the dictionary to be larger than the file
336+
if !h.UnKnownSize && h.winSize > h.UnPackedSize {
337+
h.winSize = h.UnPackedSize
338+
}
339+
if h.winSize > maxDictSize {
340+
return nil, ErrDictionaryTooLarge
341+
}
342+
if h.winSize > math.MaxInt {
343+
return nil, ErrPlatformIntSize
344+
}
345+
err := pr.dr.init(r, h.decVer, int(h.winSize), !h.Solid, h.arcSolid, h.UnPackedSize)
335346
if err != nil {
336347
return nil, err
337348
}

0 commit comments

Comments
 (0)