fix: preserve errno through XS scope cleanup#27
Draft
Koan-Bot wants to merge 1 commit intocpanel:mainfrom
Draft
Conversation
The XS functions _overload_ft_ops(), _overload_ft_ops_sv(), and _overload_ft_stat() call FREETMPS/LEAVE after the Perl _check() function sets $! (errno). This scope cleanup can trigger SV destruction or other Perl internals that clobber the C errno value, causing $! to be 0 instead of the expected ENOENT/EACCES/etc. after failed operations. Fix: save errno before PUTBACK/FREETMPS/LEAVE and restore it after. This matches the pattern used elsewhere in Perl core (e.g., pp_close). The bug was most visible with stat() on non-existent mocked files (errno lost), while file checks (-e, -f) were less affected because _check_from_stat determines success via array length rather than errno. Fixes the smoker failure reported in cpanel/Test-MockFile#207. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Save and restore
errnoaroundFREETMPS/LEAVEin the three XS callback wrapper functions.Why
_check()(or the user callback) sets$!to indicate the error type (ENOENT, EACCES, etc.) for failed operations. The XS scope cleanup (FREETMPS/LEAVE) can trigger SV destruction or other Perl internals that clobber the Cerrnovalue. This causes$!to be 0 instead of the expected errno after failedstat()calls on mocked files.File checks (
-e,-f) were less affected because_check_from_statdetermines success via stat array length, not errno. Butstat()callers that check$!after a failed stat saw incorrect values.How
Three functions patched with the same pattern:
_overload_ft_ops()— file check path_overload_ft_ops_sv()— file check returning SV path_overload_ft_stat()— stat/lstat pathEach saves
errnobeforePUTBACK/FREETMPS/LEAVEand restores it after. This matches the pattern used in Perl core (e.g.,pp_close).Testing
New test file
t/stat-errno-preservation.tcovering:Fixes the smoker failure reported in cpanel/Test-MockFile#207.