Skip to content

Commit aeaa125

Browse files
committed
plumbing: format/objfile, require Header before Read
`Reader` defines a two-phase contract: callers must invoke `Header` to consume the loose-object type and size fields, after which the internal `multi` reader and `hasher` are initialised. Calling `Read` or `Hash` before a successful `Header` accessed uninitialised fields and would panic on `r.multi.Read` / `r.hasher.Sum`. Make the contract explicit: `Read` returns the new `ErrHeaderNotRead` sentinel when `Header` has not been called successfully. `Hash` returns `plumbing.ZeroHash` in the same situation. Both methods guard on `r.multi == nil`, which `prepareForRead` sets on the first successful `Header` call. Two unit tests pin the behaviour: `TestReaderReadBeforeHeader` asserts that `Read` returns `ErrHeaderNotRead` and `Hash` returns `ZeroHash`; `TestReaderReadAfterHeaderError` calls `Read` after `Header` has returned an error. Backport of #2119. Assisted-by: Claude Opus 4.7 Signed-off-by: Hidde Beydals <[email protected]>
1 parent 1f38e17 commit aeaa125

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎plumbing/format/objfile/reader.go‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111
)
1212

1313
var (
14-
ErrClosed = errors.New("objfile: already closed")
15-
ErrHeader = errors.New("objfile: invalid header")
16-
ErrNegativeSize = errors.New("objfile: negative object size")
14+
ErrClosed = errors.New("objfile: already closed")
15+
ErrHeader = errors.New("objfile: invalid header")
16+
ErrHeaderNotRead = errors.New("objfile: Header must be called before Read")
17+
ErrNegativeSize = errors.New("objfile: negative object size")
1718
)
1819

1920
// Reader reads and decodes compressed objfile data from a provided io.Reader.
@@ -100,12 +101,23 @@ func (r *Reader) prepareForRead(t plumbing.ObjectType, size int64) {
100101
//
101102
// If Read encounters the end of the data stream it will return err == io.EOF,
102103
// either in the current call if n > 0 or in a subsequent call.
104+
//
105+
// Read returns ErrHeaderNotRead if Header has not been called successfully.
103106
func (r *Reader) Read(p []byte) (n int, err error) {
107+
if r.multi == nil {
108+
return 0, ErrHeaderNotRead
109+
}
104110
return r.multi.Read(p)
105111
}
106112

107113
// Hash returns the hash of the object data stream that has been read so far.
114+
// It returns the zero plumbing.Hash if Header has not been called
115+
// successfully — guarding against the nil hasher that prepareForRead has
116+
// not yet allocated.
108117
func (r *Reader) Hash() plumbing.Hash {
118+
if r.multi == nil {
119+
return plumbing.ZeroHash
120+
}
109121
return r.hasher.Sum()
110122
}
111123

‎plumbing/format/objfile/reader_test.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,39 @@ func (s *SuiteReader) TestReadCorruptZLib(c *C) {
6565
_, _, err = r.Header()
6666
c.Assert(err, NotNil)
6767
}
68+
69+
func (s *SuiteReader) TestReaderReadBeforeHeader(c *C) {
70+
data, _ := base64.StdEncoding.DecodeString(objfileFixtures[0].data)
71+
source := bytes.NewReader(data)
72+
73+
r, err := NewReader(source)
74+
c.Assert(err, IsNil)
75+
defer r.Close()
76+
77+
var buf [16]byte
78+
n, err := r.Read(buf[:])
79+
c.Assert(err, Equals, ErrHeaderNotRead)
80+
c.Assert(n, Equals, 0)
81+
82+
c.Assert(r.Hash(), Equals, plumbing.ZeroHash)
83+
}
84+
85+
func (s *SuiteReader) TestReaderReadAfterHeaderError(c *C) {
86+
// This zlib stream decompresses to bytes that do not form a valid
87+
// loose-object header, so Header() returns an error.
88+
data, _ := base64.StdEncoding.DecodeString("eAFLysaalPUjBgAAAJsAHw")
89+
source := bytes.NewReader(data)
90+
91+
r, err := NewReader(source)
92+
c.Assert(err, IsNil)
93+
defer r.Close()
94+
95+
_, _, err = r.Header()
96+
c.Assert(err, NotNil)
97+
98+
// Read must return an error rather than accessing uninitialised state.
99+
var buf [16]byte
100+
n, readErr := r.Read(buf[:])
101+
c.Assert(readErr, NotNil)
102+
c.Assert(n, Equals, 0)
103+
}

0 commit comments

Comments
 (0)