feat(libc): implement getdelim and getline #12920
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12920"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by heditrigui on 2026-10-29T14:22:02Z.
What
Export
getdelim(NIDe0iUSkl6-uU) andgetline(NIDEFEe73sCz-8) inlibc, implementing POSIX.1-2008 and FreeBSD 11.4getdelim.cstream reading.ApplicationHeapAllocate_nid_no_patch,ApplicationHeapReallocate_nid_no_patch) and synchronizes throughflockfile.errnotoEINVAL(22).errnotoEBADF(9).unsigned char), or stops at EOF without a delimiter, terminating with\0in both cases.docs/dev/debt/libc.md.Tested
python tools/check_conventions.py --base upstream/mainpassed with 0 findings.python core/libs/tests/test_public_headers.py --audit-onlyverified all 373 libraries and 14,002 exports (+2 exports) with unique declarations.core/libs/tests/GuestStandardStreams.cpp(CheckGetdelimAndGetline):nullptrlineptr, capacity, and stream return -1 and seterrno = 22).stream.Close(), returns -1 and setserrno = 9).getlineand custom-delimiter chunking viagetdelim(:).feof.feofpreservation.free_nid_postfix.-23and0x10A).*lineptr == nullptrwithcapacity == SIZE_MAXinitializing to default buffer.Checklist
Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T15:54:33Z.
Review: COMMENTED
Thanks @heditrigui. I checked head
faf2386aa9on macOS arm64 against FreeBSD 11.4lib/libc/stdio/getdelim.c@8c639945a4. There are two blockers and two smaller differences from FreeBSD.1. Build break (this is the
windowsfailure).StandardStreams.cpponmainopensextern "C" {at L84 and doesn't close it before the new code. That putsLibcDetail::ExpandLineBufferCapacityinside C linkage, whileFileStream.hpp:413declares it with C++ linkage:Every lane that builds
libcfails here. The relinker lanes only pass because they never build it. To fix it, close the outer block before yournamespace {, and drop the extra}aftergetline_nid_postfix(the newextern "C" {already wraps both functions). With that change,clang++ -fsyntax-onlyis clean.2. The
EOVERFLOWtest row can't pass once the file compiles.overCap = INT64_MAXwith a 16-byte buffer and the line"overflow test\n":pos + 2 > *nis never true, so no growth happens and getline returns 14 with errno 0. I ran the PR's function body verbatim in a harness and got14 errno 0, but the test requires-1 && errno == 84. FreeBSD also returns 14 here, becauseexpandtofitis only called whenlen > *capp. I'd drop that row. TheExpandLineBufferCapacity(…, limit)rows already cover the overflow branch.3. Delimiter comparison. FreeBSD uses
memchr(…, delim, …), which compares against(unsigned char)delim. The PR compares anfgetcresult in 0..255 with the rawint. In my harness on"a\xE9b\nc":delim = -23: PR returns 5 (no match, reads to EOF), FreeBSD returns 2.delim = 0x10A: PR returns 5, FreeBSD returns 4 (matches'\n').Fix:
if (ch == static_cast<unsigned char>(delimiter)) break;4. A null
*lineptrwith a non-zero*n. FreeBSD sets*linecapp = 0when*linep == NULL, so the caller's*nis ignored. The PR instead allocatesmax(*n, 128)bytes. Withline = nullptr, n = SIZE_MAXit requests 0x7fffffffffffffff bytes and returns -1/ENOMEM, where FreeBSD returns the line. Fix:*n = 0; std::size_t initialSize = 128;With 1, 3 and 4 applied (diff below), the harness passes all of the above under ASan+UBSan. It also rechecks the 300-byte growth case (
n = 512) and the read error after partial data (-1).Suggested rows (each one fails on the current head):
Optional notes:
__SERR) on every error path, including EINVAL with a validfp, ENOMEM and EOVERFLOW. Here those paths leaveferror()at 0.FileStream::SetError()exists if you want to match.stream, FreeBSD dereferences it inFLOCKFILE. Returning EINVAL is an invented result, and the debt line's "Null pointers return EINVAL" covers it. The project rule prefers throwing for unmeasured invalid input (same request as on #12924).conventionscheck fails only because the PR template'sAI-assisted:answer is missing from the body.How I tested:
git fetch upstream pull/12920/head,clang++ -std=gnu++20 -fsyntax-onlyofStandardStreams.cpp(RED on the head, clean with the fix), plus a standalone ASan+UBSan harness containing the PR'snamespace {…}…getline_nid_postfixblock verbatim and a minimalFileStream/heap shim (heap throwsbad_alloclikerequireAllocation). I couldn't build the fullguest_standard_streamstarget here because the submodules aren't initialized, so please post that target's output on the next head.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T16:27:23Z.
Review: APPROVED
Re-checked head
eba4ca6858. Points 1, 2, 3 and 4 from my last review are all fixed as suggested, and the two rows I proposed are in the test verbatim.clang++ -std=gnu++20 -fsyntax-onlyis now clean on bothStandardStreams.cpp(it was RED onfaf2386aa9) andGuestStandardStreams.cpp.LibcDetail::ExpandLineBufferCapacityhas C++ linkage again, and both exports stay insideextern "C".namespace {…getline_nid_postfixblock, copied verbatim into a standalone ASan+UBSan harness on macOS arm64, passes these cases:-23and0x10Adelimiters: returns 2, then"b\n"n = SIZE_MAX: returns 2 and the capacity is small"abcd"into a 4-byte buffer)'\0'*n = 0: it gets overwritten by*n = initialSizeinitialSize = max(*n, 128):*nis already 0 at that pointneeded > limit: thenext > limitcheck covers it, sincenext >= neededgit diff --check,merge-treeagainstbb04a4f1eaandcheck_conventions --base upstream/mainare all clean.pr_overlaponly reports the usualdocs/dev/debt/libc.mdL242 append-point neighbours (merge order) and no shared exports.The earlier optional notes are still open but don't block this:
__SERRisn't set on the error paths, and a null stream returns EINVAL where the project would rather throw. A follow-up is fine for both.I still can't build
guest_standard_streamshere because the submodules aren't initialized, so the libc lanes in CI will be the final check.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:54:14Z.
Review: COMMENTED
At
722cd58201core/libs/tests/GuestStandardStreams.cppdoesn't build:CheckGetdelimAndGetlinestarts afterstream.Close();inCheckFgetsSizeOnebut before that function's closing brace, so it is a nested definition. Please closeCheckFgetsSizeOnefirst and post a run of the registered test at the new head.No title on
mainreachesgetdelimorgetlineyet, so this will be read after title-linked PRs.Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-11-06T07:35:17Z.
Review: COMMENTED
Checked head
166570e1087d69fbd0c11a975cff7d46e06c9f42, basee772fb25cc72ee5faf77847f06bbc69533016e82. Built the actual libc module on Linux; all fiveguest_standard_streamstests pass.One extra byte is lost when growth fails. With unbuffered input
ABCDEFGH\n, an eight-byte guest buffer, and a one-shot realloc failure,getlinereturns -1/ENOMEM and setsferror. The pointer/capacity stay unchanged, bytes 0-6 containABCDEFG, and byte 7 keeps its canary. After restoring allocation and clearing the error, exportedfgetcreturns newline:His gone. WithABCDEFG\nZ, the lost byte is the delimiter and the next read returnsZ.The declared FreeBSD source attempts
sappendbefore advancing/refilling the input. Its unbuffered setup selects one-byte refills. A source adapter preserving that order leavesHor the delimiter for the next read. Custom:cases reproduce the difference; successful-growth and no-growth controls pass.Could you preserve the current input byte when growth fails and add a next-byte assertion? This comparison covers unbuffered input against the pinned FreeBSD source, not arbitrary buffered positions or measured console behavior.