Skip to content

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

Closed
wants to merge 26 commits into from
Closed

Conversation

hikari-no-yume
Copy link
Contributor

@theodorejb
Copy link
Contributor

It might also be good to have a test for to_int((string) PHP_INT_MAX . "0"). This is a case that bit my implementation at one point.


echo PHP_EOL;

// check truncation
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. Still...

@hikari-no-yume
Copy link
Contributor Author

@theodorejb I see no reason to check for to_int((string) PHP_INT_MAX . "0")... it's not particularly special.

@theodorejb
Copy link
Contributor

@TazeTSchnitzel Hmm, maybe it depends on your implementation. Right now there are tests for float overflows, but not string overflows.

@hikari-no-yume
Copy link
Contributor Author

Good point, I'll need to handle that.

@theodorejb
Copy link
Contributor

Do to_int and to_float fail with blank strings? I think there should be a test for this.

@hikari-no-yume
Copy link
Contributor Author

As it happens, they do. But I'll add a test for that.

@hikari-no-yume
Copy link
Contributor Author

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)));*/
Copy link
Member

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

Copy link
Contributor Author

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

Copy link
Member

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.

@smalyshev smalyshev added the RFC label Nov 18, 2014
@hikari-no-yume
Copy link
Contributor Author

RFC was rejected, forgot to close earlier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants