Skip to content

Commit e63febb

Browse files
bp1222nikic
authored andcommitted
Fixed bug #75921
Ensure that the "creating default object from empty value" warning is always thrown. Previously some cases were missing the warning, in particular those going through FETCH_OBJ_W rather than a dedicated opcode (like ASSIGN_OBJ). One slightly unfortunate side-effect of this change is that something like $a->b->c = 'd' will now generate two warnings rather than one when $a is null (one for property b, one for property c).
1 parent dab5462 commit e63febb

File tree

11 files changed

+168
-38
lines changed

11 files changed

+168
-38
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PHP NEWS
77
specially compiled functions). (Majkl578)
88
. Fixed bug #76430 (__METHOD__ inconsistent outside of method).
99
(Ryan McCullagh, Nikita)
10+
. Fixed bug #75921 (Inconsistent: No warning in some cases when stdObj is
11+
created on the fly). (David Walker)
1012

1113
- CURL:
1214
. Fixed bug #76480 (Use curl_multi_wait() so that timeouts are respected).

Zend/tests/bug52041.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Notice: Undefined variable: x in %sbug52041.php on line 3
3131

3232
Warning: Creating default object from empty value in %sbug52041.php on line 7
3333

34+
Warning: Creating default object from empty value in %sbug52041.php on line 7
35+
3436
Notice: Undefined variable: x in %sbug52041.php on line 3
3537

3638
Warning: Creating default object from empty value in %sbug52041.php on line 8
@@ -39,6 +41,8 @@ Notice: Undefined property: stdClass::$a in %sbug52041.php on line 8
3941

4042
Notice: Undefined variable: x in %sbug52041.php on line 3
4143

44+
Warning: Creating default object from empty value in %sbug52041.php on line 9
45+
4246
Notice: Undefined property: stdClass::$a in %sbug52041.php on line 9
4347

4448
Warning: Creating default object from empty value in %sbug52041.php on line 9
@@ -53,6 +57,8 @@ Notice: Undefined property: stdClass::$a in %sbug52041.php on line 10
5357

5458
Notice: Undefined variable: x in %sbug52041.php on line 3
5559

60+
Warning: Creating default object from empty value in %sbug52041.php on line 11
61+
5662
Notice: Undefined property: stdClass::$a in %sbug52041.php on line 11
5763

5864
Warning: Creating default object from empty value in %sbug52041.php on line 11

Zend/tests/bug71539_5.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ $array['']->prop =& $array[0];
77
$array[0] = 42;
88
var_dump($array);
99
?>
10-
--EXPECT--
10+
--EXPECTF--
11+
Warning: Creating default object from empty value in %sbug71539_5.php on line 3
1112
array(2) {
1213
[0]=>
1314
&int(42)

Zend/tests/bug75921.phpt

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
--TEST--
2+
Bug #75921: Inconsistent error when creating stdObject from empty variable
3+
--FILE--
4+
<?php
5+
6+
$null->a = 42;
7+
var_dump($null);
8+
unset($null);
9+
10+
$null->a['hello'] = 42;
11+
var_dump($null);
12+
unset($null);
13+
14+
$null->a->b = 42;
15+
var_dump($null);
16+
unset($null);
17+
18+
$null->a['hello']->b = 42;
19+
var_dump($null);
20+
unset($null);
21+
22+
$null->a->b['hello'] = 42;
23+
var_dump($null);
24+
unset($null);
25+
26+
?>
27+
--EXPECTF--
28+
Warning: Creating default object from empty value in %sbug75921.php on line 3
29+
object(stdClass)#1 (1) {
30+
["a"]=>
31+
int(42)
32+
}
33+
34+
Warning: Creating default object from empty value in %sbug75921.php on line 7
35+
object(stdClass)#1 (1) {
36+
["a"]=>
37+
array(1) {
38+
["hello"]=>
39+
int(42)
40+
}
41+
}
42+
43+
Warning: Creating default object from empty value in %sbug75921.php on line 11
44+
45+
Warning: Creating default object from empty value in %sbug75921.php on line 11
46+
object(stdClass)#1 (1) {
47+
["a"]=>
48+
object(stdClass)#2 (1) {
49+
["b"]=>
50+
int(42)
51+
}
52+
}
53+
54+
Warning: Creating default object from empty value in %sbug75921.php on line 15
55+
56+
Warning: Creating default object from empty value in %sbug75921.php on line 15
57+
object(stdClass)#1 (1) {
58+
["a"]=>
59+
array(1) {
60+
["hello"]=>
61+
object(stdClass)#2 (1) {
62+
["b"]=>
63+
int(42)
64+
}
65+
}
66+
}
67+
68+
Warning: Creating default object from empty value in %sbug75921.php on line 19
69+
70+
Warning: Creating default object from empty value in %sbug75921.php on line 19
71+
object(stdClass)#1 (1) {
72+
["a"]=>
73+
object(stdClass)#2 (1) {
74+
["b"]=>
75+
array(1) {
76+
["hello"]=>
77+
int(42)
78+
}
79+
}
80+
}

Zend/tests/objects_020.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var_dump($$test);
1414

1515
?>
1616
--EXPECTF--
17+
Warning: Creating default object from empty value in %sobjects_020.php on line 7
1718
object(stdClass)#%d (2) {
1819
["a"]=>
1920
*RECURSION*

Zend/tests/type_declarations/typed_properties_091.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ Cannot auto-initialize an stdClass inside a reference held by property Test::$pr
134134

135135
Warning: Creating default object from empty value in %s on line %d
136136

137+
Warning: Creating default object from empty value in %s on line %d
138+
139+
Warning: Creating default object from empty value in %s on line %d
140+
137141
Warning: Creating default object from empty value in %s on line %d
138142
object(Test)#3 (3) {
139143
["prop"]=>

Zend/zend_execute.c

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,10 @@ static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL make_real_object(zval *ob
709709
|| opline->opcode == ZEND_POST_INC_OBJ
710710
|| opline->opcode == ZEND_POST_DEC_OBJ) {
711711
zend_error(E_WARNING, "Attempt to increment/decrement property '%s' of non-object", ZSTR_VAL(property_name));
712+
} else if (opline->opcode == ZEND_FETCH_OBJ_W
713+
|| opline->opcode == ZEND_FETCH_OBJ_RW
714+
|| opline->opcode == ZEND_ASSIGN_OBJ_REF) {
715+
zend_error(E_WARNING, "Attempt to modify property '%s' of non-object", ZSTR_VAL(property_name));
712716
} else {
713717
zend_error(E_WARNING, "Attempt to assign property '%s' of non-object", ZSTR_VAL(property_name));
714718
}
@@ -748,38 +752,6 @@ static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL make_real_object(zval *ob
748752
return object;
749753
}
750754

751-
static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL make_real_object_rw(zval *object, zval *property OPLINE_DC)
752-
{
753-
zval *ref = NULL;
754-
if (Z_ISREF_P(object)) {
755-
ref = object;
756-
object = Z_REFVAL_P(object);
757-
}
758-
759-
if (UNEXPECTED(Z_TYPE_P(object) > IS_FALSE &&
760-
(Z_TYPE_P(object) != IS_STRING || Z_STRLEN_P(object) != 0))) {
761-
if (opline->op1_type != IS_VAR || EXPECTED(!Z_ISERROR_P(object))) {
762-
zend_string *tmp_property_name;
763-
zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name);
764-
zend_error(E_WARNING, "Attempt to modify property '%s' of non-object", ZSTR_VAL(property_name));
765-
zend_tmp_string_release(tmp_property_name);
766-
}
767-
return NULL;
768-
}
769-
770-
if (ref) {
771-
zend_property_info *error_prop = i_zend_check_ref_stdClass_assignable(Z_REF_P(ref));
772-
if (error_prop) {
773-
zend_throw_auto_init_in_ref_error(error_prop, "stdClass");
774-
return NULL;
775-
}
776-
}
777-
778-
zval_ptr_dtor_nogc(object);
779-
object_init(object);
780-
return object;
781-
}
782-
783755
static ZEND_COLD void zend_verify_type_error_common(
784756
const zend_function *zf, const zend_arg_info *arg_info,
785757
const zend_class_entry *ce, zval *value,
@@ -2736,7 +2708,7 @@ static zend_always_inline void zend_fetch_property_address(zval *result, zval *c
27362708
return;
27372709
}
27382710

2739-
container = make_real_object_rw(container, prop_ptr OPLINE_CC);
2711+
container = make_real_object(container, prop_ptr OPLINE_CC);
27402712
if (UNEXPECTED(!container)) {
27412713
ZVAL_ERROR(result);
27422714
return;

ext/simplexml/tests/bug36611.phpt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ $xml_str = <<<EOD
1414
</c_fpobel>
1515
EOD;
1616

17-
$xml = simplexml_load_string ($xml_str) ;
17+
$xml = simplexml_load_string($xml_str);
1818

1919
$val = 1;
2020

2121
var_dump($val);
2222
$zml->pos["act_idx"] = $val;
23-
var_dump($val) ;
23+
var_dump($val);
2424

2525
?>
2626
===DONE===
27-
--EXPECT--
27+
--EXPECTF--
2828
int(1)
29+
30+
Warning: Creating default object from empty value in %sbug36611.php on line 17
2931
int(1)
3032
===DONE===

tests/lang/engine_assignExecutionOrder_008.phpt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,15 @@ Warning: Creating default object from empty value in %s on line %d
7070
good
7171
$i->p->q=f():
7272
Warning: Creating default object from empty value in %s on line %d
73+
74+
Warning: Creating default object from empty value in %s on line %d
75+
good
76+
$i->p[0]=f():
77+
Warning: Creating default object from empty value in %s on line %d
7378
good
74-
$i->p[0]=f(): good
7579
$i->p[0]->p=f():
80+
Warning: Creating default object from empty value in %s on line %d
81+
7682
Warning: Creating default object from empty value in %s on line %d
7783
good
7884
C::$p=f(): good

tests/lang/foreachLoop.016.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,26 +157,34 @@ array(1) {
157157

158158
$a->b->c
159159

160+
Warning: Creating default object from empty value in %s on line %d
161+
160162
Warning: Creating default object from empty value in %s on line %d
161163
array(1) {
162164
[0]=>
163165
string(8) "original"
164166
}
165167

166168
$a->b[0]
169+
170+
Warning: Creating default object from empty value in %s on line %d
167171
array(1) {
168172
[0]=>
169173
string(8) "original"
170174
}
171175

172176
$a->b[0][0]
177+
178+
Warning: Creating default object from empty value in %s on line %d
173179
array(1) {
174180
[0]=>
175181
string(8) "original"
176182
}
177183

178184
$a->b[0]->c
179185

186+
Warning: Creating default object from empty value in %s on line %d
187+
180188
Warning: Creating default object from empty value in %s on line %d
181189
array(1) {
182190
[0]=>

0 commit comments

Comments
 (0)