Skip to content

Conversation

@t-a-k
Copy link
Contributor

@t-a-k t-a-k commented Jan 7, 2026

PerlIOScalar_write used to blindly use SvCUR() for SVs that is not SvPOK nor SvPOKp and bypasses the code that world normally clear out the leading bytes. This would result as garbage (uninitialized) bytes when the target scalar once held a non-empty string and then undef'ed.

This will fix #24008.


  • This set of changes requires a perldelta entry, and it is included.

PerlIOScalar_write used to blindly use SvCUR() for SVs that is not
SvPOK nor SvPOKp and bypasses the code that world normally clear out
the leading bytes.  This would result as garbage (uninitialized) bytes
when the target scalar once held a non-empty string and then undef'ed.

This will fix GH Perl#24008.
t/io/scalar.t Outdated
}

{ # GH #24008
open my $fh, '>', \my $str or BAIL_OUT $!;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised to see that this suggests using BAIL_OUT. According to perldoc Test::More, BAIL_OUT

        Indicates to the harness that things are going so badly all testing
        should terminate. This includes the running of any additional test
        scripts.

        This is typically used when testing cannot continue such as a
        critical module failing to compile or a necessary external utility
        not being available such as a database connection failing.

        The test will exit with 255.

Why wouldn't a simple die suffice here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@t-a-k I think we are waiting for changing to die, and then this p.r. can be merged

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for delayed response. I will make a commit shortly.

I think that BAIL_OUT is provided because die would change the behavior due to some internal state, such as $SIG{__DIE__}, $! and enclosing eval, so it might not be able to make the test fail reliably.

perlio.c Outdated
Comment on lines 1262 to 1268
if (SvOK(sv))
SvPV_force_nomg_nolen(sv);
else
/* SvPV_force_nomg_nolen would initialize PV even if !SvOK(sv),
but in such case it will emit "Use of uninitialized value"
warning, which is not expected here. */
SvPVCLEAR(sv);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation is strange, the else should be at the same level as the if to match our common usage, see the if/else starting line 1274 for example.

I think the conditional sv_force_normal() above your change is now redundant, since all paths now either force_normal (SvPV_force*()) or set an explicit value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation is strange,

This seems to be due to GitHub's diff rendering. The if (SvOK(sv)) line is indented with a single tab character (this came from the original source before this change), while other added lines are indented without tab characters (according to .editorconfig).
.editorconfig also specifies tab_width = 8 and the else line is indented with 8 spaces, so these lines should appear to be aligned.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would seem that is my fault. Before ec57fb9 this part of the code was in a separate module that hadn't been updated to the "new" core style standard, and I didn't notice I should have fixed the indentation in that process.

I would suggest we follow up with a commit that fixes all the tabs to spaces.

t-a-k added 2 commits January 17, 2026 17:26
Once a scalar variable is undef'ed, its SvCUR might remain the previous
value.  So avoid calling SvCUR() blindly and only call it only when
the SV is defined.

This partially revert my previous commit 3ea1aa9
and intended to be squashed before merge.
This commit is intended to be squashed before merge.
Thanks to the comment from @jkeenan.
@t-a-k
Copy link
Contributor Author

t-a-k commented Jan 17, 2026

I've pushed a commit to change BAIL_OUT to die, and another commit to change the way to avoid calling SvCUR on undef'ed scalar. I thought that this approach is simpler than previous one.

If these are OK, I will squash them to a single commit.

if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND) {
dst = SvGROW(sv, SvCUR(sv) + count + 1);
dst = SvGROW(sv, cur + count + 1);
offset = SvCUR(sv);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this now be offset = cur; ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Uninitialised memory use when Dumping undef'd string

6 participants