Skip to content

Commit a9e6667

Browse files
committed
Detect invalid uses of parent:: during compilation
We already detect the case where we're entirely outside a class -- now also check whether there actually is a parent. This is a minor BC break, in that code that was never executed might have previously contained an invalid parent:: reference without generating an error.
1 parent 447b347 commit a9e6667

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ PHP 7.4 UPGRADE NOTES
2020
1. Backward Incompatible Changes
2121
========================================
2222

23+
- Core:
24+
. Referencing parent:: inside a class that does not have a parent will now
25+
generate a compile-time error. Previously the error was only emitted at
26+
run-time.
27+
2328
- Curl:
2429
. Attempting to serialize a CURLFile class will now generate an exception.
2530
Previously the exception was only thrown on unserialization.

Zend/tests/bug75573.phpt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ Bug #75573 (Segmentation fault in 7.1.12 and 7.0.26)
66
class A
77
{
88
var $_stdObject;
9-
function initialize($properties = FALSE) {
10-
$this->_stdObject = $properties ? (object) $properties : new stdClass();
11-
parent::initialize();
12-
}
139
function &__get($property)
1410
{
1511
if (isset($this->_stdObject->{$property})) {
@@ -31,10 +27,6 @@ class A
3127

3228
class B extends A
3329
{
34-
function initialize($properties = array())
35-
{
36-
parent::initialize($properties);
37-
}
3830
function &__get($property)
3931
{
4032
if (isset($this->settings) && isset($this->settings[$property])) {

Zend/tests/class_name_as_scalar_error_002.phpt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,4 @@ namespace Foo\Bar {
1111
}
1212
?>
1313
--EXPECTF--
14-
Fatal error: Uncaught Error: Cannot use "parent" when current class scope has no parent in %s:%d
15-
Stack trace:
16-
#0 {main}
17-
thrown in %s on line %d
14+
Fatal error: Cannot use "parent" when current class scope has no parent in %s on line %d

Zend/zend_compile.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,10 +1358,16 @@ static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
13581358

13591359
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
13601360
{
1361-
if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && !CG(active_class_entry) && zend_is_scope_known()) {
1362-
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1363-
fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1364-
fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1361+
if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1362+
zend_class_entry *ce = CG(active_class_entry);
1363+
if (!ce) {
1364+
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1365+
fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1366+
fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1367+
} else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1368+
zend_error_noreturn(E_COMPILE_ERROR,
1369+
"Cannot use \"parent\" when current class scope has no parent");
1370+
}
13651371
}
13661372
}
13671373
/* }}} */

0 commit comments

Comments
 (0)