fix(libc): keep the caller errno when localtime cannot represent a time #4015
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4015"
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 sherySJ on 2026-10-11T21:16:33Z.
What
When a time is outside the range
struct tmcan hold,localtimeandlocaltime_sreturnNULLand leave the host conversion's errno in the guest's errno. On Linux that is glibc'sEOVERFLOW, which is 75, and 75 isEPROGUNAVAILin the guest's FreeBSD numbering. On Windows it is the CRT'sEINVAL.The console's FreeBSD 11 tzcode (
contrib/tzcode/stdtime/localtime.con stable/11) returnsNULLfromtimesubon overflow without touching errno, asgmtimeandgmtime_salready do here.localtime_snow saves the caller's errno before the host conversion and restores it when the conversion fails.GuestTimeConversion.cppsets errno to a sentinel and checks thatgmtime,gmtime_s,localtimeandlocaltime_skeep it atINT64_MINandINT64_MAX. The existingctimecheck, which expectsEINVALfor an unrepresentable time, is unchanged and still passes.Tested
main43e3a70f8: full build passed.ctest -j2passed 994 of 994; the GPU and display tests skip on this headless machine.main'sTime.cpp,guest_time_conversionaborts with "localtime_s of an unrepresentable time changed errno". With the change it passes.localtime_sfailure branch.python3 tools/check_conventions.py --base upstream/mainandgit diff --checkpassed.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T23:20:26Z.
What is the 84 based on for the console? FreeBSD's
timesubsetsEOVERFLOWonly in the current tzcode; in stable/11 (contrib/tzcode/stdtime/localtime.c) it returnsNULLand leaves errno alone, which is whatgmtimedoes on main. The host errno leaking out oflocaltimeandlocaltime_sis a real bug; keeping the guest's errno unchanged there fixes it without a new value.Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T15:33:29Z.
Windows test of head
480382d66merged onto currentmain(d592d54e1, clean merge; built together with two other open PRs, so the run is for the combination): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors,-j2.guest_time_conversion(the test this PR changes) passes. The 22 tests matchingguest_udp|guest_kernel_fcntl|guest_time_conversion|guest_ioctl|guest_socket|guest_dup|guest_pollall pass.Not checked: that the test fails without the change, Linux, a title run.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by sherySJ on 2026-10-12T23:46:02Z.
You're right, thanks. I checked stable/11's
contrib/tzcode/stdtime/localtime.c:timesubreturnsNULLon overflow and never touches errno, and the 84 came from current tzcode. I've force-pushed the change you suggested:localtimeandlocaltime_snow save the caller's errno before the host conversion and restore it when the host conversion fails, so neither Linux's 75 nor the Windows CRT'sEINVALreaches the guest.gmtimeandgmtime_sare unchanged.INT64_MINandINT64_MAX. Againstmain, it fails atlocaltime_s.I've updated the PR title and description to match. @xXPBSXx, thanks for the Windows run. That run was of the previous head, so it doesn't cover this one.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T02:42:39Z.
The new head removes the four
CheckAsctimerows fromGuestTimeConversion.cpp(lines 277-283 on main); please put them back. The errno change itself is right now.Imported from boykopovar/AnyPS5. Originally posted by sherySJ on 2026-10-13T04:36:39Z.
Restored the four
CheckAsctimerows inGuestTimeConversion.cpp(they were dropped by mistake when I reworked the branch); force-pushed as f656da3ad. The diff to that file is now only the errno checks. Tested on Ubuntu 24.04 x86-64:cmake --build build --target guest_time_conversion_tests,ctest --test-dir build -R guest_time_conversion(passed),python3 tools/check_conventions.py --base upstream/main(passed).Imported from boykopovar/AnyPS5. Originally posted by sherySJ on 2026-10-13T07:37:11Z.
Rebased onto current main (the merged Windows pre-1970/post-3000 localtime fix in
Time.cppconflicted); force-pushed as 4aede946c. The failinglocaltime_sandlocaltime_rpaths still restore the caller's errno, and the new EINVAL on tm_year overflow is left as main has it. Tested on Ubuntu 24.04 x86-64:cmake --build build --target guest_time_conversion_tests,ctest --test-dir build -R guest_time_conversion(passed),python3 tools/check_conventions.py --base upstream/main(passed). Not built or run on Windows.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T10:37:28Z.
Checked the diff,
guest_time_conversionand the 90 libs/libc tests on Linux; the new errno checks fail without theTime.cppchange.localtimegoes throughlocaltime_s, so both keep the caller's errno on failure. Merging once the full Build is green.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T13:00:18Z.
The full Windows run failed in
guest_time_conversion: "localtime_s of an unrepresentable time changed errno". On WindowsINT64_MIN/INT64_MAXare reduced by calendar cycles, so the hostlocaltime_ssucceeds and thetm_yearoverflow branch inlocaltime_s_nid_postfixseterrno = EINVAL, which the new errno checks forbid.Pushed a small commit that restores the caller's errno in that branch too (
ctimesets its ownEINVAL, so its check is unaffected). Linux build and the 92 libs/libc tests pass; the Windows result comes from the full run, merging once it is green.