Skip to content

fix(libc): print the guest errno message for %m in the printf family - #4353

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/printf-errno-message
Oct 12, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/printf-errno-message

Conversation

@milescoviello

@milescoviello milescoviello commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What

Fixes #4045.

FreeBSD's printf formats %m as the message for the errno value at the time of the call. On Linux a format with %m went to glibc, which looks the number up in Linux's table, so guest errno values above 34 printed another error's text. With errno 35 (EAGAIN), "[%m]" printed [Resource deadlock avoided] instead of [Resource temporarily unavailable], and errno 63 (ENAMETOOLONG) printed Out of streams resources. On Windows, and on Linux when the format also had a wide argument, the guest formatter threw on %m (listed in TechnicalDebt.md).

FreeBSD 11.4, lib/libc/stdio/vfprintf.c:

	saved_errno = errno;
	...
		case 'm':
			cp = strerror(saved_errno);
			size = (prec >= 0) ? strnlen(cp, prec) : strlen(cp);
			sign = '\0';
			break;
  • The guest formatter saves errno when it starts and formats %m as a string holding the guest strerror_r text, so width, precision and - apply as they do in FreeBSD's string path. errno is left unchanged.
  • On Linux a format with %m goes to the guest formatter, as a format with a wide argument already does.
  • TechnicalDebt.md no longer lists %m as throwing.

asprintf on Linux still calls the host's vasprintf directly. #3978 routes it through vasprintf, which then picks this up.

Overlap: #3987 (Windows printf conversions, carrying #3975) keeps %m throwing on Windows and lists it among its unsupported cases. This branch implements %m there instead, so whichever lands first, the other needs a small rebase: the conversion chain in FormatWindows and that test line.

Tested

  • Rebased onto main 5a59d4e after fix(libc): format 16-bit wide arguments in asprintf on Linux #3978 (asprintf, same test file): guest_formatting passes with this change and fails with that main's formatter; all 295 guest_* tests pass.
    Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags.
  • guest_formatting gained %m checks: errno 35 with [%m][%.8m][%-10.5m], errno 63 through vsnprintf, and errno unchanged afterwards. The test fails with main's WindowsFormatting.hpp and Formatting.cpp ("Formatting check failed") and passes with this change.
  • ctest -R '^guest_' -E '^guest_video_out_': 281/281 pass.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

FreeBSD's printf formats %m as the message for the errno value at the
time of the call. On Linux a format with %m went to glibc, which looks
the number up in Linux's table, so guest errno values above 34 got
another error's text: with errno 35 (EAGAIN) "[%m]" printed
"[Resource deadlock avoided]" instead of "[Resource temporarily
unavailable]", and errno 63 (ENAMETOOLONG) printed "Out of streams
resources". On Windows, and on Linux when the format also had a wide
argument, the guest formatter threw on %m.

FreeBSD 11.4, lib/libc/stdio/vfprintf.c:

	saved_errno = errno;
	...
		case 'm':
			cp = strerror(saved_errno);
			size = (prec >= 0) ? strnlen(cp, prec) : strlen(cp);
			sign = '\0';
			break;

The guest formatter now saves errno when it starts and formats %m as a
string with the guest strerror_r text, so width, precision and the -
flag apply as FreeBSD's string path applies them. On Linux a format with
%m goes to the guest formatter like one with a wide argument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@xXPBSXx

xXPBSXx commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Windows test: head 4ece6750b merged onto current main 8751a2940 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: full build 0 errors; ctest with the filter guest_formatting|guest_wide_formatting: guest_wide_formatting passed, guest_formatting passed (2 tests, 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

@oneandonlydean oneandonlydean self-assigned this Oct 12, 2026
@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4548 core/libs/prx/libc/include/WindowsFormatting.hpp L5
docs/dev/TechnicalDebt.md L654

@oneandonlydean
oneandonlydean merged commit bde332a into boykopovar:main Oct 12, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libc: printf %m prints Linux's message for the guest errno on Linux and throws on Windows; fix branch ready

3 participants