Skip to content

Three hand-written copies of the same clipping contract#741

Merged
xroche merged 3 commits into
masterfrom
fix/clipbuff
Jul 26, 2026
Merged

Three hand-written copies of the same clipping contract#741
xroche merged 3 commits into
masterfrom
fix/clipbuff

Conversation

@xroche

@xroche xroche commented Jul 26, 2026

Copy link
Copy Markdown
Owner

Three places spelled out the same clip-don't-abort copy by hand: the engine's cache reader in htscache.c, and both the ZIP and ARC readers in proxy/store.c. They live in binaries that share no code, so the duplication could not be caught by the linker, only by noticing it.

strclipbuff() in htssafe.h states the contract once. It also evaluates its arguments once, which the macro form did not: refvalue and refvalue_size were each expanded twice, and (refvalue_size) - 1 would have wrapped to SIZE_MAX if any call site had ever passed zero. No call site does, so that part is hardening rather than a fix.

Covered by -#test=strsafe, mutation-tested against six candidate bugs: an off-by-one clip, a missing terminator, writing nothing when truncating, always reporting a fit, a sign-extended 0xff ending the copy early, and dropping the last byte. All six fail the test; the restored build passes.

htscache.c and the two readers in proxy/store.c each spelled out
clear-then-strlncatbuff, in binaries that share no code. A helper in
htssafe.h states the contract once, and evaluates its arguments once: the
macro form expanded refvalue and refvalue_size twice, and (refvalue_size)
- 1 would have wrapped to SIZE_MAX had any call site ever passed 0.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
xroche and others added 2 commits July 26, 2026 22:28
The cases jumped from the degenerate capacity 1 straight to 8, so a
defect confined to small-but-not-degenerate sizes passed: clipping a
two-byte destination to the empty string instead of one character.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
@xroche
xroche merged commit aa11319 into master Jul 26, 2026
22 checks passed
@xroche
xroche deleted the fix/clipbuff branch July 26, 2026 21:21
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.

1 participant