-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Safe casting functions #874
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
Conversation
It might also be good to have a test for |
|
||
echo PHP_EOL; | ||
|
||
// check truncation |
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.
These assertions might make more sense under the "shouldn't pass" section since we no longer truncate.
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.
We never did... the "shouldn't pass" section is poorly labelled.
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.
I'm referring to the change in 9d135f7, and the assertions on lines 38-39.
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.
Oh, right. Still...
@theodorejb I see no reason to check for |
@TazeTSchnitzel Hmm, maybe it depends on your implementation. Right now there are tests for float overflows, but not string overflows. |
Good point, I'll need to handle that. |
Do |
As it happens, they do. But I'll add a test for that. |
Done! |
* This is a shame, as an extra malloc is always bad | ||
* TODO: Stop this segfaulting so we can use it instead | ||
*/ | ||
/*RETVAL_STR(zend_strpprintf("%.*G", (int) EG(precision), Z_DVAL_P(var)));*/ |
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.
zend_strpprintf(0, "%.*G", (int) EG(precision), Z_DVAL_P(var));
max_len is first parameter ... http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_exceptions.h#57
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.
Alright, if I do that, I just get this:
Segmentation fault: 11
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.
With this patch applied:
diff --git a/ext/standard/type.c b/ext/standard/type.c
index 3c1d4a7..12e5320 100644
--- a/ext/standard/type.c
+++ b/ext/standard/type.c
@@ -546,25 +546,7 @@ PHP_FUNCTION(to_string)
RETURN_STR(zend_long_to_str(Z_LVAL_P(var)));
case IS_DOUBLE:
- /* This code, despite being used in many other places, segfaults
- * Since I can't figure out why, it's commented out for now
- * This is a shame, as an extra malloc is always bad
- * TODO: Stop this segfaulting so we can use it instead
- */
- /*RETVAL_STR(zend_strpprintf("%.*G", (int) EG(precision), Z_DVAL_P(var)));*/
-
- {
- char *str;
- zend_string *zstr;
- int len;
-
- str = emalloc(MAX_LENGTH_OF_DOUBLE + EG(precision) + 1);
- len = zend_sprintf(str, "%.*G", (int) EG(precision), Z_DVAL_P(var));
- zstr = zend_string_init(str, len, 0);
- RETVAL_STR(zstr);
- efree(str);
- }
- return;
+ RETURN_STR(strpprintf(0, "%.*G", (int) EG(precision), Z_DVAL_P(var)));
case IS_OBJECT:
{
I pass to_string test.
RFC was rejected, forgot to close earlier. |
https://wiki.php.net/rfc/safe_cast