Skip to content

Commit

Permalink
Don't treat failed assignment as initialization
Browse files Browse the repository at this point in the history
Only reset the uninitialized property flag once the type check
has succeeded. Previously the property was treated as unset rather
than uninitialized after a failed assignment.

Noticed this edge-case while working on accessors...
  • Loading branch information
nikic committed May 4, 2021
1 parent 5ce0fa2 commit 398cfb9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
A failed assignment should not be considered an initialization
--FILE--
<?php

class Test {
public int $prop;

public function __get($name) {
echo "__get() called\n";
return 0;
}
}

$test = new Test;
try {
$test->prop;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
$test->prop = "foo";
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
$test->prop;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
Typed property Test::$prop must not be accessed before initialization
Cannot assign string to property Test::$prop of type int
Typed property Test::$prop must not be accessed before initialization
2 changes: 1 addition & 1 deletion Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,6 @@ ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zva
}
if (Z_PROP_FLAG_P(variable_ptr) == IS_PROP_UNINIT) {
/* Writes to uninitializde typed properties bypass __set(). */
Z_PROP_FLAG_P(variable_ptr) = 0;
goto write_std_property;
}
} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
Expand Down Expand Up @@ -783,6 +782,7 @@ ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zva
goto exit;
}
value = &tmp;
Z_PROP_FLAG_P(variable_ptr) = 0;
goto found; /* might have been updated via e.g. __toString() */
}

Expand Down

0 comments on commit 398cfb9

Please sign in to comment.