1
0
mirror of https://github.com/php/php-src.git synced 2026-04-22 15:38:49 +02:00
Files
archived-php-src/Zend/tests/weakrefs/weakmap_error_conditions.phpt
T
Nikita Popov 653e4ea1c5 Add flag to forbid dynamic property creation on internal classes
While performing resource -> object migrations, we're adding
defensive classes that are final, non-serializable and non-clonable
(unless they are, of course). This path adds a ZEND_ACC_NO_DYNAMIC_PROPERTIES
flag, that also forbids the creation of dynamic properties on these objects.
This is a subset of #3931 and targeted at internal usage only
(though may be extended to userland at some point in the future).

It's already possible to achieve this (what the removed
WeakRef/WeakMap code does), but there's some caveats: First, this
simple approach is only possible if the class has no declared
properties, otherwise it's necessary to special-case those
properties. Second, it's easy to make it overly strict, e.g. by
forbidding isset($obj->prop) as well. And finally, it requires a
lot of boilerplate code for each class.

Closes GH-5572.
2020-06-24 11:52:36 +02:00

93 lines
1.7 KiB
PHP

--TEST--
WeakMap error conditions
--FILE--
<?php
$map = new WeakMap;
try {
$map[1] = 2;
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump($map[1]);
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
try {
isset($map[1]);
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
try {
unset($map[1]);
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
try {
$map[] = 1;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
$map[][1] = 1;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump($map[new stdClass]);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump($map->prop);
var_dump(isset($map->prop));
unset($map->prop);
try {
$map->prop = 1;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
$map->prop[] = 1;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
$r =& $map->prop;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
serialize($map);
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
try {
unserialize('C:7:"WeakMap":0:{}');
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECTF--
WeakMap key must be an object
WeakMap key must be an object
WeakMap key must be an object
WeakMap key must be an object
Cannot append to WeakMap
Cannot append to WeakMap
Object stdClass#2 not contained in WeakMap
Warning: Undefined property: WeakMap::$prop in %s on line %d
NULL
bool(false)
Cannot create dynamic property WeakMap::$prop
Cannot create dynamic property WeakMap::$prop
Cannot create dynamic property WeakMap::$prop
Serialization of 'WeakMap' is not allowed
Unserialization of 'WeakMap' is not allowed