libc: snprintf and vsnprintf on Windows accept buffer sizes above INT_MAX+1 where FreeBSD returns -1 with EOVERFLOW; fix branch ready #3974

Closed
opened 2026-10-11 20:19:58 +00:00 by husseinkad · 2 comments
husseinkad commented 2026-10-11 20:19:58 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-11T20:19:58Z.


On Windows, snprintf and vsnprintf called with a buffer size too large for an int format into the buffer and return the length. FreeBSD 11.4 vsnprintf.c does if (n != 0) n--; if (n > INT_MAX) and then fails: it sets errno = EOVERFLOW (84), writes an empty string and returns -1. A guest that passes SIZE_MAX or another huge size as "no limit" gets a successful result here and an error on the console.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libc-vsnprintf-overflow (two commits on current main: the fix, then a boundary correction found in review; libc/src/Formatting.cpp, tests/Formatting.cpp, one TechnicalDebt.md line). The check is size != 0 && size - 1 > INT_MAX, as in FreeBSD.

Checks:

  • The new guest_formatting cases fail on main and pass with the change: INT_MAX + 1 still formats (snprintf(buf, INT_MAX + 1, "ab") returns 2 and writes ab), INT_MAX + 2 up to SIZE_MAX give -1, errno 84 and an empty string.
  • Sizes 0, 1, 2 and INT_MAX behave as before (size 0 leaves the buffer alone); a null buffer with an oversized size returns -1 without crashing; errno is untouched on success; sprintf, snprintf_s and vsnprintf_s are unchanged.
  • A differential fuzz against the FreeBSD 11.4 vfprintf.c/vsnprintf.c shows the formatting output identical to main. Two independent reviews: the first one found an off-by-one at INT_MAX + 1 in my first commit, fixed in the second commit and verified by a third review. check_conventions.py is clean and pr_overlap.py finds no overlap.

Caveats: Windows only; the Linux glibc path is not covered and was not built. The behaviour on a console is unverified (the reference is the FreeBSD source). Open PRs #3566, #3567 and #3569 also edit the Windows formatting code, in other functions.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5804817654 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3974). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-11T20:19:58Z. --- On Windows, `snprintf` and `vsnprintf` called with a buffer size too large for an `int` format into the buffer and return the length. FreeBSD 11.4 `vsnprintf.c` does `if (n != 0) n--; if (n > INT_MAX)` and then fails: it sets `errno = EOVERFLOW` (84), writes an empty string and returns -1. A guest that passes `SIZE_MAX` or another huge size as "no limit" gets a successful result here and an error on the console. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libc-vsnprintf-overflow (two commits on current main: the fix, then a boundary correction found in review; `libc/src/Formatting.cpp`, `tests/Formatting.cpp`, one `TechnicalDebt.md` line). The check is `size != 0 && size - 1 > INT_MAX`, as in FreeBSD. Checks: - The new `guest_formatting` cases fail on main and pass with the change: `INT_MAX + 1` still formats (`snprintf(buf, INT_MAX + 1, "ab")` returns 2 and writes `ab`), `INT_MAX + 2` up to `SIZE_MAX` give -1, errno 84 and an empty string. - Sizes 0, 1, 2 and `INT_MAX` behave as before (size 0 leaves the buffer alone); a null buffer with an oversized size returns -1 without crashing; errno is untouched on success; `sprintf`, `snprintf_s` and `vsnprintf_s` are unchanged. - A differential fuzz against the FreeBSD 11.4 `vfprintf.c`/`vsnprintf.c` shows the formatting output identical to main. Two independent reviews: the first one found an off-by-one at `INT_MAX + 1` in my first commit, fixed in the second commit and verified by a third review. `check_conventions.py` is clean and `pr_overlap.py` finds no overlap. Caveats: Windows only; the Linux glibc path is not covered and was not built. The behaviour on a console is unverified (the reference is the FreeBSD source). Open PRs #3566, #3567 and #3569 also edit the Windows formatting code, in other functions. AI-assisted: yes
MarcZX10 commented 2026-10-13 10:31:47 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T10:31:47Z.


I carried Hussein Kadhim’s two commits onto current main, preserving his author attribution, and opened draft PR #5070. The Linux formatting test passes, and MinGW GCC 12 syntax checks pass for the Windows source and tests. This host cannot run the repository’s supported Windows build because its MinGW toolchain uses SjLj exceptions; the Windows test report on the original branch is recorded in the PR. I moved the regression cases away from #4995’s test hunk. The draft is waiting on a merge-order response from #4981 because of a conflict in the shared libc debt file.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6143595377 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3974#issuecomment-6143595377). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-13T10:31:47Z. --- I carried Hussein Kadhim’s two commits onto current main, preserving his author attribution, and opened draft PR #5070. The Linux formatting test passes, and MinGW GCC 12 syntax checks pass for the Windows source and tests. This host cannot run the repository’s supported Windows build because its MinGW toolchain uses SjLj exceptions; the Windows test report on the original branch is recorded in the PR. I moved the regression cases away from #4995’s test hunk. The draft is waiting on a merge-order response from #4981 because of a conflict in the shared libc debt file.
oneandonlydean commented 2026-10-14 06:53:45 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:53:45Z.


Closing: no title is known to reach this, and functions are only changed when a title needs them. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162702550 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3974#issuecomment-6162702550). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T06:53:45Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.
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#3974
No description provided.