Repository navigation
feat(libcurl): implement buffer curl_m*printf over guest va_list - #17390
ThorstenFR-Cyber wants to merge 13 commits into
Conversation
Port the two passes of curl 7.81.0 lib/mprintf.c with LP64 argument sizes and without the Windows I32/I64 qualifiers, reading arguments from the guest SysV va_list. Floating-point conversions go through the guest snprintf. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gabichuuuuu
left a comment
There was a problem hiding this comment.
Windows run of head 391ecdce76, merged onto main 095ff72367. To save builds, this ran in one tree together with 24 other open PRs that touch different code; all 25 merged cleanly. Setup: Windows 11, WinLibs GCC 15.2 (x86_64-ucrt-posix-seh), Release, BUILD_TESTING=ON.
guest_curl_formatwith--repeat until-fail:5: 5 / 5 pass.- No negative control: on
mainthese six exports throwNotImplemented. - Differential check against curl itself, not part of this PR. I linked a test against both this PR's
curl_msnprintf_nid_postfixand the host build of the bundled curl 7.81 (3rdparty/curl,lib/mprintf.c), and fed both the same arguments. There were 200,000 random formats, each with anintconversion (d i u x X o c), anllconversion, a floating conversion (f e E g G),%s(including null) and%p, with random-+ #0flags, widths 0-12 and precisions 0-10, literal text and%%between them, and output sizes of 0-11 or 256. Every result and buffer matches, including truncation,(nil)and the#/0combinations.
The only differences come with floating values that end exactly on a tie or need more than 17 digits: %.0f of 2.5 is 3 here and 2 in the host curl, and %f of 1e100 keeps 17 exact digits. That isn't this PR. The values go through the guest snprintf, which deliberately formats as the titles' libc.prx does (docs/dev/debt/libc.md, the "%e, %f and %g" entry). On the console, curl's mprintf would get the same digits from that libc.prx, so going through the guest snprintf is the right choice. The debt entry could say so, so the difference from a host build of curl isn't mistaken for a bug.
|
Windows check of head Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.
AI-assisted: yes. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@gabichuuuuu |
CurlFormat.cpp ports both passes of lib/mprintf.c from curl tag curl-7_81_0, so it now starts with that file's copyright and licence header, unchanged.
…d curl_maprintf entry
…he merge Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
curl_global_init_mem now selects the guest callbacks curl_free releases through, so the aprintf buffer grows with Curl_crealloc as Curl_dyn_addn does, frees through Curl_cfree on failure and duplicates the empty result with Curl_cstrdup, as curl 7.81.0's curl_mvaprintf does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
A suggestion for the tests, from a port of the same template<typename... T> void Oracle(const char* format, T... arguments) {
for (std::size_t size : {0, 1, 7, 256}) {
char host[256], guest[256];
std::memset(host, 'x', sizeof(host));
std::memset(guest, 'x', sizeof(guest));
const int expected = curl_msnprintf(host, size, format, arguments...);
const int actual = curl_msnprintf_nid_postfix(guest, size, format, arguments...);
Require(expected == actual && std::memcmp(host, guest, sizeof(host)) == 0);
}
}This only holds for formats that don't depend on Cases that caught mistakes in mine, in case they are useful here:
AI-assisted: yes (Claude Code) |
|
Independent check on Windows, with an oracle test written separately from this PR. Setup. Windows 11 Pro 10.0.26300 and WinLibs GCC 15.2.0, Release build,
What the test covers. It links
The test also catches errors: in a separate port of Two inputs are undefined in curl 7.81.0, and this PR may want to decide them explicitly; neither is tested above:
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks @LeandroLP23 and @Ducengarth for the independent checks. I pushed three commits on top of
On the oracle suggestion: I ran the port against the host curl 7.81.0 Tested on Windows 11, WinLibs GCC 15.2.0, Release: all 11 AI-assisted: yes. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I cross-checked this PR's formatter (
No issues found. Co-authored with Claude Code |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — changes requested
Changes are required before merge: handle negative * widths for floating-point conversions as left-justified fields.
What I ran
Reviewed head a30a3287c49b9854d56d43fe99aa8a6409e2c4ba using the prepared diff and repo_read.py source reader. No commands or tests were run.
Results
| Check | Result | Note |
|---|---|---|
| Code review | failed | Negative float width is dropped |
| Tests | not run | Author reports Windows tests passed |
Findings
Blocking
core/libs/prx/libcurl/src/CurlFormat.cpp:774-776: For a floating conversion with a negative*width,floating()only appends a width when it is nonnegative, but does not set left alignment. Thus a format such as%*fwith width-8is formatted without the requested field width or left justification. Normalize negative dynamic float widths to positive width and apply the left flag, as the integer conversion path does.
Non-blocking
None.
Not verified
No tests were run. The changed files core/libs/prx/libcurl/CMakeLists.txt, core/libs/prx/libcurl/Export.cpp, core/libs/prx/libcurl/src/CurlFormat.cpp, core/libs/prx/libcurl/src/CurlFormat.hpp, core/libs/prx/libcurl/tests/CurlFormat.cpp, and docs/dev/debt/libcurl.md were present in the review bundle; the implementation, API wiring, and tests were read. The complete text of Export.cpp, CMake configuration, and debt documentation was not read. Linux and console behavior were not verified. The author's reported test results were not independently run.
To get this to a good state
Handle negative dynamic widths in floating conversions and add coverage for %*f with a negative width. Rerun the reported guest_curl tests on the new head.
Checked at a30a328 against the upstream file this port names, curl lib/mprintf.c at 801bd5138ce31aa0d906fa4e2eabfc599d74e793. Upstream formatf turns a negative '*' width into FLAGS_LEFT plus a positive local width, but its FORMAT_DOUBLE branch re-reads the width from vto[p->width], which still holds the negative value, and appends a width only if(width >= 0). So upstream formats %*f with -8 as %-f: left flag, no width. CurlFormat.cpp does the same (shared width resolution at lines 278 to 285, floating() at 674 to 690), so the port matches the library it reproduces.
MertCtn
left a comment
There was a problem hiding this comment.
Nit on the [Formatting] debt entry: sibling entries link the pinned upstream source (e.g. easy.c, formdata.c), but this one says follow `lib/mprintf.c` of curl 7.81.0 without a link. Suggested:
- follow `lib/mprintf.c` of curl 7.81.0 with LP64 argument sizes
+ follow pinned [lib/mprintf.c](https://github.com/curl/curl/blob/801bd5138ce31aa0d906fa4e2eabfc599d74e793/lib/mprintf.c) with LP64 argument sizes(That SHA is the checked-in 3rdparty/curl submodule commit, and lib/mprintf.c exists there.)
Note the link pushes the entry over the 1000-char debt-length limit (969 chars now), so something small has to give to stay under it — e.g. through the guest → with the guest plus dropping the , which the console's curl also gets from libc.prx`` clause lands at 996 chars with no gap statement lost. Happy to post the exact one-line replacement if useful.
Verified locally: debt_fold.py --check passes, check_conventions.py scoped to the one-line debt change passes, and the full guest_curl suite (13/13, including guest_curl_format) passes on the 18468 stack containing this commit.
ligmaSec
left a comment
There was a problem hiding this comment.
Please move the debt changes into a fragment and add a permalink to the pinned upstream source in the CurlFormat.cpp attribution. The source currently gives the commit hash while the debt edits go straight into docs/dev/debt/libcurl.md.
I ran guest_curl_format on Linux with the patch applied. It passed the buffer, allocation, and guest argument-formatting fixtures.
AI-assisted: yes.
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
THEMANnt
left a comment
There was a problem hiding this comment.
On the negative dynamic width finding above: the pinned curl 7.81.0 formatter rereads the raw width in FORMAT_DOUBLE and emits a numeric width only when that value is nonnegative. This PR does the same. I would keep the curl behavior unless guest measurements show a difference, and add this case to the existing backend comparison fixture so the intended result stays explicit.
What
Implements
curl_mvsnprintf,curl_msnprintf,curl_mvsprintf,curl_msprintf,curl_mvaprintfandcurl_maprintf, which threw throughNotImplemented_nid_no_patch. libcurl goes from 56/85 to 62/85.The host libcurl cannot format guest arguments. On a MinGW host
longis 32 bits, the guestva_listis SysV, and curl'smprintf.cenables theI32/I64qualifiers under__MINGW32__.src/CurlFormat.cppports both passes oflib/mprintf.cfrom curl tagcurl-7_81_0(the submodule's source) with LP64 sizes and noI32/I64:l,zandOread 8 bytes;%n,%hnand%lnwrite 4, 2 and 8 bytes throughGuestCopyOut_nid_no_patch;va_listwithLibcDetail::FormatArguments, and the caller's list is left unchanged.These follow curl's code: positional parameters,
*widths and precisions,(nil)for NULL strings and pointers,%Squoting, the 128-parameter limit (-1), the return value ofmsnprintf(characters stored, not the would-be length), and theaprintfresult limit (DYN_APRINTF, 8000000) returning NULL.aprintfresults come from the guest heap and are released withcurl_free.Floating-point conversions build the same
%[flags][width][.prec][l]f|e|gstring as curl and format it with the guestsnprintfinstead of the consolesprintf.Inputs whose behaviour in curl is undefined throw instead. They are listed in a new Formatting entry in
docs/dev/debt/libcurl.md:va_stack);endposoverflow);%ndestination that is not writable guest memory.The
FILEand stdout variants (curl_m[v]printf,curl_m[v]fprintf) remain stubs for a follow-up PR.Tested
Windows 11, WinLibs GCC 15.2.0
x86_64-ucrt-posix-sehr7, full build pluslibs.ctest --test-dir build -R guest_curl --output-on-failure: 9/9 passed. Onmain, the 8 existing tests also pass.guest_curl_formattest covers:%ld/%lu/%lld/%zu/%lx/%qd;%dreading the low 32 bits of a slot;%c, strings and NULL; pointers and NULL;%%; a conversion curl does not know that still consumes its argument;*arguments, including negative widths;%nof three sizes;%nafter truncation;v*entry points with an unchanged guestva_list;maprintfat and above the 8000000-byte limit.tools/check_conventions.py --base upstream/mainpassed.tools/check_test_registration.py buildpassed.core/libs/tests/test_public_headers.py --audit-onlypassed.tools/progress.py --module libcurlreports 56/85 before and 62/85 after.tools/pr_overlap.py: it could not compare docs(debt): separate entries with blank lines and drop derived export totals #14212 (blank-line reformat of the debt files, already conflicting withmain). That PR does not touch these functions.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code