Commit b1f270f
committed
SvPV_shrink_to_cur: don't be unrealistic, do include space for COW
The `SvPV_shrink_to_cur` macro shrinks an allocation to `SvCUR(sv) + 1`,
which does not include an additional byte for Copy-On-Write (COW).
GH#22116 - a902d92 - short-circuited
constant folding on CONST OPs, as this should be unnecessary. However,
Dave Mitchell noticed that it had the inadvertent effect of disabling
COW on SVs holding UTF8 string literals (e.g. `"\x{100}abcd"`).
When a CONST OP is created, `Perl_ck_svconst` should mark its SV as
being COW-able. But SVs built via `S_scan_const`, when that has
called `SvPV_shrink_to_cur`, have resulting `SvLEN(sv)` values that
fail the `SvCANCOW(sv)` test. Previously, constant folding had the
effect of copying the literal into a buffer large enough for COW.
This commit modifies `SvPV_shrink_to_cur` so that it adds an
additional byte to allow for subsequent direct COWing.
The macro has also been modified to use the new `expected_size` macro,
so that reallocation will not be attempted if the resulting buffer
size is not going to be any smaller.
The intended saving is compared with SvLEN(sv), rather than SvCUR(sv),
as this also helps in cases such as SvCUR(sv) having previously been
set to zero and a size smaller than PERL_STRLEN_NEW_MIN is requested.
This will also enable some size checks at call sites to be simplified.1 parent c6b821f commit b1f270f
1 file changed
+16
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1590 | 1590 | | |
1591 | 1591 | | |
1592 | 1592 | | |
1593 | | - | |
1594 | | - | |
1595 | | - | |
| 1593 | + | |
| 1594 | + | |
| 1595 | + | |
| 1596 | + | |
| 1597 | + | |
| 1598 | + | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
| 1608 | + | |
1596 | 1609 | | |
1597 | 1610 | | |
1598 | 1611 | | |
| |||
0 commit comments