Commit Graph
63 Commits
Author SHA1 Message Date
Adrià df53927b7b Merge pull request #10929 from Aspct3434/claude/modest-curie-705h76
fix(libc): read characters outside the BMP as surrogate pairs in fgetwc
2026-10-29 13:49:54 +01:00
Oscar45397andClaude Opus 5.5 1dd153cf22 test(libc): define the setvbuf mode check outside the Windows-only block
CheckSetvbufInvalidMode sat inside the #ifdef _WIN32 block of
CheckStandardStreamDeleteSharing while main calls it on every platform,
so the Linux build failed. It uses nothing Windows-specific: move it
above the block so the check also runs on Linux.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 22:04:44 +01:00
Oscar45397andClaude Opus 5.5 e4e4985106 test(libc): check setvbuf rejects an invalid buffering mode
GuestStandardStreams used setvbuf only with valid modes.
CheckSetvbufInvalidMode requires modes 3, -1 and INT_MAX to return EOF,
as FreeBSD's setvbuf does for any mode other than _IOFBF, _IOLBF and
_IONBF (C11 7.21.5.6: nonzero for an invalid mode). A rejected call with
a buffer must not adopt it: a character written afterwards stays out of
that buffer and the stream still writes and reads back normally.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 22:04:44 +01:00
Claude 84aec8b0ac fix(libc): read characters outside the BMP as surrogate pairs in fgetwc
fputws and wprintf write a UTF-16 surrogate pair as one four-byte
UTF-8 sequence, but fgetwc decoded that sequence into a single code
point above 0xFFFF, which is not a 16-bit guest wchar_t value, and
fgetws stored it truncated: fputws(u"A\U0001F600\n") read back as
A, U+F600, newline.

fgetwc now returns the high surrogate and keeps the low one in the
stream for the next read, so fgetws stores the pair, also when the
buffer fills between the two halves. A successful seek discards it,
as FreeBSD's fseeko resets the stream's mbstate. Sequences that
decode to an overlong form or above U+10FFFF, and lead bytes F8 to
FF, fail with EILSEQ instead of returning made-up characters; the
three-byte surrogates that fputwc writes for a single unit are still
read back as that unit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QegaHM5QeCynUQ2ZLWHE4b
2026-10-28 14:43:48 +01:00
Dean 4c6fcfcb5c Merge pull request #9763 from rooklz/fix/libc-freopen-closed-stream
fix(libc): reopen a closed stream in freopen instead of aborting
2026-10-28 13:48:50 +13:00
Dean 77f68f314b Merge pull request #11560 from Ragbie/fix/write-after-read-update-stream
fix(libc): set up streams for writing after a read as FreeBSD does on Windows
2026-10-27 21:27:48 +13:00
Rookie 4c28647f0a fix(libc): answer EBADF to a null filename on a closed stream as the console does 2026-10-27 08:11:28 +00:00
Rookie 9618254977 fix(libc): reopen a closed stream in freopen instead of aborting 2026-10-27 08:11:28 +00:00
Dean f8c0779c48 Merge pull request #10782 from MarcZX10/marc/fix-perror-closed-stderr
fix(libc): let perror skip a closed standard error
2026-10-27 10:32:10 +13:00
Dean fd0da73ec5 Merge pull request #10781 from MarcZX10/marc/fix-printf-s-closed-stdout
fix(libc): fail printf_s on a closed standard output with EBADF
2026-10-27 09:04:40 +13:00
Evrim Cicek a7b52084ec fix(libc): set up streams for writing after a read as FreeBSD does on Windows
On Windows the UCRT drops a write that directly follows a read on an r+,
w+ or a+ stream: fputc and fprintf report success, fwrite returns 0 and
fputs returns EOF, all without the error indicator, and the file is left
unchanged, also after an fflush. On a stream open only for reading, fputc
after a read reports success as well.

Writes now first do what FreeBSD 11.4's __swsetup does: a stream open only
for reading fails with EBADF and sets the error indicator, and an update
stream that is reading drops its read buffer and ungetc data, clears end
of file and writes at the descriptor's offset. This covers fwrite, fputs,
fputc, putc_unlocked, fprintf, fprintf_s, fputwc, fputws, fwprintf,
fwprintf_s, puts and wprintf, and printf, vprintf and printf_s, which
write to standard output through PrintWindows and now return -1 there
instead of throwing.

Linux is unchanged: glibc writes at the stream position, which the libc
technical debt now records.
2026-10-26 21:53:56 +03:00
orezraeyandClaude Opus 5.5 3dac7eb76c fix(libc): check the fdopen mode before the descriptor and skip the access check as measured on a PS5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-25 17:01:01 -03:00
MarcZX10 2562a7d9d1 test(libc): cover printf_s on a closed standard output 2026-10-25 07:56:20 +00:00
MarcZX10 df44b2aa59 test(libc): cover perror on a closed standard error 2026-10-25 07:56:12 +00:00
MarcZX10 c2efce8ab7 test(libc): cover the wide calls on a closed stream 2026-10-25 06:30:05 +00:00
boykopovar f38d29fd08 refactor(sdk): add canonical headers for remaining libraries 2026-10-24 23:17:42 +03:00
Dean 5669dff560 Merge pull request #9596 from JJenkx/windows/fgetws-console-contract
fix(libc): follow the console fgetws for short buffers and read errors
2026-10-24 15:22:31 +13:00
Queuze 6d31de8561 chore: sync contribution #8873 with main 2026-10-23 20:02:20 -03:00
JJenkxandClaude Opus 5.5 ea6aa0a809 fix(libc): follow the console fgetws for short buffers and read errors
The fgetws in the libc.prx that titles ship (the same 133 bytes in the
copies of PPSA13934, PPSA07923 and PPSA05916, read from disassembly)
differs from this one in two cases:

- A size below 2 returns null at once, without writing the buffer,
  errno or the stream (cmp $0x2,%esi; jge, else the null result). Here a
  size of 1 stored a NUL and returned the buffer, and a size of 0 or less
  set the stream error and errno 22.
- When fgetwc returns -1 after characters were stored, the console
  null-terminates and returns the buffer; it returns null only when
  nothing was stored, and never checks ferror. Here a read error after
  some characters returned null.

The rest already matches: each fgetwc result is stored as its low 16 bits
(mov %ax,(%rbx)), and the newline test compares all 32 bits. A null buffer
or stream, which the console dereferences, keeps failing with errno 22.

guest_standard_streams now expects the console results for sizes 1, 0
and -1 and for an invalid byte after a stored character; its fgetwc error
indicator check is unchanged. The libc debt entry cites the reading and
what it leaves unverified.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-22 16:19:24 -04:00
Queuze a2e07a5eb5 test(libc): check host stdout after freopen 2026-10-21 06:18:20 -03:00
hypermist 04dc63a353 Merge remote-tracking branch 'upstream/main' into fix/libc-utf8-multibyte
# Conflicts:
#	core/libs/prx/libc/src/Multibyte.cpp
2026-10-21 16:20:33 +13:00
Dean 6c12024f7d Merge pull request #8741 from Queuse/fix/libc-freopen-share-delete
fix(libc): share delete on standard stream reopen
2026-10-21 14:47:29 +13:00
Queuze 3916eeaf6f fix(libc): share delete on standard stream reopen 2026-10-20 20:07:16 -03:00
p3nicillin dde6b34d37 feat(libc): implement unlocked stream status queries 2026-10-20 23:41:22 +01:00
hypermistandClaude Opus 5.5 906f18f609 fix(libc): convert multibyte text as UTF-8 like the console's C locale
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
2026-10-21 02:51:59 +13:00
babyhuey 2d8d3b9dc1 fix(libc): keep the host standard stream when a guest freopen of it fails 2026-10-18 09:12:17 -04:00
T3cm0andClaude Opus 5.5 22c584acd3 feat(libc): implement clearerr_unlocked, fileno_unlocked and fgetln
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 16:17:27 -05:00
Adrià 21ca964f3f Merge pull request #5632 from babyhuey/fix/libc-closed-stream-writes
fix(libc): fail stream calls on a stream that a failed freopen closed
2026-10-15 17:03:43 +02:00
Nawasfadili 6509ec274a feat(libc): implement wide and unlocked stream I/O 2026-10-14 14:24:37 +00:00
babyhuey 292752ff5a fix(libc): fail stream calls on a stream that a failed freopen closed 2026-10-14 02:20:06 -04:00
Dean 6546a33944 Merge pull request #4728 from Ondra97/codex/fix-fdopen-access-mode
fix(libc): reject incompatible fdopen access modes on Windows
2026-10-14 19:06:55 +13:00
Ondra97 f2120a6458 fix(libc): reject incompatible fdopen access modes on Windows 2026-10-13 05:30:05 +02:00
David 7aabb9005c fix(libc): fail fseek and ftell on a pipe with ESPIPE on Windows 2026-10-13 02:38:01 +00:00
David dbbd6d582c fix(libc): append through a descriptor opened by fdopen in an append mode on Windows 2026-10-12 20:09:54 +00:00
Dean b701bc5d8d Merge pull request #4372 from milescoviello/fix/fflush-read-stream
fix(libc): leave a stream that is reading untouched in fflush on Linux
2026-10-13 07:22:17 +13:00
Dean 655cba83d5 Merge pull request #4358 from milescoviello/fix/scanf-binary-prefix
fix(libc): read %i without the C23 binary prefix in the scanf family on Linux
2026-10-13 07:19:26 +13:00
Dean b538b28d8b Merge pull request #4354 from milescoviello/fix/fopen-append-position
fix(libc): start append-mode streams at the end of the file
2026-10-13 07:18:50 +13:00
Dean d58ba1010e Merge pull request #4232 from MarcZX100/codex/carry-fdopen-modes-4022
fix(libc): accept FreeBSD fdopen modes
2026-10-13 01:30:04 +13:00
milescovielloandClaude Opus 5.5 89bc9d1082 fix(libc): read %i without the C23 binary prefix in the scanf family on Linux
On Linux sscanf, vsscanf, fscanf and sscanf_s hand their conversions to
glibc. Since glibc 2.38, code built as GNU C++ calls the C23 entry
points (__isoc23_vsscanf, __isoc23_vfscanf, __isoc23_sscanf), whose %i
reads a 0b/0B binary prefix. On main,
sscanf("0b101 x", "%i%n%7s", &value, &end, rest) stores value 5 and
end 5 and leaves "x" for the %s; " -0B11" gives -3. TechnicalDebt
records this.

FreeBSD's %i knows only the 0 and 0x prefixes. FreeBSD 11.4
lib/libc/stdio/vfscanf.c, parseint, lines 345-353 and 371-380:

	case '0':
		if (base == 0) {
			base = 8;
			flags |= PFXOK;
		}
	...
	/* letters ok iff hex */
	case 'A': case 'B': case 'C':
	case 'D': case 'E': case 'F':
	case 'a': case 'b': case 'c':
	case 'd': case 'e': case 'f':
		/* no need to fix base here */
		if (base <= 10)
			break;	/* not legal here */

so "0b101" stores 0, consumes the "0" and leaves "b101", as C17 does
and as the strtol family here already does.

With glibc the scanf family now calls __isoc99_vsscanf and
__isoc99_vfscanf, the entry points GNU C++ used before glibc 2.38
(exported since 2.7), which read %i without the binary prefix. Other
hosts keep std::vsscanf and std::vfscanf. The TechnicalDebt entry is
removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-12 08:38:06 +00:00
milescovielloandClaude Opus 5.5 1337bd7e45 fix(libc): leave a stream that is reading untouched in fflush on Linux
fflush() passed every stream to glibc's fflush(). For a stream that is
reading, glibc seeks the descriptor back and drops the read buffer, and
with a pushed-back character it gets the position wrong: after fgetc()
returned 'a' from "abcdef\n" and ungetc('Z'), fflush() returned 0 and
the next fread() returned "bcdef\n\n". The pushed-back 'Z' was lost and
the last byte came back twice.

FreeBSD's fflush() writes out only a stream that is writing and leaves
every other stream as it is, lib/libc/stdio/fflush.c:

	if ((fp->_flags & (__SWR | __SRW)) == 0)
		retval = 0;
	else
		retval = __sflush(fp);

and __sflush():

	t = fp->_flags;
	if ((t & __SWR) == 0)
		return (0);

fflush() on Linux now returns 0 without calling the host for a stream
whose last operation was not a write (__fwriting() is 0). fflush(NULL)
and streams that are writing are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-12 08:00:21 +00:00
Dean bb67fe1a34 Merge pull request #3612 from karim200003/fix/libc-fgets-nonpositive-size
fix(libc): fail fgets with a size of 0 or less like FreeBSD
2026-10-12 17:33:20 +13:00
milescovielloandClaude Opus 5.5 6a19442f12 fix(libc): start append-mode streams at the end of the file
fopen and freopen leave an "a+" stream where the host CRT puts it, at
the start of the file. On main, after fopen(file, "a+") on a 7-byte
file, ftell returns 0 and the first fgetc returns the first byte of the
file. FreeBSD seeks every append-mode stream to the end when it opens
it, so ftell returns 7 and a read before any fseek returns EOF. Code
that opens a log or save file with "a+" and tests ftell(f) == 0 to see
whether the file is new treats every existing file as empty.

FreeBSD 11.4 lib/libc/stdio/fopen.c (fopen), and the same lines in
lib/libc/stdio/freopen.c (freopen):

	if (oflags & O_APPEND) {
		fp->_flags2 |= __S2OAP;
		(void)_sseek(fp, (fpos_t)0, SEEK_END);
	}

lib/libc/stdio/flags.c sets O_APPEND for every mode that starts with 'a':

	case 'a':	/* open for appending */
		ret = __SWR;
		m = O_WRONLY;
		o = O_CREAT | O_APPEND;

fopen and freopen now seek a stream opened with an 'a' mode to the end
of the file, ignoring a failed seek as FreeBSD does. Writes still go to
the end through O_APPEND.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-12 02:50:07 +00:00
milescovielloandClaude Opus 5.5 589de27c17 fix(libc): accept the fdopen modes FreeBSD accepts
fdopen only took a fixed list of 15 mode strings and failed with EINVAL
for any other, so "rt", "r+t", "re", "rbe" or "rw" gave NULL. FreeBSD's
fdopen parses the mode with __sflags (lib/libc/stdio/flags.c): after
r, w or a it takes b, +, x, e and v and stops at the first other
character ("default: known = 0;"), and only a bad first character or x
on a read-only mode fail with EINVAL. fopen and freopen already parse
modes this way through ParseFileMode (#2033).

fdopen now uses ParseFileMode as well and passes the host its normalised
mode without x, which FreeBSD's fdopen ignores.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-12 01:50:11 +00:00
fablevoidandClaude Opus 5.5 17e03d3c0a fix(libc): fail fgets with a size of 0 or less like FreeBSD
The guest fgets handed the size straight to the host. With a size of 0
glibc and the Windows runtime return NULL without setting errno or the
stream error indicator, and a negative size goes to the Windows
invalid-parameter path. FreeBSD's fgets checks n <= 0 before reading:
it sets __SERR on the stream, sets errno to EINVAL and returns NULL,
leaving the buffer and the file position untouched.

fgets_nid_postfix now does the same before calling the host. The
FileStream flag it uses was only set for encoding errors so far and is
renamed from SetEncodingError/encodingError to SetError/guestError; it
already stays set until clearerr like __SERR.

guest_standard_streams checks sizes 0, -1 and INT_MIN (NULL, errno 22,
error indicator set and cleared by clearerr, buffer and position
untouched) and that a following read still returns the line; it fails
without the change.

Fixes #3084

AI-assisted: yes

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-11 13:49:43 +00:00
DavidandClaude Fable 5.1 9c7aa0d61a feat(libc): implement ftrylockfile
Porpoise PPSA99764 stops after pthread_attr_* (#3494) with the
predicted `FAIL: unresolved ELF import h05OHOMZNMw from
libSceLibcInternal.prx` (idx 311, call site 0x8EC37); RetroArch
PPSA99169 imports it at idx 448 (call site 0xA4A27). h05OHOMZNMw is
ftrylockfile by tools/nid_names.py and aerolib.csv (zecoxao/sce_symbols
2883963a); ps5-payload-dev/sdk f5d40f02 declares
`int ftrylockfile(FILE *)` in include/freebsd/stdio.h.

ftrylockfile tries the same host CRT stream lock that flockfile,
funlockfile and every locking host stdio call on the stream take, so
no second lock exists beside the one stdio uses. It returns 0 when the
lock is acquired or already owned by the caller (recursive, released by
as many funlockfile calls) and -1 when another thread holds it, as
FreeBSD 8c639945 lib/libc/stdio/_flock_stub.c:89-104 does. Linux calls
glibc ftrylockfile. UCRT has no try variant of _lock_file, so Windows
calls TryEnterCriticalSection on the CRITICAL_SECTION that _lock_file
enters (__crt_stdio_stream_data::_lock, ucrt/inc/corecrt_internal_stdio.h,
same layout in Windows SDK 10.0.19041, 10.0.22621 and 10.0.26100). A
null or closed stream throws. The UCRT layout dependency is recorded in
TechnicalDebt.

guest_standard_streams covers trylock on an unlocked stream, re-entry
by the owner, failure from another thread while held (also after one of
two unlocks and while another thread holds flockfile), stdio calls
from another thread blocking on the trylock-held lock, release order,
and null and closed streams.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-10-11 18:42:23 +05:30
Hussein Kadhim 09921dd376 fix(libc): return 0 from scanf when input fails after a suppressed conversion on Windows
C17 7.21.6.2p16 returns EOF only if input fails before the first conversion completes, and a suppressed conversion completes. FreeBSD counts it in nconversions (vfscanf.c, releng/11.4, lines 799-805). The host scanf returned EOF there, so sscanf("12", "%*d%d", &v) reported EOF instead of 0. Each suppressed conversion now records its end with a %n and a result of EOF becomes 0 when one was recorded.
2026-10-11 12:12:48 +02:00
Romans PokrovskisandMarcZX100 228532e9a8 fix(libc): keep unlocked stream status queries lock-free
Use unlocked POSIX EOF/error queries after unlocked byte operations so status synchronization cannot reacquire the stream lock. Keep the verified UCRT status calls on Windows, and cover byte, status, error, and held-lock behavior.

Co-authored-by: MarcZX100 <[email protected]>
2026-10-11 09:18:23 +00:00
MarcZX100 753b856526 feat(libc): implement unlocked character I/O 2026-10-11 09:18:23 +00:00
Adrià FranchandClaude e7605c8fd6 feat(libc): implement rewind
Co-Authored-By: Claude <[email protected]>
2026-10-11 02:54:24 +02:00
Hannah Lyons 92c9296ae3 feat(libc): implement flockfile and funlockfile
(cherry picked from commit d32eaf181aadf79efc4199b22591f1f5f228d00c)
2026-10-10 21:49:58 +00:00