Skip to content

Commit 2cce1ab

Browse files
committed
Add alternative to zend_string_dup for persistent strings
And add a ZEND_ASSERT statement so that misuse of zend_string_dup can be detected. See comments in 9eb295f
1 parent 5405676 commit 2cce1ab

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Diff for: Zend/zend_string.h

+13
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,22 @@ static zend_always_inline zend_string *zend_string_copy(zend_string *s)
195195
return s;
196196
}
197197

198+
static zend_always_inline zend_string *zend_string_dup_safe(zend_string *s, bool persistent)
199+
{
200+
if (ZSTR_IS_INTERNED(s) && (!persistent || (GC_FLAGS(s) & IS_STR_PERSISTENT))) {
201+
return s;
202+
} else {
203+
return zend_string_init(ZSTR_VAL(s), ZSTR_LEN(s), persistent);
204+
}
205+
}
206+
207+
/* Callers should use PHP 8.2's definition of zend_string_dup_safe instead,
208+
if they need a temporary or interned persistent string
209+
and aren't sure if it might be a temporary (allocated with emalloc) interned string. */
198210
static zend_always_inline zend_string *zend_string_dup(zend_string *s, bool persistent)
199211
{
200212
if (ZSTR_IS_INTERNED(s)) {
213+
ZEND_ASSERT(!persistent || (GC_FLAGS(s) & IS_STR_PERSISTENT));
201214
return s;
202215
} else {
203216
return zend_string_init(ZSTR_VAL(s), ZSTR_LEN(s), persistent);

0 commit comments

Comments
 (0)