Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alternative to zend_string_dup for persistent strings #9768

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Zend/zend_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,22 @@ static zend_always_inline zend_string *zend_string_copy(zend_string *s)
return s;
}

static zend_always_inline zend_string *zend_string_dup_safe(zend_string *s, bool persistent)
{
if (ZSTR_IS_INTERNED(s) && (!persistent || (GC_FLAGS(s) & IS_STR_PERSISTENT))) {
return s;
} else {
return zend_string_init(ZSTR_VAL(s), ZSTR_LEN(s), persistent);
}
}

/* Callers should use PHP 8.2's definition of zend_string_dup_safe instead,
if they need a temporary or interned persistent string
and aren't sure if it might be a temporary (allocated with emalloc) interned string. */
static zend_always_inline zend_string *zend_string_dup(zend_string *s, bool persistent)
{
if (ZSTR_IS_INTERNED(s)) {
ZEND_ASSERT(!persistent || (GC_FLAGS(s) & (IS_STR_PERSISTENT|IS_STR_PERMANENT)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is my understanding that ZSTR_IS_INTERNED(s) implies IS_STR_PERSISTENT|IS_STR_PERMANENT, unless the string was interned during a request, which is not supposed to happen except when using dl(), according to

php-src/Zend/zend_string.c

Lines 254 to 260 in 33dd226

#if ZEND_RC_DEBUG
if (zend_rc_debug) {
/* PHP shouldn't create persistent interned string during request,
* but at least dl() may do this */
ZEND_ASSERT(!(GC_FLAGS(str) & GC_PERSISTENT));
}
#endif

Are there other cases where ZSTR_IS_INTERNED(s) does not imply IS_STR_PERSISTENT|IS_STR_PERMANENT?

Since this targets master, we might as well change the behavior of zend_string_dup() directly?

return s;
} else {
return zend_string_init(ZSTR_VAL(s), ZSTR_LEN(s), persistent);
Expand Down