Exactness gates: keeping an archive reader honest

A decoder that works on your fixtures is not the same as a decoder that's right, and the gap is where data gets corrupted quietly.

By · · Part of Files, Unwrapped: building a Mac archive app

There's a kind of bug I'm more afraid of than a crash. A crash announces itself. The one I lose sleep over is a reader that extracts a file, reports success, and hands you bytes that are almost the file you stored. A photo with one wrong block. A spreadsheet with a transposed row. Nobody notices until the day it matters, and by then the original archive may be gone.

Peel exists to show you what is really inside an archive. If what it shows is subtly wrong, that purpose is defeated in the worst possible way: politely.

How a reader "works" and is still wrong

Here's the pattern. I write a decoder from a spec. I build a few test archives. I extract them, the output looks right, the tests pass, and I feel good. But the archives were produced by my own understanding of the format, and the decoder was written from the same understanding. If I misread one edge of the spec, the fixture and the decoder share the misreading, and they agree with each other perfectly.

A test that only compares my code against my expectations is a mirror. It can tell me nothing I didn't already believe.

Real files are where beliefs go to die. They come from old tools with quirks, from encoders that took liberties, from the one version of a program that did something nobody documented. So the rule I try to hold myself to is that a reader isn't done when it passes my tests. It's done when its output has been checked against something I didn't write.

The oracle

I call that something an oracle, and the repository has a written strategy for choosing one. The order of preference is roughly this:

  1. The format's own integrity data, actually enforced. If an archive stores a checksum for each file, a mismatch is an error, never a quietly wrong file. This is the cheapest oracle and it comes free.
  2. An independent extractor. Run a different tool, one that shares no code with mine, over the same files and require agreement on listing and content, byte for byte. For many older formats that tool is The Unarchiver's unar and lsar, and where the format has a canonical writer I build or install that too.
  3. A known-good corpus. Real test archives published by a project with a compatible licence, committed with their source noted.
  4. Layered validation. For a container that wraps another format, the inner format is the check. If a disc image wraps a filesystem, and the filesystem parses and its invariants hold, that shows my address mapping for the sectors I read. The docs are careful to say this validates the mapping, not every byte.
  5. Generated fixtures from an independent writer. Useful, but explicitly only supporting evidence, because they test my reading against my reading of a layout.

What's not acceptable on its own is just as important: round-tripping through my own writer, editing a fixture until it matches what the decoder emits, or the phrase "it extracted without error." That last one is the most tempting and the least informative.

The oracle changes with the format. Compressed streams can be checked by round-tripping through zlib and requiring the same bytes back. An installer format can be checked against the tool that builds installers: I wrote and tested my NSIS reader against output from makensis, the real compiler, rather than only archives I'd made myself. Amiga disk images and DMS files can be compared to what xdms produces. Where a corpus exists, like public collections of real-world files, I run against that and not only against tiny hand-made samples.

One episode taught me why the independence matters. While working on the NSIS reader I wanted to handle a particular filter, but the version of makensis I was testing against never actually emits one. So there was no real stream to validate against, and the right answer was not to write the decoder from the spec and hope. A decoder with no oracle is a guess wearing a uniform.

When there is no oracle

This is the part I'm proudest of, because it's a rule about saying no. If I can't find anything independent to check a format against, I don't decode it. Peel recognises the file by signature, says what it is, and says plainly why it won't read it. One legacy format is in that position today: the only decoder is closed and restricted, and the independent tools don't read it either, so Peel declines.

The same idea applies per layout, not just per format. A reader can support the variants it can validate and refuse the rest explicitly: audio tracks on a disc image, a particular sector mode, scrambled images, unknown codecs, encrypted entries, multi-volume sets. Each refusal has a specific message. A file that fails a check is an error for that entry, and nothing gets written under the real name. Partial output is never presented as complete.

I've come to think this is most of what trustworthy software looks like: a clear, honest edge where it stops, instead of a confident guess past it.

Archives are hostile input

A reader can be exactly right on every honest file and still be dangerous, because archives are untrusted. Anyone can hand you one, and the lengths, offsets and counts inside are whatever the author wants them to be. So correctness has a second half: the reader has to survive files that were built to hurt it.

That's what fuzzing is for. The repository has a fuzz crate whose targets feed arbitrary bytes to the listing, inspection, assessment and extraction paths, and a few of the more specialised analysers directly. The extraction target runs under a tight resource budget, so a decompression bomb fails as an error instead of filling the disk or eating memory.

Fuzzing alone is slow if it starts from nothing, because a mutator won't stumble into a valid header on its own. So each archive target is seeded: the test fixtures, a few small hand-made archives, and every previous crash, minimised and kept as a regression with a test that reads it. When I add a format, I add seeds for it, so the fuzzer starts inside the format and not outside the door.

It earns its keep. The fuzzer has found real bugs for me: a sector-shift overflow in the Compound File reader and an out-of-bounds read on a short LZH header. Both were the sort of thing no hand-written test of mine would have thought to try, because I wasn't thinking like someone trying to break the parser. The fuzzer isn't polite enough to share my assumptions.

Reading the true exit code

The last gate is the dullest and the one I've failed most often: making sure my own checks actually report what happened.

Two habits. First, run the full engine suite for any engine change, not a filtered subset. A filtered run once let a regression through because the test that would have caught it wasn't in the filter. Second, capture cargo's real exit code. If you pipe test output into tail or grep, the exit status you see belongs to the last command in the pipe, not to cargo. A failing suite can look like success. So the pattern is to redirect to a file, capture the code on the very next line, then read the file:

cargo test -p peel-core > /tmp/test.log 2>&1; code=$?
tail -n 20 /tmp/test.log
echo "cargo exit: $code"

It's a small thing, but a verification step that can lie to you is worse than none, because you'll believe it.

What I take from it

None of this is clever. It's a posture: assume my own output is the least trustworthy evidence in the room, and go find evidence that doesn't come from me. Check against a tool that shares no code. Check against real files, not just tidy ones. Hand the parser to something that wants to break it. Refuse to guess when there's nothing to check against.

I don't think it makes Peel bug-free, and I'd distrust anyone who claimed that of an archive reader. What it does is move my mistakes from "silent and plausible" toward "loud and early," which is the only direction a tool like this can afford. The gates cost time, and some formats take longer to ship because I wouldn't ship them blind. I'd make the same trade again. For an app whose reason to exist is showing you what's really inside, being right is the product, and optimism isn't a test.

Peel is free for macOS 13 or later. Get Peel, or install it with brew install --cask snackstudio/tap/peel.

Next in the series: The long tail of 'opens most archives'