Skip to content

Commit

Permalink
Req #74963 (Improved error message on fetching property of non-object)
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed Jul 31, 2017
1 parent c62e5eb commit 9b87b73
Show file tree
Hide file tree
Showing 21 changed files with 932 additions and 628 deletions.
8 changes: 4 additions & 4 deletions Zend/tests/024.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ NULL
Notice: Undefined variable: b in %s on line %d
int(1)

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

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

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

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

Notice: Trying to get property of non-object in %s on line %d
Notice: Trying to get property '' of non-object in %s on line %d
NULL
2 changes: 1 addition & 1 deletion Zend/tests/026.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ print "ok\n";

?>
--EXPECTF--
Notice: Trying to get property of non-object in %s on line %d
Notice: Trying to get property 'a' of non-object in %s on line %d
ok

Warning: Creating default object from empty value in %s on line %d
Expand Down
2 changes: 1 addition & 1 deletion Zend/tests/033.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Notice: Undefined variable: arr in %s on line %d

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions Zend/tests/assign_dim_obj_null_return.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ NULL
Warning: Cannot use a scalar value as an array in %s on line %d
NULL

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

Warning: Attempt to assign property of non-object in %s on line %d
Warning: Attempt to assign property 'foo' of non-object in %s on line %d
NULL
4 changes: 2 additions & 2 deletions Zend/tests/bug31098.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ ok
ok
ok

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

Warning: Illegal string offset 'wrong' in %s on line %d
ok
ok

Warning: Illegal string offset 'wrong' in %s on line %d
ok
ok
2 changes: 1 addition & 1 deletion Zend/tests/bug36303.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ foreach($x->a->b as &$v) {
echo "ok\n";
?>
--EXPECTF--
Warning: Attempt to modify property of non-object in %sbug36303.php on line 3
Warning: Attempt to modify property 'a' of non-object in %sbug36303.php on line 3

Warning: Invalid argument supplied for foreach() in %sbug36303.php on line 3
ok
12 changes: 6 additions & 6 deletions Zend/tests/bug44660.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ var_dump($a);
?>
--EXPECTF--
--> read access:
Notice: Trying to get property of non-object in %sbug44660.php on line 6
Notice: Trying to get property 'p' of non-object in %sbug44660.php on line 6

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

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

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

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

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

--> Confirm assignments have had no impact:
bool(true)
2 changes: 1 addition & 1 deletion Zend/tests/bug47109.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ $a->{"a"."b"};
--EXPECTF--
Notice: Undefined variable: a in %sbug47109.php on line 2

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

2 changes: 1 addition & 1 deletion Zend/tests/bug52237.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ var_dump($data);
?>
--EXPECTF--

Warning: Attempt to modify property of non-object in %sbug52237.php on line 3
Warning: Attempt to modify property 'info' of non-object in %sbug52237.php on line 3
string(4) "test"
2 changes: 1 addition & 1 deletion Zend/tests/bug54262.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ echo "ok\n";
--EXPECTF--
bool(false)

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

Warning: Cannot use a scalar value as an array in %sbug54262.php on line 5
ok
4 changes: 2 additions & 2 deletions Zend/tests/bug72911.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var_dump($b);

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

Warning: Attempt to assign property of non-object in %sbug72911.php on line %d
Warning: Attempt to assign property 'i' of non-object in %sbug72911.php on line %d
NULL
4 changes: 2 additions & 2 deletions Zend/tests/dereference_014.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ var_dump($h);

?>
--EXPECTF--
Notice: Trying to get property of non-object in %s on line %d
Notice: Trying to get property 'a' of non-object in %s on line %d
NULL

Notice: Trying to get property of non-object in %s on line %d
Notice: Trying to get property 'b' of non-object in %s on line %d
NULL
2 changes: 1 addition & 1 deletion Zend/tests/isset_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Notice: Undefined variable: c in %s on line %d

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

Notice: Trying to get property of non-object in %s on line %d
Notice: Trying to get property '' of non-object in %s on line %d
bool(false)
bool(true)
bool(false)
Expand Down
2 changes: 1 addition & 1 deletion Zend/tests/varSyntax/propertyOfStringError.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Cannot take property of a string

?>
--EXPECTF--
Notice: Trying to get property of non-object in %s on line %d
Notice: Trying to get property 'bar' of non-object in %s on line %d
4 changes: 3 additions & 1 deletion Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,9 @@ static zend_always_inline void zend_fetch_property_address(zval *result, zval *c
object_init(container);
} else {
if (container_op_type != IS_VAR || EXPECTED(!Z_ISERROR_P(container))) {
zend_error(E_WARNING, "Attempt to modify property of non-object");
zend_string *property_name = zval_get_string(prop_ptr);
zend_error(E_WARNING, "Attempt to modify property '%s' of non-object", ZSTR_VAL(property_name));
zend_string_release(property_name);
}
ZVAL_ERROR(result);
return;
Expand Down
57 changes: 37 additions & 20 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,9 @@ ZEND_VM_HELPER(zend_binary_assign_op_obj_helper, VAR|UNUSED|CV, CONST|TMPVAR|CV,
if (OP1_TYPE != IS_UNUSED && UNEXPECTED(Z_TYPE_P(object) != IS_OBJECT)) {
ZVAL_DEREF(object);
if (UNEXPECTED(!make_real_object(object))) {
zend_error(E_WARNING, "Attempt to assign property of non-object");
zend_string *property_name = zval_get_string(property);
zend_error(E_WARNING, "Attempt to assign property '%s' of non-object", ZSTR_VAL(property_name));
zend_string_release(property_name);
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
ZVAL_NULL(EX_VAR(opline->result.var));
}
Expand Down Expand Up @@ -1070,7 +1072,9 @@ ZEND_VM_HELPER(zend_pre_incdec_property_helper, VAR|UNUSED|CV, CONST|TMPVAR|CV,
if (OP1_TYPE != IS_UNUSED && UNEXPECTED(Z_TYPE_P(object) != IS_OBJECT)) {
ZVAL_DEREF(object);
if (UNEXPECTED(!make_real_object(object))) {
zend_error(E_WARNING, "Attempt to increment/decrement property of non-object");
zend_string *property_name = zval_get_string(property);
zend_error(E_WARNING, "Attempt to increment/decrement property '%s' of non-object", ZSTR_VAL(property_name));
zend_string_release(property_name);
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
ZVAL_NULL(EX_VAR(opline->result.var));
}
Expand Down Expand Up @@ -1147,7 +1151,9 @@ ZEND_VM_HELPER(zend_post_incdec_property_helper, VAR|UNUSED|CV, CONST|TMPVAR|CV,
if (OP1_TYPE != IS_UNUSED && UNEXPECTED(Z_TYPE_P(object) != IS_OBJECT)) {
ZVAL_DEREF(object);
if (UNEXPECTED(!make_real_object(object))) {
zend_error(E_WARNING, "Attempt to increment/decrement property of non-object");
zend_string *property_name = zval_get_string(property);
zend_error(E_WARNING, "Attempt to increment/decrement property '%s' of non-object", ZSTR_VAL(property_name));
zend_string_release(property_name);
ZVAL_NULL(EX_VAR(opline->result.var));
break;
}
Expand Down Expand Up @@ -1802,8 +1808,11 @@ ZEND_VM_HANDLER(82, ZEND_FETCH_OBJ_R, CONST|TMP|VAR|UNUSED|THIS|CV, CONST|TMPVAR
}

if (UNEXPECTED(zobj->handlers->read_property == NULL)) {
zend_string *property_name;
ZEND_VM_C_LABEL(fetch_obj_r_no_object):
zend_error(E_NOTICE, "Trying to get property of non-object");
property_name = zval_get_string(offset);
zend_error(E_NOTICE, "Trying to get property '%s' of non-object", ZSTR_VAL(property_name));
zend_string_release(property_name);
ZVAL_NULL(EX_VAR(opline->result.var));
} else {
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));
Expand Down Expand Up @@ -2014,7 +2023,7 @@ ZEND_VM_HANDLER(136, ZEND_ASSIGN_OBJ, VAR|UNUSED|THIS|CV, CONST|TMPVAR|CV, SPEC(
{
USE_OPLINE
zend_free_op free_op1, free_op2, free_op_data;
zval *object, *property_name, *value, tmp;
zval *object, *property, *value, tmp;

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

property_name = GET_OP2_ZVAL_PTR(BP_VAR_R);
property = GET_OP2_ZVAL_PTR(BP_VAR_R);
value = GET_OP_DATA_ZVAL_PTR(BP_VAR_R);

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

if (OP2_TYPE == IS_CONST &&
EXPECTED(Z_OBJCE_P(object) == CACHED_PTR(Z_CACHE_SLOT_P(property_name)))) {
uint32_t prop_offset = (uint32_t)(intptr_t)CACHED_PTR(Z_CACHE_SLOT_P(property_name) + sizeof(void*));
EXPECTED(Z_OBJCE_P(object) == CACHED_PTR(Z_CACHE_SLOT_P(property)))) {
uint32_t prop_offset = (uint32_t)(intptr_t)CACHED_PTR(Z_CACHE_SLOT_P(property) + sizeof(void*));
zend_object *zobj = Z_OBJ_P(object);
zval *property;
zval *property_val;

if (EXPECTED(prop_offset != (uint32_t)ZEND_DYNAMIC_PROPERTY_OFFSET)) {
property = OBJ_PROP(zobj, prop_offset);
if (Z_TYPE_P(property) != IS_UNDEF) {
property_val = OBJ_PROP(zobj, prop_offset);
if (Z_TYPE_P(property_val) != IS_UNDEF) {
ZEND_VM_C_LABEL(fast_assign_obj):
value = zend_assign_to_variable(property, value, OP_DATA_TYPE);
value = zend_assign_to_variable(property_val, value, OP_DATA_TYPE);
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
ZVAL_COPY(EX_VAR(opline->result.var), value);
}
Expand All @@ -2090,8 +2101,8 @@ ZEND_VM_C_LABEL(fast_assign_obj):
}
zobj->properties = zend_array_dup(zobj->properties);
}
property = zend_hash_find(zobj->properties, Z_STR_P(property_name));
if (property) {
property_val = zend_hash_find(zobj->properties, Z_STR_P(property));
if (property_val) {
ZEND_VM_C_GOTO(fast_assign_obj);
}
}
Expand Down Expand Up @@ -2129,7 +2140,7 @@ ZEND_VM_C_LABEL(fast_assign_obj):
Z_ADDREF_P(value);
}
}
zend_hash_add_new(zobj->properties, Z_STR_P(property_name), value);
zend_hash_add_new(zobj->properties, Z_STR_P(property), value);
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
ZVAL_COPY(EX_VAR(opline->result.var), value);
}
Expand All @@ -2139,7 +2150,9 @@ ZEND_VM_C_LABEL(fast_assign_obj):
}

if (!Z_OBJ_HT_P(object)->write_property) {
zend_error(E_WARNING, "Attempt to assign property of non-object");
zend_string *property_name = zval_get_string(property);
zend_error(E_WARNING, "Attempt to assign property '%s' of non-object", ZSTR_VAL(property_name));
zend_string_release(property_name);
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
ZVAL_NULL(EX_VAR(opline->result.var));
}
Expand All @@ -2151,7 +2164,7 @@ ZEND_VM_C_LABEL(fast_assign_obj):
ZVAL_DEREF(value);
}

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

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

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

0 comments on commit 9b87b73

Please sign in to comment.