-
Notifications
You must be signed in to change notification settings - Fork 8k
Improve TypeError messages for non-numeric/empty strings in coercions; Closes #20632 #20639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: Michael Voříšek <mvorisek@mvorisek.cz>
iluuu1994
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also see my comment in #20632 (comment).
| return "empty-string"; | ||
| } | ||
|
|
||
| return "non-numeric-string"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not all non-empty strings are non-numeric. You can see this in the tests, it says non-numeric for many examples where one of the strings is actually numeric.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To address the issue of numeric strings being reported as "non-numeric string", would adding an is_numeric_string_ex() check like this in zend_zval_numeric_string_value_name() be sufficient?
zend_long lval;
double dval;
bool trailing_data = false;
if (is_numeric_string_ex(Z_STRVAL_P(val), Z_STRLEN_P(val),&lval, &dval, 0, NULL, &trailing_data))
{
return "string";
}Ref.: ext/opcache/jit/zend_jit_helpers.c:1189
case IS_STRING:
{
bool trailing_data = false;
/* For BC reasons we allow errors so that we can warn on leading numeric string */
if (IS_LONG == is_numeric_string_ex(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL,
/* allow errors */ true, NULL, &trailing_data)) {
if (UNEXPECTED(trailing_data)
&& EG(current_execute_data)->opline->opcode != ZEND_FETCH_DIM_UNSET) {
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
}
return offset;
}
zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_STRING), dim, BP_VAR_R);
return 0;
}The idea is that empty strings still map to "empty string", non-empty non-numeric strings to "non-numeric string", and numeric strings fall back to "string" so they’re not mislabelled.
…e callers Removed unnecessary changes.
Implements proposal from #20632