Skip to content

lib/suitar: fix Writer block-boundary handling and directory headers - #179

Merged
nigeltao merged 3 commits into
google:mainfrom
rootkiller6788:fix-suitar-writer
Sep 15, 2026
Merged

nigeltao merged 3 commits into
google:mainfrom
rootkiller6788:fix-suitar-writer

Conversation

@rootkiller6788

Copy link
Copy Markdown

Found two bugs in the new lib/suitar Writer while poking at block boundaries.

The first one: Write didn't reset w.bIndex after writing out a full 512-byte block. The next loop iteration then took the "partial block" path, copied nothing and went round forever writing the same block. Calling Write with 300 bytes and then 700 bytes is enough to hit it, the second call never came back. It also duplicated the last data block when a Write happened to end exactly on a block boundary, which the reader then rejects as a bad header. One line fixes it.

The second one: WriteHeader set w.header = *h before calling w.Write to emit the name block (suitar writes each name as a GNUTYPE_LONGNAME entry first). Write refuses anything that isn't TypeReg or TypeGNUSparse, so a TypeDir header blew up there and poisoned the Writer for every later entry too. Moved the assignment to after the name block. Header.Valid(), the exported TypeDir constant and the spec all treat directories as normal, so I think it was just an oversight.

I added TestWriterChunked (writes the same file with different chunk sizes and checks the bytes come out the same) and TestWriterDir (a directory plus a file inside it). Both fail on the old code. The chunked one fails in 0.00s rather than hanging, I put a capped writer in the test for that.

One thing I noticed and left alone: Reader.Next has two return Header{}, err that don't latch r.err, unlike the error handling everywhere else in the file. Doesn't seem to cause a problem right now, just inconsistent.

go test ./lib/suitar/ passes.

rootkiller6788 added 3 commits September 12, 2026 23:48
Write left w.bIndex at 512 once a full block had been written out, so the
next loop iteration took the "partial block" branch, copied zero bytes and
went round again forever, writing the same 512 bytes over and over. Calling
Write with sizes that aren't multiples of 512 is enough to hit this.

It also duplicated the last data block when a Write call happened to end
exactly on a block boundary, because flush then saw a non-zero bIndex and
wrote the block out a second time.
WriteHeader copied h into w.header before calling w.Write to emit the
GNUTYPE_LONGNAME block holding the name. Write only accepts TypeReg and
TypeGNUSparse, so a TypeDir header failed that call and left the Writer
permanently in an error state - every later entry on the same Writer
failed too.

Set w.header after the name block has gone out instead.
TestWriterChunked writes one file several times, splitting the contents
over Write calls differently each time, and checks the archive bytes come
out identical. The capped writer makes a runaway Write fail the test
instead of hanging it.

TestWriterDir writes a directory and then a file inside it, and reads
both back.
@google-cla

google-cla Bot commented Sep 14, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@nigeltao
nigeltao merged commit 5049107 into google:main Sep 15, 2026
5 of 6 checks passed
@nigeltao

Copy link
Copy Markdown
Collaborator

Thanks! I have a few style nits, but I can fix that myself, after I rebase.

Regarding the google-cla, I have confirmed (using Google's internal tools) that GitHub username rootkiller6788 has passed the CLA check.

nigeltao added a commit that referenced this pull request Sep 16, 2026
nigeltao added a commit that referenced this pull request Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants