Skip to content

Windows builds alias snprintf to _snprintf, which leaves a truncated string unterminated - #1752

Merged
xroche merged 2 commits into
masterfrom
fix-win-snprintf-terminator
Sep 22, 2026
Merged

xroche merged 2 commits into
masterfrom
fix-win-snprintf-terminator

Conversation

@xroche

@xroche xroche commented Sep 22, 2026

Copy link
Copy Markdown
Owner

A user reported WinHTTrack 3.50-2 aborting with size < sizeof_source at htsparse.c:4976. That line copies back[b].r.msg, and the abort says those 80 bytes held no terminating NUL.

htslib.h aliased snprintf to _snprintf under _WIN32. Microsoft documents _snprintf as writing no terminator when it truncates, while the UCRT snprintf the alias hid is C99-conformant. So the engine's 89 raw snprintf calls left their destination unterminated on Windows and nowhere else. Dropping the alias is the fix.

The six failure messages that land in r.msg now go through htsblk_failf(), which clips. Three carry text the crawl does not control, and do overflow 80 bytes: a link's port, a server's Content-Encoding, and the resolver's message.

Test 491 fails on any macro renaming a C99 printf call, and on the same alias handed to the compiler as a define. A Linux build sees neither.

Closes #1753

xroche and others added 2 commits September 22, 2026 20:16
htslib.h defined snprintf as _snprintf under _WIN32. Microsoft documents
_snprintf as writing no terminating NUL when it truncates, so every raw
snprintf in the engine produced an unterminated string on Windows, and
only there. A user hit it through htsblk.msg[80]: the crawler aborted in
htsparse.c reading a failure message that had filled the field.

Drop the alias, route the writers of r.msg through htsblk_failf(), and
add a test that fails on any macro renaming a C99 printf call, since a
Linux build cannot see one.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
Review findings on this branch. Test 490 collided with the number PR
#1743 claims, so the file moves to 491. Its scan reported a clean tree
whatever the file list held, so it now floors the count. It also missed
the alias handed to the compiler rather than written in a header, which
is how a Visual Studio build would carry one, so it reads the project
files too.

The no-argument message in htsback.c goes back to snprintf: its text is
65 bytes, so it cannot fill the field and the conversion bought nothing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
@xroche xroche changed the title Windows builds alias snprintf to a call that leaves no terminator Windows builds alias snprintf to _snprintf, which leaves a truncated string unterminated Sep 22, 2026
@xroche
xroche merged commit 893a2f9 into master Sep 22, 2026
39 checks passed
@xroche
xroche deleted the fix-win-snprintf-terminator branch September 22, 2026 19:25
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.

A truncated failure message leaves no terminator on Windows, and the crawler aborts

1 participant