Skip to content

Commit

Permalink
Merge pull request #46 from creachadair/checksum
Browse files Browse the repository at this point in the history
Do not permit index entries to be flushed twice.
  • Loading branch information
jfontan authored Jun 13, 2019
2 parents 11db885 + 1a203a8 commit 9ee6c8c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
11 changes: 3 additions & 8 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,23 @@ func (w *writer) Write(b []byte) (int, error) {
func (w *writer) Flush() error {
if w.closed {
return ErrClosedWriter
}

if w.current == nil {
} else if w.current == nil {
return ErrMissingHeader
}

w.current.Size = w.position - w.current.Start
w.current.CRC32 = w.w.Checksum()
w.current = nil
w.w.Reset()

return nil
}

func (w *writer) flushIfPending() error {
if w.closed {
return ErrClosedWriter
}

if w.current == nil {
} else if w.current == nil {
return nil
}

return w.Flush()
}

Expand Down
17 changes: 17 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ func (s *WriterSuite) TestFlushOnClose(c *C) {
c.Assert(err, Equals, ErrClosedWriter)
}

func (s *WriterSuite) TestDoubleFlushRegression(c *C) {
buf := new(bytes.Buffer)
w := NewWriter(buf)
const data = "foo"
c.Assert(w.WriteHeader(&Header{
Name: "test.txt",
Mode: 0640,
}), IsNil)
_, err := w.Write([]byte(data))
c.Assert(err, IsNil)
c.Assert(w.Flush(), IsNil)
want := *w.(*writer).index[0]
c.Assert(w.Close(), IsNil)
got := *w.(*writer).index[0]
c.Assert(got, Equals, want)
}

func (s *WriterSuite) TestWriterReaderIdempotent(c *C) {
buf := new(bytes.Buffer)
w := NewWriter(buf)
Expand Down

0 comments on commit 9ee6c8c

Please sign in to comment.