Skip to content

Fix WeakMap object reference offset causing TypeError #8995

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

Merged
merged 2 commits into from
Jul 15, 2022
Merged
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
32 changes: 32 additions & 0 deletions Zend/tests/weakrefs/weakmap_object_reference.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
WeakMap object reference offset
--FILE--
<?php

$map = new WeakMap;
$obj = new stdClass;
$obj2 = &$obj;

$map[$obj] = 1;
var_dump(count($map));
var_dump($map);
var_dump(isset($map[$obj]));
var_dump(!empty($map[$obj]));
var_dump($map[$obj]);

?>
--EXPECT--
int(1)
object(WeakMap)#1 (1) {
[0]=>
array(2) {
["key"]=>
object(stdClass)#2 (0) {
}
["value"]=>
int(1)
}
}
bool(true)
bool(true)
int(1)
4 changes: 4 additions & 0 deletions Zend/zend_weakrefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ static zval *zend_weakmap_read_dimension(zend_object *object, zval *offset, int
return NULL;
}

ZVAL_DEREF(offset);
if (Z_TYPE_P(offset) != IS_OBJECT) {
zend_type_error("WeakMap key must be an object");
return NULL;
Expand Down Expand Up @@ -340,6 +341,7 @@ static void zend_weakmap_write_dimension(zend_object *object, zval *offset, zval
return;
}

ZVAL_DEREF(offset);
if (Z_TYPE_P(offset) != IS_OBJECT) {
zend_type_error("WeakMap key must be an object");
return;
Expand Down Expand Up @@ -367,6 +369,7 @@ static void zend_weakmap_write_dimension(zend_object *object, zval *offset, zval
/* int return and check_empty due to Object Handler API */
static int zend_weakmap_has_dimension(zend_object *object, zval *offset, int check_empty)
{
ZVAL_DEREF(offset);
if (Z_TYPE_P(offset) != IS_OBJECT) {
zend_type_error("WeakMap key must be an object");
return 0;
Expand All @@ -386,6 +389,7 @@ static int zend_weakmap_has_dimension(zend_object *object, zval *offset, int che

static void zend_weakmap_unset_dimension(zend_object *object, zval *offset)
{
ZVAL_DEREF(offset);
if (Z_TYPE_P(offset) != IS_OBJECT) {
zend_type_error("WeakMap key must be an object");
return;
Expand Down