77 "errors"
88 "fmt"
99 "io"
10+ "io/fs"
1011
1112 "github.com/go-git/go-git/v5/plumbing/hash"
1213 "github.com/go-git/go-git/v5/utils/binary"
@@ -25,35 +26,88 @@ const (
2526 objectIDLength = hash .Size
2627)
2728
29+ // Byte sizes of the idx v2 layout elements, used by the size formula
30+ // in [validateIdxV2Size]. See [gitformat-pack] for the canonical
31+ // layout.
32+ //
33+ // [gitformat-pack]: https://git-scm.com/docs/gitformat-pack
34+ const (
35+ headerLen = 8 // magic + version
36+ fanoutLen = fanout * 4 // uint32 per bucket
37+ crc32Len = 4 // CRC32 per object
38+ offset32Len = 4 // 32-bit offset per object
39+ offset64Len = 8 // 64-bit overflow offset
40+ trailerHashes = 2 // pack checksum + idx checksum, each hashsz
41+ )
42+
43+ // statInput is the optional shape the [Decoder] probes for at the
44+ // start of [Decoder.Decode] to learn the on-disk length of the idx
45+ // blob, which it uses to validate the canonical-Git size formula
46+ // before any allocations driven by the fanout table. Callers that
47+ // pass an [*os.File] or a `billy.File` backed by an `*os.File`
48+ // (the production call sites in `storage/filesystem`) satisfy it
49+ // directly; arbitrary [io.Reader]s do not, and decode for them
50+ // retains the pre-existing behaviour of erroring out at the
51+ // truncated-payload boundary instead.
52+ //
53+ // The interface is intentionally unexported so the public
54+ // [NewDecoder] signature stays compatible with v5.
55+ type statInput interface {
56+ Stat () (fs.FileInfo , error )
57+ }
58+
2859// Decoder reads and decodes idx files from an input stream.
2960type Decoder struct {
3061 io.Reader
31- h hash.Hash
62+ src io.Reader
63+ h hash.Hash
3264}
3365
3466// NewDecoder builds a new idx stream decoder, that reads from r.
3567func NewDecoder (r io.Reader ) * Decoder {
3668 h := hash .New (crypto .SHA1 )
3769 tr := io .TeeReader (r , h )
38- return & Decoder {tr , h }
70+ return & Decoder {tr , r , h }
3971}
4072
4173// Decode reads from the stream and decode the content into the MemoryIndex struct.
4274func (d * Decoder ) Decode (idx * MemoryIndex ) error {
75+ idxSize := int64 (- 1 )
76+ if in , ok := d .src .(statInput ); ok {
77+ fi , err := in .Stat ()
78+ if err != nil {
79+ return fmt .Errorf ("%w: stat input: %w" , ErrMalformedIdxFile , err )
80+ }
81+ idxSize = fi .Size ()
82+ }
83+
4384 if err := validateHeader (d ); err != nil {
4485 return err
4586 }
4687
47- flow := []func (* MemoryIndex , io.Reader ) error {
88+ headerFlow := []func (* MemoryIndex , io.Reader ) error {
4889 readVersion ,
4990 readFanout ,
91+ }
92+ for _ , f := range headerFlow {
93+ if err := f (idx , d ); err != nil {
94+ return err
95+ }
96+ }
97+
98+ if idxSize >= 0 {
99+ if err := validateIdxV2Size (idx , idxSize ); err != nil {
100+ return err
101+ }
102+ }
103+
104+ bodyFlow := []func (* MemoryIndex , io.Reader ) error {
50105 readObjectNames ,
51106 readCRC32 ,
52107 readOffsets ,
53108 readPackChecksum ,
54109 }
55-
56- for _ , f := range flow {
110+ for _ , f := range bodyFlow {
57111 if err := f (idx , d ); err != nil {
58112 return err
59113 }
@@ -199,3 +253,103 @@ func readIdxChecksum(idx *MemoryIndex, r io.Reader) error {
199253
200254 return nil
201255}
256+
257+ // validateIdxV2Size enforces the size formula used by canonical Git
258+ // load_idx for idx v2 files: the on-disk length must lie within
259+ // [minSize, maxSize] where
260+ //
261+ // perObject = hashsz + crc32Len + offset32Len
262+ // minSize = headerLen + fanoutLen + trailerHashes*hashsz + nr*perObject
263+ // maxSize = minSize + (nr-1)*offset64Len when nr > 0
264+ //
265+ // with nr taken from the last fanout entry and hashsz fixed at
266+ // [objectIDLength] (SHA-1 in v5). Multiplications use a self-checking
267+ // overflow guard so inputs whose claimed object count overflows the
268+ // formula are rejected rather than wrapping into a smaller value.
269+ func validateIdxV2Size (idx * MemoryIndex , idxSize int64 ) error {
270+ nr := int64 (idx .Fanout [fanout - 1 ])
271+ hashsz := int64 (objectIDLength )
272+
273+ minSize := minIdxV2Size (nr , hashsz )
274+ maxSize := maxIdxV2Size (nr , hashsz )
275+ if minSize < 0 || maxSize < 0 {
276+ return fmt .Errorf ("%w: object count %d is inconsistent with file size" , ErrMalformedIdxFile , nr )
277+ }
278+
279+ if idxSize < minSize || idxSize > maxSize {
280+ return fmt .Errorf ("%w: file size %d is inconsistent with object count %d" , ErrMalformedIdxFile , idxSize , nr )
281+ }
282+ return nil
283+ }
284+
285+ // minIdxV2Size returns the minimum on-disk size of an idx v2 file
286+ // holding nr objects with the given hash size, mirroring the
287+ // computation in canonical Git load_idx. Returns -1 when any
288+ // intermediate multiplication or addition would overflow int64.
289+ func minIdxV2Size (nr , hashsz int64 ) int64 {
290+ perObject := hashsz + crc32Len + offset32Len
291+ fixed := int64 (headerLen + fanoutLen ) + trailerHashes * hashsz
292+
293+ objects , ok := mulInt64 (nr , perObject )
294+ if ! ok {
295+ return - 1
296+ }
297+ sum , ok := addInt64 (fixed , objects )
298+ if ! ok {
299+ return - 1
300+ }
301+ return sum
302+ }
303+
304+ // maxIdxV2Size returns the maximum on-disk size of an idx v2 file
305+ // holding nr objects with the given hash size, mirroring the
306+ // computation in canonical Git load_idx. Returns -1 on overflow.
307+ func maxIdxV2Size (nr , hashsz int64 ) int64 {
308+ minSize := minIdxV2Size (nr , hashsz )
309+ if minSize < 0 {
310+ return - 1
311+ }
312+ if nr == 0 {
313+ return minSize
314+ }
315+ overflow , ok := mulInt64 (nr - 1 , offset64Len )
316+ if ! ok {
317+ return - 1
318+ }
319+ sum , ok := addInt64 (minSize , overflow )
320+ if ! ok {
321+ return - 1
322+ }
323+ return sum
324+ }
325+
326+ // mulInt64 returns a*b and whether the result fits in an int64 without
327+ // overflow. Negative operands or overflow yield ok=false. The overflow
328+ // check uses the standard self-inverse identity: a*b/b == a only when
329+ // the multiplication did not wrap.
330+ func mulInt64 (a , b int64 ) (int64 , bool ) {
331+ if a < 0 || b < 0 {
332+ return 0 , false
333+ }
334+ if a == 0 || b == 0 {
335+ return 0 , true
336+ }
337+ c := a * b
338+ if c / b != a {
339+ return 0 , false
340+ }
341+ return c , true
342+ }
343+
344+ // addInt64 returns a+b and whether the result fits in an int64 without
345+ // overflow. Negative operands or overflow yield ok=false.
346+ func addInt64 (a , b int64 ) (int64 , bool ) {
347+ if a < 0 || b < 0 {
348+ return 0 , false
349+ }
350+ c := a + b
351+ if c < a {
352+ return 0 , false
353+ }
354+ return c , true
355+ }
0 commit comments