Skip to content

Comments

fix: preserve errno through XS scope cleanup#27

Draft
Koan-Bot wants to merge 1 commit intocpanel:mainfrom
atoomic:koan.atoomic/fix-stat-errno-clobber
Draft

fix: preserve errno through XS scope cleanup#27
Koan-Bot wants to merge 1 commit intocpanel:mainfrom
atoomic:koan.atoomic/fix-stat-errno-clobber

Conversation

@Koan-Bot
Copy link
Contributor

What

Save and restore errno around FREETMPS/LEAVE in 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 C errno value. This causes $! to be 0 instead of the expected errno after failed stat() calls on mocked files.

File checks (-e, -f) were less affected because _check_from_stat determines success via stat array length, not errno. But stat() 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 path

Each saves errno before PUTBACK/FREETMPS/LEAVE and restores it after. This matches the pattern used in Perl core (e.g., pp_close).

Testing

New test file t/stat-errno-preservation.t covering:

  • errno set by callback is preserved through stat/lstat/file-check operations
  • Custom errno (EACCES) preserved through XS cleanup
  • Default ENOENT set when callback omits errno

Fixes the smoker failure reported in cpanel/Test-MockFile#207.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant