Skip to content

Fix #81142: memory leak when unserialize()ing associative array #7160

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

Closed
wants to merge 1 commit into from

Conversation

cmb69
Copy link
Member

@cmb69 cmb69 commented Jun 16, 2021

We should not internalize unserialized array keys, because that easily
wastes a lot of memory for a small gain. Instead, we check whether the
string is already internalized, and use that; otherwise we use a new
string.

This patch is based on an idea by @nikic.

We should not internalize unserialized array keys, because that easily
wastes a lot of memory for a small gain.  Instead, we check whether the
string is already internalized, and use that; otherwise we use a new
string.

This patch is based on an idea by @nikic.
@cmb69 cmb69 added the Bug label Jun 16, 2021
@@ -947,7 +947,13 @@ use_double:
} else if (len == 1) {
ZVAL_INTERNED_STR(rval, ZSTR_CHAR((zend_uchar)*str));
} else if (as_key) {
ZVAL_STR(rval, zend_string_init_interned(str, len, 0));
zend_string *key = zend_string_init(str, len, 0);
zend_string *ikey = zend_interned_string_find_permanent(key);
Copy link
Member

Choose a reason for hiding this comment

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

This will only work for strings that are created during startup, which is probably not terribly useful. We'd be mainly interested in strings that are either interned per-request or part of opcache. I think this would require adding a new hook for this purpose that can be replaced by opcache.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this ought to use interned strings at all.

It looks simpler to add a table to unserialize data for keys.

Copy link
Member

Choose a reason for hiding this comment

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

I think the idea here is specifically to reuse normal interned strings for things like object property names, which are usually available as interned strings.

Maybe @dstogov can comment.

Copy link
Member

Choose a reason for hiding this comment

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

With opcache zend_string_init_interned() doesn't create new interned strings (just search for existing ones).
To implement the same behavior without opcache, we need to introduce a new callback in zend_string API.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that we want to introduce a new callback in the stable PHP versions. So, postpone the fix to PHP 8.1?

@cmb69
Copy link
Member Author

cmb69 commented Jul 15, 2021

Sorry, I don't have the time to work on this.

@cmb69 cmb69 closed this Jul 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants