Skip to content

Commit adda0b6

Browse files
committed
add a small buffer to gv_stash_name
1 parent 57ab6c6 commit adda0b6

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

gv.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,7 @@ S_parse_gv_stash_name(pTHX_ HV **stash, GV **gv, const char **name,
15961596
const char *name_cursor;
15971597
const char *const name_end = nambeg + full_len;
15981598
const char *const name_em1 = name_end - 1;
1599+
char smallbuf[64]; /* small buffer to avoid a malloc when possible */
15991600

16001601
PERL_ARGS_ASSERT_PARSE_GV_STASH_NAME;
16011602

@@ -1627,7 +1628,11 @@ S_parse_gv_stash_name(pTHX_ HV **stash, GV **gv, const char **name,
16271628
}
16281629
else {
16291630
char *tmpbuf;
1630-
Newx(tmpbuf, *len+2, char);
1631+
/* use our pre-allocated buffer when possible to save a malloc */
1632+
if ( *len+2 <= sizeof smallbuf)
1633+
tmpbuf = smallbuf;
1634+
else
1635+
Newx(tmpbuf, *len+2, char);
16311636
Copy(*name, tmpbuf, *len, char);
16321637
tmpbuf[(*len)++] = ':';
16331638
tmpbuf[(*len)++] = ':';
@@ -1641,7 +1646,7 @@ S_parse_gv_stash_name(pTHX_ HV **stash, GV **gv, const char **name,
16411646
else
16421647
GvMULTI_on(*gv);
16431648
}
1644-
if (key != *name)
1649+
if (key != *name && key != smallbuf)
16451650
Safefree(key);
16461651
if (!*gv || *gv == (const GV *)&PL_sv_undef)
16471652
return FALSE;

0 commit comments

Comments
 (0)