aboutsummaryrefslogtreecommitdiff
path: root/internal/format
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-06-11 13:23:23 +0000
committerGravatar Runxi Yu2026-06-11 13:23:23 +0000
commit3c4f205a54dc3d84fe5716e9f759122f0891a599 (patch)
tree3faa2675475cbe43d48f695927c24055c2c18bdc /internal/format
parentinternal/format/packfile/delta: Apply tests (diff)
internal/format/packfile: Panic on implausible hash sizes
Diffstat (limited to 'internal/format')
-rw-r--r--internal/format/packfile/entry_header.go11
-rw-r--r--internal/format/packfile/entry_header_test.go13
2 files changed, 12 insertions, 12 deletions
diff --git a/internal/format/packfile/entry_header.go b/internal/format/packfile/entry_header.go
index 1428600b..e5f2d62d 100644
--- a/internal/format/packfile/entry_header.go
+++ b/internal/format/packfile/entry_header.go
@@ -13,12 +13,6 @@ import (
// or declares a size that overflows uint64.
var ErrMalformedEntryHeader = errors.New("internal/format/packfile: malformed entry header")
-// ErrInvalidHashSize reports that
-// a supplied hash size is not a plausible object ID size.
-// This indicates a caller bug,
-// not malformed pack data.
-var ErrInvalidHashSize = errors.New("internal/format/packfile: invalid hash size")
-
// MaxTypeSizeLen is the maximum encoded length
// of the type/size prefix of an entry header.
// Every uint64 size is encodable within this bound,
@@ -73,7 +67,8 @@ type EntryHeader struct {
// from the beginning of data.
//
// hashSize must be the object ID size
-// of the pack's object format.
+// of the pack's object format;
+// ParseEntryHeader panics on implausible hash sizes.
//
// data need not contain the whole entry;
// [MaxEntryHeaderLen] bytes always suffice.
@@ -83,7 +78,7 @@ func ParseEntryHeader(data []byte, hashSize int) (EntryHeader, error) {
var zero EntryHeader
if hashSize <= 0 || hashSize > id.MaxObjectIDSize {
- return zero, fmt.Errorf("%w: %d", ErrInvalidHashSize, hashSize)
+ panic("internal/format/packfile: invalid hash size")
}
if len(data) == 0 {
diff --git a/internal/format/packfile/entry_header_test.go b/internal/format/packfile/entry_header_test.go
index 807c544e..79dc2740 100644
--- a/internal/format/packfile/entry_header_test.go
+++ b/internal/format/packfile/entry_header_test.go
@@ -130,10 +130,15 @@ func TestParseEntryHeaderBadHashSize(t *testing.T) {
t.Parallel()
for _, hashSize := range []int{-1, 0, id.MaxObjectIDSize + 1} {
- _, err := packfile.ParseEntryHeader([]byte{0x95, 0x05}, hashSize)
- if err == nil {
- t.Fatalf("ParseEntryHeader hash size %d: expected error", hashSize)
- }
+ func() {
+ defer func() {
+ if recover() == nil {
+ t.Fatalf("ParseEntryHeader hash size %d: expected panic", hashSize)
+ }
+ }()
+
+ _, _ = packfile.ParseEntryHeader([]byte{0x95, 0x05}, hashSize)
+ }()
}
}