Skip to content

Commit 3dc75b6

Browse files
committed
toke.c - only try to shrink an SV buffer when conceivably possible
A few places within toke.c try to return unused string buffer space by doing something like: if (SvCUR(sv) + 5 < SvLEN(sv)) { SvPV_shrink_to_cur(sv); } The rationale for the 5 is not commented upon. Maybe it's a random thumb in the air, maybe it was 1 byte for the trailing null plus a 4 byte pointer to account for allocation alignment? Either way, on a platform with 8 byte pointers, that code could try to shrink buffers when there's no realistic chance of doing so. (The space between chunk sizes will be at least 1x PTRSIZE, often 2x PTRSIZE at smaller sizes and then progressively more.) For a reallocation that is at least has the potential to be meaningful, the difference between CUR and LEN should be at least: * 1 byte for a trailing null byte + * 1 byte to enable the SV to be COWed + * 1x PTRSIZE This commit changes makes that so.
1 parent 482fd30 commit 3dc75b6

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

toke.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4421,8 +4421,9 @@ S_scan_const(pTHX_ char *start)
44214421
SvUTF8_on(sv);
44224422
}
44234423

4424-
/* shrink the sv if we allocated more than we used */
4425-
if (SvCUR(sv) + 5 < SvLEN(sv)) {
4424+
/* shrink the sv if we allocated more than we used - and conceivably
4425+
* could actually shrink it */
4426+
if (SvCUR(sv) + 2 + PTRSIZE < SvLEN(sv)) {
44264427
SvPV_shrink_to_cur(sv);
44274428
}
44284429

@@ -9793,7 +9794,9 @@ Perl_yylex(pTHX)
97939794
PL_parser->lex_shared->re_eval_str = NULL;
97949795
SvCUR_set(sv,
97959796
PL_bufptr - PL_parser->lex_shared->re_eval_start);
9796-
SvPV_shrink_to_cur(sv);
9797+
if (SvCUR(sv) + PTRSIZE + 2 < SvLEN(sv)) {
9798+
SvPV_shrink_to_cur(sv);
9799+
}
97979800
}
97989801
else sv = newSVpvn(PL_parser->lex_shared->re_eval_start,
97999802
PL_bufptr - PL_parser->lex_shared->re_eval_start);
@@ -11348,7 +11351,7 @@ S_scan_heredoc(pTHX_ char *s)
1134811351
SvREFCNT_dec_NN(newstr);
1134911352
}
1135011353

11351-
if (SvCUR(tmpstr) + 5 < SvLEN(tmpstr)) {
11354+
if (SvCUR(tmpstr) + 2 + PTRSIZE < SvLEN(tmpstr)) {
1135211355
SvPV_shrink_to_cur(tmpstr);
1135311356
}
1135411357

@@ -11877,8 +11880,7 @@ Perl_scan_str(pTHX_ char *start, int keep_bracketed_quoted, int keep_delims, int
1187711880
PL_parser->herelines = herelines;
1187811881

1187911882
/* if we allocated too much space, give some back */
11880-
if (SvCUR(sv) + 5 < SvLEN(sv)) {
11881-
SvLEN_set(sv, SvCUR(sv) + 1);
11883+
if (SvCUR(sv) + 2 + PTRSIZE < SvLEN(sv)) {
1188211884
SvPV_shrink_to_cur(sv);
1188311885
}
1188411886

0 commit comments

Comments
 (0)