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
Closed
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions ext/standard/tests/serialize/bug81142.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Bug #81142 (memory leak when unserialize()ing associative array)
--FILE--
<?php
$mem0 = memory_get_usage();
$ctr = 0;
unserialize(serialize(["foo_$ctr" => 1]));
$mem1 = memory_get_usage();
var_dump($mem1 - $mem0);
?>
--EXPECT--
int(0)
8 changes: 7 additions & 1 deletion ext/standard/var_unserializer.re
Original file line number Diff line number Diff line change
Expand Up @@ -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?

if (ikey) {
zend_string_release(key);
key = ikey;
}
ZVAL_STR(rval, key);
} else {
ZVAL_STRINGL(rval, str, len);
}
Expand Down