Skip to content

Commit

Permalink
fix: encoder WriteSeeker now relative to current offset (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
muktihari authored Sep 1, 2024
1 parent 7bdf0df commit 2cbe865
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,18 @@ func (e *Encoder) updateFileHeader(header *proto.FileHeader) (err error) {

switch w := e.w.(type) {
case io.WriteSeeker:
// Relative to EOF, ensure that we only write in our own data.
_, err = w.Seek(-e.n+e.lastFileHeaderPos, io.SeekEnd)
// Ensure that we only write in our own data.
size := e.n - e.lastFileHeaderPos
_, err = w.Seek(-size, io.SeekCurrent)
if err != nil {
return err
}
_, err = w.Write(b)
var n int
n, err = w.Write(b)
if err != nil {
return err
}
_, err = w.Seek(0, io.SeekEnd)
_, err = w.Seek(size-int64(n), io.SeekCurrent)
return err
case io.WriterAt:
// This might write at the wrong offset if the writer was not
Expand Down
2 changes: 1 addition & 1 deletion encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (f *writeSeekerStub) Seek(off int64, whence int) (int64, error) {
switch whence {
case io.SeekCurrent:
l := int64(len(f.buf))
l2 := l + off
l2 := f.off + off
if l2 < 0 {
return 0, os.ErrInvalid
}
Expand Down

0 comments on commit 2cbe865

Please sign in to comment.