Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions perlio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1265,14 +1265,14 @@ PerlIOScalar_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
SETERRNO(EINVAL, SS_IVCHAN);
return 0;
}
/* Avoid calling SvCUR() on undef'ed SVs */
STRLEN const cur = SvOK(sv) ? SvCUR(sv) : 0;
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; ?

Copy link
Contributor Author

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; ?

I think this has its pros and cons. Referencing cur after SvGROW will make this variable live across the function call, which will impose more register pressure and might worsen performance. (I assume that SvCUR is always valid after SvGROW, and re-fetching SvCUR is not so complex operation.)

Copy link
Contributor

Choose a reason for hiding this comment

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

You could move the assignment before the SvGROW() to avoid that.

Though I doubt the extra memory access of the extra SvCUR() will make any measurable difference.

s->posn = offset + count;
}
else {
STRLEN const cur = SvCUR(sv);

/* ensure we don't try to create ridiculously large
* SVs on small platforms
*/
Expand Down
6 changes: 6 additions & 0 deletions pod/perldelta.pod
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,12 @@ used to be parsed incorrectly.

=item *

When a scalar variable used as the target of a filehandle is C<undef>'ed
while being output, garbage bytes would be left in that scalar.
[L<GH #24008|https://github.com/Perl/perl5/issues/24008>]

=item *

XXX

=back
Expand Down
10 changes: 9 additions & 1 deletion t/io/scalar.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use strict;
use Fcntl qw(SEEK_SET SEEK_CUR SEEK_END); # Not 0, 1, 2 everywhere.
use Errno qw(EACCES);

plan(128);
plan(129);

my $fh;
my $var = "aaa\n";
Expand Down Expand Up @@ -528,3 +528,11 @@ SKIP:
select((select($fh), ++$|)[0]);
ok(!(print $fh "x"), "write to a large offset");
}

{ # GH #24008
open my $fh, '>', \my $str or die $!;
print $fh "xxxxx";
undef $str;
print $fh "y";
is($str, "\0\0\0\0\0y", "write to undef'ed variable");
}
Loading