Skip to content

Commit 9b87b73

Browse files
committed
Req #74963 (Improved error message on fetching property of non-object)
1 parent c62e5eb commit 9b87b73

21 files changed

+932
-628
lines changed

Zend/tests/024.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ NULL
3737
Notice: Undefined variable: b in %s on line %d
3838
int(1)
3939

40-
Notice: Trying to get property of non-object in %s on line %d
40+
Notice: Trying to get property '1' of non-object in %s on line %d
4141
NULL
4242

43-
Notice: Trying to get property of non-object in %s on line %d
43+
Notice: Trying to get property '1' of non-object in %s on line %d
4444
NULL
4545

4646
Notice: Undefined variable: c in %s on line %d
4747

48-
Notice: Trying to get property of non-object in %s on line %d
48+
Notice: Trying to get property '1' of non-object in %s on line %d
4949

50-
Notice: Trying to get property of non-object in %s on line %d
50+
Notice: Trying to get property '' of non-object in %s on line %d
5151
NULL

Zend/tests/026.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ print "ok\n";
1818

1919
?>
2020
--EXPECTF--
21-
Notice: Trying to get property of non-object in %s on line %d
21+
Notice: Trying to get property 'a' of non-object in %s on line %d
2222
ok
2323

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

Zend/tests/033.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Notice: Undefined variable: arr in %s on line %d
2424

2525
Notice: Undefined variable: arr in %s on line %d
2626

27-
Notice: Trying to get property of non-object in %s on line %d
27+
Notice: Trying to get property 'foo' of non-object in %s on line %d
2828

2929
Warning: Creating default object from empty value in %s on line %d
3030

Zend/tests/assign_dim_obj_null_return.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ NULL
4949
Warning: Cannot use a scalar value as an array in %s on line %d
5050
NULL
5151

52-
Warning: Attempt to assign property of non-object in %s on line %d
52+
Warning: Attempt to assign property 'foo' of non-object in %s on line %d
5353
NULL
5454

55-
Warning: Attempt to assign property of non-object in %s on line %d
55+
Warning: Attempt to assign property 'foo' of non-object in %s on line %d
5656
NULL

Zend/tests/bug31098.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ ok
4444
ok
4545
ok
4646

47-
Notice: Trying to get property of non-object in %s on line %d
47+
Notice: Trying to get property 'wrong' of non-object in %s on line %d
4848
ok
4949

5050
Warning: Illegal string offset 'wrong' in %s on line %d
5151
ok
5252
ok
5353

5454
Warning: Illegal string offset 'wrong' in %s on line %d
55-
ok
55+
ok

Zend/tests/bug36303.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ foreach($x->a->b as &$v) {
88
echo "ok\n";
99
?>
1010
--EXPECTF--
11-
Warning: Attempt to modify property of non-object in %sbug36303.php on line 3
11+
Warning: Attempt to modify property 'a' of non-object in %sbug36303.php on line 3
1212

1313
Warning: Invalid argument supplied for foreach() in %sbug36303.php on line 3
1414
ok

Zend/tests/bug44660.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ var_dump($a);
2828
?>
2929
--EXPECTF--
3030
--> read access:
31-
Notice: Trying to get property of non-object in %sbug44660.php on line 6
31+
Notice: Trying to get property 'p' of non-object in %sbug44660.php on line 6
3232

3333
--> direct assignment:
34-
Warning: Attempt to assign property of non-object in %sbug44660.php on line 9
34+
Warning: Attempt to assign property 'p' of non-object in %sbug44660.php on line 9
3535

3636
--> increment:
37-
Warning: Attempt to increment/decrement property of non-object in %sbug44660.php on line 12
37+
Warning: Attempt to increment/decrement property 'p' of non-object in %sbug44660.php on line 12
3838

3939
--> reference assignment:
40-
Warning: Attempt to modify property of non-object in %sbug44660.php on line 15
40+
Warning: Attempt to modify property 'p' of non-object in %sbug44660.php on line 15
4141

4242
--> reference assignment:
43-
Warning: Attempt to modify property of non-object in %sbug44660.php on line 18
43+
Warning: Attempt to modify property 'p' of non-object in %sbug44660.php on line 18
4444

4545
--> indexed assignment:
46-
Warning: Attempt to modify property of non-object in %sbug44660.php on line 21
46+
Warning: Attempt to modify property 'p' of non-object in %sbug44660.php on line 21
4747

4848
--> Confirm assignments have had no impact:
4949
bool(true)

Zend/tests/bug47109.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ $a->{"a"."b"};
77
--EXPECTF--
88
Notice: Undefined variable: a in %sbug47109.php on line 2
99

10-
Notice: Trying to get property of non-object in %sbug47109.php on line 2
10+
Notice: Trying to get property 'ab' of non-object in %sbug47109.php on line 2
1111

Zend/tests/bug52237.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ var_dump($data);
88
?>
99
--EXPECTF--
1010

11-
Warning: Attempt to modify property of non-object in %sbug52237.php on line 3
11+
Warning: Attempt to modify property 'info' of non-object in %sbug52237.php on line 3
1212
string(4) "test"

Zend/tests/bug54262.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ echo "ok\n";
1111
--EXPECTF--
1212
bool(false)
1313

14-
Warning: Attempt to modify property of non-object in %sbug54262.php on line 4
14+
Warning: Attempt to modify property 'a' of non-object in %sbug54262.php on line 4
1515

1616
Warning: Cannot use a scalar value as an array in %sbug54262.php on line 5
1717
ok

Zend/tests/bug72911.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var_dump($b);
1111

1212
?>
1313
--EXPECTF--
14-
Warning: Attempt to modify property of non-object in %sbug72911.php on line %d
14+
Warning: Attempt to modify property 'b' of non-object in %sbug72911.php on line %d
1515

16-
Warning: Attempt to assign property of non-object in %sbug72911.php on line %d
16+
Warning: Attempt to assign property 'i' of non-object in %sbug72911.php on line %d
1717
NULL

Zend/tests/dereference_014.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ var_dump($h);
2727

2828
?>
2929
--EXPECTF--
30-
Notice: Trying to get property of non-object in %s on line %d
30+
Notice: Trying to get property 'a' of non-object in %s on line %d
3131
NULL
3232

33-
Notice: Trying to get property of non-object in %s on line %d
33+
Notice: Trying to get property 'b' of non-object in %s on line %d
3434
NULL

Zend/tests/isset_003.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Notice: Undefined variable: c in %s on line %d
3333

3434
Notice: Undefined variable: d in %s on line %d
3535

36-
Notice: Trying to get property of non-object in %s on line %d
36+
Notice: Trying to get property '' of non-object in %s on line %d
3737
bool(false)
3838
bool(true)
3939
bool(false)

Zend/tests/varSyntax/propertyOfStringError.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ Cannot take property of a string
77

88
?>
99
--EXPECTF--
10-
Notice: Trying to get property of non-object in %s on line %d
10+
Notice: Trying to get property 'bar' of non-object in %s on line %d

Zend/zend_execute.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,9 @@ static zend_always_inline void zend_fetch_property_address(zval *result, zval *c
18861886
object_init(container);
18871887
} else {
18881888
if (container_op_type != IS_VAR || EXPECTED(!Z_ISERROR_P(container))) {
1889-
zend_error(E_WARNING, "Attempt to modify property of non-object");
1889+
zend_string *property_name = zval_get_string(prop_ptr);
1890+
zend_error(E_WARNING, "Attempt to modify property '%s' of non-object", ZSTR_VAL(property_name));
1891+
zend_string_release(property_name);
18901892
}
18911893
ZVAL_ERROR(result);
18921894
return;

Zend/zend_vm_def.h

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,9 @@ ZEND_VM_HELPER(zend_binary_assign_op_obj_helper, VAR|UNUSED|CV, CONST|TMPVAR|CV,
811811
if (OP1_TYPE != IS_UNUSED && UNEXPECTED(Z_TYPE_P(object) != IS_OBJECT)) {
812812
ZVAL_DEREF(object);
813813
if (UNEXPECTED(!make_real_object(object))) {
814-
zend_error(E_WARNING, "Attempt to assign property of non-object");
814+
zend_string *property_name = zval_get_string(property);
815+
zend_error(E_WARNING, "Attempt to assign property '%s' of non-object", ZSTR_VAL(property_name));
816+
zend_string_release(property_name);
815817
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
816818
ZVAL_NULL(EX_VAR(opline->result.var));
817819
}
@@ -1070,7 +1072,9 @@ ZEND_VM_HELPER(zend_pre_incdec_property_helper, VAR|UNUSED|CV, CONST|TMPVAR|CV,
10701072
if (OP1_TYPE != IS_UNUSED && UNEXPECTED(Z_TYPE_P(object) != IS_OBJECT)) {
10711073
ZVAL_DEREF(object);
10721074
if (UNEXPECTED(!make_real_object(object))) {
1073-
zend_error(E_WARNING, "Attempt to increment/decrement property of non-object");
1075+
zend_string *property_name = zval_get_string(property);
1076+
zend_error(E_WARNING, "Attempt to increment/decrement property '%s' of non-object", ZSTR_VAL(property_name));
1077+
zend_string_release(property_name);
10741078
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
10751079
ZVAL_NULL(EX_VAR(opline->result.var));
10761080
}
@@ -1147,7 +1151,9 @@ ZEND_VM_HELPER(zend_post_incdec_property_helper, VAR|UNUSED|CV, CONST|TMPVAR|CV,
11471151
if (OP1_TYPE != IS_UNUSED && UNEXPECTED(Z_TYPE_P(object) != IS_OBJECT)) {
11481152
ZVAL_DEREF(object);
11491153
if (UNEXPECTED(!make_real_object(object))) {
1150-
zend_error(E_WARNING, "Attempt to increment/decrement property of non-object");
1154+
zend_string *property_name = zval_get_string(property);
1155+
zend_error(E_WARNING, "Attempt to increment/decrement property '%s' of non-object", ZSTR_VAL(property_name));
1156+
zend_string_release(property_name);
11511157
ZVAL_NULL(EX_VAR(opline->result.var));
11521158
break;
11531159
}
@@ -1802,8 +1808,11 @@ ZEND_VM_HANDLER(82, ZEND_FETCH_OBJ_R, CONST|TMP|VAR|UNUSED|THIS|CV, CONST|TMPVAR
18021808
}
18031809

18041810
if (UNEXPECTED(zobj->handlers->read_property == NULL)) {
1811+
zend_string *property_name;
18051812
ZEND_VM_C_LABEL(fetch_obj_r_no_object):
1806-
zend_error(E_NOTICE, "Trying to get property of non-object");
1813+
property_name = zval_get_string(offset);
1814+
zend_error(E_NOTICE, "Trying to get property '%s' of non-object", ZSTR_VAL(property_name));
1815+
zend_string_release(property_name);
18071816
ZVAL_NULL(EX_VAR(opline->result.var));
18081817
} else {
18091818
retval = zobj->handlers->read_property(container, offset, BP_VAR_R, ((OP2_TYPE == IS_CONST) ? CACHE_ADDR(Z_CACHE_SLOT_P(offset)) : NULL), EX_VAR(opline->result.var));
@@ -2014,7 +2023,7 @@ ZEND_VM_HANDLER(136, ZEND_ASSIGN_OBJ, VAR|UNUSED|THIS|CV, CONST|TMPVAR|CV, SPEC(
20142023
{
20152024
USE_OPLINE
20162025
zend_free_op free_op1, free_op2, free_op_data;
2017-
zval *object, *property_name, *value, tmp;
2026+
zval *object, *property, *value, tmp;
20182027

20192028
SAVE_OPLINE();
20202029
object = GET_OP1_OBJ_ZVAL_PTR_PTR_UNDEF(BP_VAR_W);
@@ -2023,7 +2032,7 @@ ZEND_VM_HANDLER(136, ZEND_ASSIGN_OBJ, VAR|UNUSED|THIS|CV, CONST|TMPVAR|CV, SPEC(
20232032
ZEND_VM_DISPATCH_TO_HELPER(zend_this_not_in_object_context_helper);
20242033
}
20252034

2026-
property_name = GET_OP2_ZVAL_PTR(BP_VAR_R);
2035+
property = GET_OP2_ZVAL_PTR(BP_VAR_R);
20272036
value = GET_OP_DATA_ZVAL_PTR(BP_VAR_R);
20282037

20292038
if (OP1_TYPE != IS_UNUSED && UNEXPECTED(Z_TYPE_P(object) != IS_OBJECT)) {
@@ -2055,7 +2064,9 @@ ZEND_VM_HANDLER(136, ZEND_ASSIGN_OBJ, VAR|UNUSED|THIS|CV, CONST|TMPVAR|CV, SPEC(
20552064
Z_DELREF_P(object);
20562065
} else {
20572066
if (OP1_TYPE != IS_VAR || EXPECTED(!Z_ISERROR_P(object))) {
2058-
zend_error(E_WARNING, "Attempt to assign property of non-object");
2067+
zend_string *property_name = zval_get_string(property);
2068+
zend_error(E_WARNING, "Attempt to assign property '%s' of non-object", ZSTR_VAL(property_name));
2069+
zend_string_release(property_name);
20592070
}
20602071
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
20612072
ZVAL_NULL(EX_VAR(opline->result.var));
@@ -2067,16 +2078,16 @@ ZEND_VM_HANDLER(136, ZEND_ASSIGN_OBJ, VAR|UNUSED|THIS|CV, CONST|TMPVAR|CV, SPEC(
20672078
}
20682079

20692080
if (OP2_TYPE == IS_CONST &&
2070-
EXPECTED(Z_OBJCE_P(object) == CACHED_PTR(Z_CACHE_SLOT_P(property_name)))) {
2071-
uint32_t prop_offset = (uint32_t)(intptr_t)CACHED_PTR(Z_CACHE_SLOT_P(property_name) + sizeof(void*));
2081+
EXPECTED(Z_OBJCE_P(object) == CACHED_PTR(Z_CACHE_SLOT_P(property)))) {
2082+
uint32_t prop_offset = (uint32_t)(intptr_t)CACHED_PTR(Z_CACHE_SLOT_P(property) + sizeof(void*));
20722083
zend_object *zobj = Z_OBJ_P(object);
2073-
zval *property;
2084+
zval *property_val;
20742085

20752086
if (EXPECTED(prop_offset != (uint32_t)ZEND_DYNAMIC_PROPERTY_OFFSET)) {
2076-
property = OBJ_PROP(zobj, prop_offset);
2077-
if (Z_TYPE_P(property) != IS_UNDEF) {
2087+
property_val = OBJ_PROP(zobj, prop_offset);
2088+
if (Z_TYPE_P(property_val) != IS_UNDEF) {
20782089
ZEND_VM_C_LABEL(fast_assign_obj):
2079-
value = zend_assign_to_variable(property, value, OP_DATA_TYPE);
2090+
value = zend_assign_to_variable(property_val, value, OP_DATA_TYPE);
20802091
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
20812092
ZVAL_COPY(EX_VAR(opline->result.var), value);
20822093
}
@@ -2090,8 +2101,8 @@ ZEND_VM_C_LABEL(fast_assign_obj):
20902101
}
20912102
zobj->properties = zend_array_dup(zobj->properties);
20922103
}
2093-
property = zend_hash_find(zobj->properties, Z_STR_P(property_name));
2094-
if (property) {
2104+
property_val = zend_hash_find(zobj->properties, Z_STR_P(property));
2105+
if (property_val) {
20952106
ZEND_VM_C_GOTO(fast_assign_obj);
20962107
}
20972108
}
@@ -2129,7 +2140,7 @@ ZEND_VM_C_LABEL(fast_assign_obj):
21292140
Z_ADDREF_P(value);
21302141
}
21312142
}
2132-
zend_hash_add_new(zobj->properties, Z_STR_P(property_name), value);
2143+
zend_hash_add_new(zobj->properties, Z_STR_P(property), value);
21332144
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
21342145
ZVAL_COPY(EX_VAR(opline->result.var), value);
21352146
}
@@ -2139,7 +2150,9 @@ ZEND_VM_C_LABEL(fast_assign_obj):
21392150
}
21402151

21412152
if (!Z_OBJ_HT_P(object)->write_property) {
2142-
zend_error(E_WARNING, "Attempt to assign property of non-object");
2153+
zend_string *property_name = zval_get_string(property);
2154+
zend_error(E_WARNING, "Attempt to assign property '%s' of non-object", ZSTR_VAL(property_name));
2155+
zend_string_release(property_name);
21432156
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
21442157
ZVAL_NULL(EX_VAR(opline->result.var));
21452158
}
@@ -2151,7 +2164,7 @@ ZEND_VM_C_LABEL(fast_assign_obj):
21512164
ZVAL_DEREF(value);
21522165
}
21532166

2154-
Z_OBJ_HT_P(object)->write_property(object, property_name, value, (OP2_TYPE == IS_CONST) ? CACHE_ADDR(Z_CACHE_SLOT_P(property_name)) : NULL);
2167+
Z_OBJ_HT_P(object)->write_property(object, property, value, (OP2_TYPE == IS_CONST) ? CACHE_ADDR(Z_CACHE_SLOT_P(property)) : NULL);
21552168

21562169
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
21572170
ZVAL_COPY(EX_VAR(opline->result.var), value);
@@ -5597,7 +5610,9 @@ ZEND_VM_HANDLER(76, ZEND_UNSET_OBJ, VAR|UNUSED|THIS|CV, CONST|TMPVAR|CV)
55975610
if (Z_OBJ_HT_P(container)->unset_property) {
55985611
Z_OBJ_HT_P(container)->unset_property(container, offset, ((OP2_TYPE == IS_CONST) ? CACHE_ADDR(Z_CACHE_SLOT_P(offset)) : NULL));
55995612
} else {
5600-
zend_error(E_NOTICE, "Trying to unset property of non-object");
5613+
zend_string *property_name = zval_get_string(offset);
5614+
zend_error(E_NOTICE, "Trying to unset property '%s' of non-object", ZSTR_VAL(property_name));
5615+
zend_string_release(property_name);
56015616
}
56025617
} while (0);
56035618

@@ -6489,7 +6504,9 @@ ZEND_VM_HANDLER(148, ZEND_ISSET_ISEMPTY_PROP_OBJ, CONST|TMPVAR|UNUSED|THIS|CV, C
64896504
}
64906505
}
64916506
if (UNEXPECTED(!Z_OBJ_HT_P(container)->has_property)) {
6492-
zend_error(E_NOTICE, "Trying to check property of non-object");
6507+
zend_string *property_name = zval_get_string(offset);
6508+
zend_error(E_NOTICE, "Trying to check property '%s' of non-object", ZSTR_VAL(property_name));
6509+
zend_string_release(property_name);
64936510
ZEND_VM_C_LABEL(isset_no_object):
64946511
result = ((opline->extended_value & ZEND_ISSET) == 0);
64956512
} else {

0 commit comments

Comments
 (0)