aboutsummaryrefslogtreecommitdiff
path: root/pack_idx.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2025-11-17 00:00:00 +0000
committerGravatar Runxi Yu2025-11-17 00:00:00 +0000
commit1dcb92427c23d0a8b23c0154b892243c749afa5a (patch)
tree0683fa19e05d2ea0ceac324dafdcd49eb6cdb2ec /pack_idx.go
parentAdd internal todo (diff)
signature
Compute checksum when reading packfiles
Diffstat (limited to 'pack_idx.go')
-rw-r--r--pack_idx.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/pack_idx.go b/pack_idx.go
index 03397286..3cb89701 100644
--- a/pack_idx.go
+++ b/pack_idx.go
@@ -196,6 +196,23 @@ func (pi *packIndex) parse(buf []byte) error {
pi.numObjects = nobj
pi.names = buf[namesStart:namesEnd]
pi.crcs = buf[crcStart:crcEnd]
+
+ if len(buf) < 2*pi.repo.hashSize {
+ return ErrInvalidObject
+ }
+ idxChecksumStart := len(buf) - pi.repo.hashSize
+ idxChecksumInFile := buf[idxChecksumStart:]
+
+ hashFn, ok := hashFuncs[pi.repo.hashSize]
+ if !ok {
+ return fmt.Errorf("furgit: unsupported hash size %d", pi.repo.hashSize)
+ }
+
+ computedHash := hashFn(buf[:idxChecksumStart])
+ if !bytes.Equal(computedHash.data[:pi.repo.hashSize], idxChecksumInFile) {
+ return fmt.Errorf("furgit: index checksum mismatch in %s", pi.idxRel)
+ }
+
return nil
}