libc: snprintf and vsnprintf format into the buffer when the size exceeds INT_MAX, FreeBSD 11.4 fails with EOVERFLOW #8510

Closed
opened 2026-10-20 14:22:06 +00:00 by xXPBSXx · 2 comments
xXPBSXx commented 2026-10-20 14:22:06 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T14:22:06Z.


Repro

snprintf(buf, (size_t)INT_MAX + 2, "x") and snprintf(buf, SIZE_MAX, "x") (for example from a negative int size) return 1 and write "x".
Expected (FreeBSD 11.4 lib/libc/stdio/vsnprintf.c:61-67): when n - 1 > INT_MAX, set errno to EOVERFLOW (84), write a terminating NUL to buf and return -1. INT_MAX + 1 is still accepted.
Actual: snprintf_nid_postfix and vsnprintf_nid_postfix (libc src/Formatting.cpp) pass the size on.
Test: core/libs/tests/Formatting.cpp (new cases; guest_formatting fails before the fix).
Fix branch: https://github.com/xXPBSXx/AnyPS5/tree/fix/snprintf-size-overflow
Earlier attempts: #3982 and #5070 (closed).
AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5943386082 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/8510). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T14:22:06Z. --- ### Repro `snprintf(buf, (size_t)INT_MAX + 2, "x")` and `snprintf(buf, SIZE_MAX, "x")` (for example from a negative `int` size) return 1 and write "x". Expected (FreeBSD 11.4 lib/libc/stdio/vsnprintf.c:61-67): when `n - 1 > INT_MAX`, set errno to `EOVERFLOW` (84), write a terminating NUL to `buf` and return -1. `INT_MAX + 1` is still accepted. Actual: `snprintf_nid_postfix` and `vsnprintf_nid_postfix` (libc `src/Formatting.cpp`) pass the size on. Test: `core/libs/tests/Formatting.cpp` (new cases; `guest_formatting` fails before the fix). Fix branch: https://github.com/xXPBSXx/AnyPS5/tree/fix/snprintf-size-overflow Earlier attempts: #3982 and #5070 (closed). AI-assisted (Claude Code).
MarcZX10 commented 2026-10-20 19:27:28 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T19:27:28Z.


I found the ready branch xXPBSXx/AnyPS5:fix/snprintf-size-overflow and reviewed its one commit. The boundary logic matches the report on inspection: size == INT_MAX + 1 is accepted, larger sizes return before formatting, set guest EOVERFLOW (84), and NUL the buffer when present. There is no open PR for it, but the branch is 193 commits behind current main, so I am not opening a duplicate from that stale base. Please rebase/publish it if you still intend to submit it; I can review the current-head PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6299438170 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/8510#issuecomment-6299438170). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-20T19:27:28Z. --- I found the ready branch `xXPBSXx/AnyPS5:fix/snprintf-size-overflow` and reviewed its one commit. The boundary logic matches the report on inspection: `size == INT_MAX + 1` is accepted, larger sizes return before formatting, set guest `EOVERFLOW` (84), and NUL the buffer when present. There is no open PR for it, but the branch is 193 commits behind current `main`, so I am not opening a duplicate from that stale base. Please rebase/publish it if you still intend to submit it; I can review the current-head PR.
MarcZX10 commented 2026-10-20 19:29:25 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T19:29:25Z.


Correction to my previous comment: I missed that #8511 was closed after the maintainer reported a console run showing snprintf/vsnprintf do not reject sizes above INT_MAX with EOVERFLOW. The branch matches FreeBSD 11.4, but that behavior is not the console behavior AnyPS5 should reproduce. I retract my earlier endorsement of carrying it forward as a fix; no PR should be opened from that branch for this issue unless new target evidence changes the conclusion.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6299482311 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/8510#issuecomment-6299482311). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-20T19:29:25Z. --- Correction to my previous comment: I missed that #8511 was closed after the maintainer reported a console run showing `snprintf`/`vsnprintf` do not reject sizes above `INT_MAX` with `EOVERFLOW`. The branch matches FreeBSD 11.4, but that behavior is not the console behavior AnyPS5 should reproduce. I retract my earlier endorsement of carrying it forward as a fix; no PR should be opened from that branch for this issue unless new target evidence changes the conclusion.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#8510
No description provided.