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
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0de1e1a
toInt(), toFloat() and toString() safe casting functions
hikari-no-yume Oct 5, 2014
78101d5
Renamed toInt/toFloat/toString to to_int/to_float/to_string
hikari-no-yume Oct 6, 2014
a0f372c
Return FALSE on failure, not NULL
hikari-no-yume Oct 6, 2014
c3a73f0
Removed base argument from to_int
hikari-no-yume Oct 6, 2014
26c7ecf
Oh, RETURN_FALSE *does* exist
hikari-no-yume Oct 6, 2014
56ae42a
use zend_string_copy to avoid breaking interned strings
hikari-no-yume Oct 6, 2014
82cefa6
Removed redundant breaks
hikari-no-yume Oct 6, 2014
6fc532b
Removed unneeded TSRMLS_FETCH();
hikari-no-yume Oct 6, 2014
9ea1bfb
Add commented-out zend_strpprintf block mentioning segfault
hikari-no-yume Oct 6, 2014
9d135f7
Reject non-integral floats passed to to_int
hikari-no-yume Oct 9, 2014
c672669
We don't need to use zend_dval_to_lval here
hikari-no-yume Oct 9, 2014
9898449
Removed the (now redundant) INF/NAN check
hikari-no-yume Oct 9, 2014
03401d9
Clarify that "10.0" should work for float, not for int
hikari-no-yume Oct 11, 2014
6c05ba9
Don't permit leading or trailing whitespace
hikari-no-yume Oct 20, 2014
0cce32d
Test string overflow, octal, hex for to_int()
hikari-no-yume Oct 21, 2014
0f7c856
Test string overflow, octal, hex for to_int(), to_float()
hikari-no-yume Oct 21, 2014
5158a43
Merge branch 'master' into safe_casts
hikari-no-yume Nov 7, 2014
b845227
Merge branch 'master' into safe_casts
hikari-no-yume Nov 10, 2014
f5cf9df
Return NULL, not FALSE, on failure
hikari-no-yume Nov 11, 2014
9afe6a5
Reject leading 0, +
hikari-no-yume Nov 12, 2014
d9361da
Test empty strings
hikari-no-yume Nov 12, 2014
6144649
Note that first char check also excludes empty strings
hikari-no-yume Nov 12, 2014
fccf267
Use more efficient float-string conversion which appears not to segfa…
hikari-no-yume Nov 14, 2014
5632a34
Move casting behaviour into own functions, to allow multiple interfaces
hikari-no-yume Nov 14, 2014
d6c0182
Made to_* functions throw exceptions, add try_* functions to return NULL
hikari-no-yume Nov 14, 2014
7b3a900
Allow + prefix to signify sign
hikari-no-yume Nov 19, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add commented-out zend_strpprintf block mentioning segfault
  • Loading branch information
hikari-no-yume committed Oct 6, 2014
commit 9ea1bfb0a6668afdeecd7bdd939c8a7c625cd679
7 changes: 7 additions & 0 deletions ext/standard/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,13 @@ 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)));*/
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.


{
char *str;
zend_string *zstr;
Expand Down