Skip to content

Commit e9e7ac5

Browse files
committed
newSVpvn_share: Use new utf8_to_bytes_temp_pv()
The new form saves allocating memory unless it is actually needed
1 parent cffb5af commit e9e7ac5

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

sv.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9886,14 +9886,18 @@ Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
98869886
{
98879887
SV *sv;
98889888
bool is_utf8 = FALSE;
9889-
const char *const orig_src = src;
98909889

98919890
if (len < 0) {
9892-
STRLEN tmplen = -len;
9893-
is_utf8 = TRUE;
9891+
len = -len;
9892+
Size_t size_t_len = len;
98949893
/* See the note in hv.c:hv_fetch() --jhi */
9895-
src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
9896-
len = tmplen;
9894+
if (! utf8_to_bytes_temp_pv((const U8**)&src, &size_t_len)) {
9895+
is_utf8 = true;
9896+
}
9897+
else {
9898+
hash = 0;
9899+
len = size_t_len;
9900+
}
98979901
}
98989902
if (!hash)
98999903
PERL_HASH(hash, src, len);
@@ -9907,8 +9911,6 @@ Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
99079911
SvPOK_on(sv);
99089912
if (is_utf8)
99099913
SvUTF8_on(sv);
9910-
if (src != orig_src)
9911-
Safefree(src);
99129914
return sv;
99139915
}
99149916

0 commit comments

Comments
 (0)