Skip to content

Commit 45f235c

Browse files
tonycozatoomic
authored andcommitted
(perl #17844) don't update SvCUR until after we've done moving
SvCUR() before the SvGROW() calls could result in reading beyond the end of a buffer. It wasn't a problem in the normal case, since sv_grow() just calls realloc() which has its own notion of how big the memory block is, but if the SV is SvOOK() sv_backoff() tries to move SvCUR()+1 bytes, which might be larger than the currently allocated size of the PV.
1 parent cb3d73d commit 45f235c

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

doop.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,6 @@ Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
10871087
lsave = lc;
10881088
rsave = rc;
10891089

1090-
SvCUR_set(sv, len);
10911090
(void)SvPOK_only(sv);
10921091
if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
10931092
dc = SvPV_force_nomg_nolen(sv);
@@ -1103,6 +1102,7 @@ Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
11031102
sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
11041103
dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
11051104
}
1105+
SvCUR_set(sv, len);
11061106

11071107
if (len >= sizeof(long)*4 &&
11081108
!(PTR2nat(dc) % sizeof(long)) &&

t/op/bop.t

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ BEGIN {
1818
# If you find tests are failing, please try adding names to tests to track
1919
# down where the failure is, and supply your new names as a patch.
2020
# (Just-in-time test naming)
21-
plan tests => 501;
21+
plan tests => 502;
2222

2323
# numerics
2424
ok ((0xdead & 0xbeef) == 0x9ead);
@@ -669,3 +669,12 @@ foreach my $op_info ([and => "&"], [or => "|"], [xor => "^"]) {
669669
like $@, $expected, $description;
670670
}
671671
}
672+
673+
{
674+
# perl #17844 - only visible with valgrind/ASAN
675+
fresh_perl_is(<<'EOS',
676+
formline X000n^\\0,\\0^\\0for\0,0..10
677+
EOS
678+
'',
679+
{}, "[perl #17844] access beyond end of block");
680+
}

0 commit comments

Comments
 (0)