-
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
Closed
Closed
Changes from all commits
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 78101d5
Renamed toInt/toFloat/toString to to_int/to_float/to_string
hikari-no-yume a0f372c
Return FALSE on failure, not NULL
hikari-no-yume c3a73f0
Removed base argument from to_int
hikari-no-yume 26c7ecf
Oh, RETURN_FALSE *does* exist
hikari-no-yume 56ae42a
use zend_string_copy to avoid breaking interned strings
hikari-no-yume 82cefa6
Removed redundant breaks
hikari-no-yume 6fc532b
Removed unneeded TSRMLS_FETCH();
hikari-no-yume 9ea1bfb
Add commented-out zend_strpprintf block mentioning segfault
hikari-no-yume 9d135f7
Reject non-integral floats passed to to_int
hikari-no-yume c672669
We don't need to use zend_dval_to_lval here
hikari-no-yume 9898449
Removed the (now redundant) INF/NAN check
hikari-no-yume 03401d9
Clarify that "10.0" should work for float, not for int
hikari-no-yume 6c05ba9
Don't permit leading or trailing whitespace
hikari-no-yume 0cce32d
Test string overflow, octal, hex for to_int()
hikari-no-yume 0f7c856
Test string overflow, octal, hex for to_int(), to_float()
hikari-no-yume 5158a43
Merge branch 'master' into safe_casts
hikari-no-yume b845227
Merge branch 'master' into safe_casts
hikari-no-yume f5cf9df
Return NULL, not FALSE, on failure
hikari-no-yume 9afe6a5
Reject leading 0, +
hikari-no-yume d9361da
Test empty strings
hikari-no-yume 6144649
Note that first char check also excludes empty strings
hikari-no-yume fccf267
Use more efficient float-string conversion which appears not to segfa…
hikari-no-yume 5632a34
Move casting behaviour into own functions, to allow multiple interfaces
hikari-no-yume d6c0182
Made to_* functions throw exceptions, add try_* functions to return NULL
hikari-no-yume 7b3a900
Allow + prefix to signify sign
hikari-no-yume File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
--TEST-- | ||
to_float() | ||
--FILE-- | ||
<?php | ||
|
||
function to_float_wrapper($value) { | ||
try { | ||
return to_float($value); | ||
} catch (CastException $e) { | ||
return NULL; | ||
} | ||
} | ||
|
||
// should pass | ||
var_dump(to_float_wrapper("0")); | ||
var_dump(to_float_wrapper(0)); | ||
var_dump(to_float_wrapper(0.0)); | ||
var_dump(to_float_wrapper("10")); | ||
var_dump(to_float_wrapper("+10")); | ||
var_dump(to_float_wrapper("-10")); | ||
var_dump(to_float_wrapper("10.0")); | ||
var_dump(to_float_wrapper(10)); | ||
var_dump(to_float_wrapper(10.0)); | ||
var_dump(to_float_wrapper((string)PHP_INT_MAX)); | ||
var_dump(to_float_wrapper(PHP_INT_MAX)); | ||
var_dump(to_float_wrapper((float)PHP_INT_MAX)); | ||
var_dump(to_float_wrapper((string)PHP_INT_MIN)); | ||
var_dump(to_float_wrapper(PHP_INT_MIN)); | ||
var_dump(to_float_wrapper((float)PHP_INT_MIN)); | ||
|
||
echo PHP_EOL; | ||
|
||
// disallowed types | ||
var_dump(to_float_wrapper("")); | ||
var_dump(to_float_wrapper("0x10")); | ||
var_dump(to_float_wrapper(NULL)); | ||
var_dump(to_float_wrapper(TRUE)); | ||
var_dump(to_float_wrapper(FALSE)); | ||
var_dump(to_float_wrapper(new StdClass)); | ||
var_dump(to_float_wrapper(fopen("data:text/html,foobar", "r"))); | ||
var_dump(to_float_wrapper([])); | ||
|
||
echo PHP_EOL; | ||
|
||
var_dump(to_float_wrapper(1.5)); | ||
var_dump(to_float_wrapper("1.5")); | ||
|
||
echo PHP_EOL; | ||
|
||
// reject leading, trailing chars | ||
var_dump(to_float_wrapper("010")); | ||
var_dump(to_float_wrapper("10abc")); | ||
var_dump(to_float_wrapper("abc10")); | ||
var_dump(to_float_wrapper(" 100 ")); | ||
var_dump(to_float_wrapper("\n\t\v\r\f 78 \n \t\v\r\f \n")); | ||
var_dump(to_float_wrapper("\n\t\v\r\f78")); | ||
var_dump(to_float_wrapper("78\n\t\v\r\f")); | ||
|
||
echo PHP_EOL; | ||
|
||
// overflow/nan/inf | ||
var_dump(to_float_wrapper(INF)); | ||
var_dump(to_float_wrapper(-INF)); | ||
var_dump(to_float_wrapper(NAN)); | ||
var_dump(to_float_wrapper(PHP_INT_MAX * 2)); | ||
var_dump(to_float_wrapper(PHP_INT_MIN * 2)); | ||
var_dump(to_float_wrapper((string)(PHP_INT_MAX * 2))); | ||
var_dump(to_float_wrapper((string)(PHP_INT_MIN * 2))); | ||
|
||
echo PHP_EOL; | ||
|
||
// check exponents work | ||
var_dump(to_float_wrapper("75e-5")); | ||
var_dump(to_float_wrapper("31e+7")); | ||
--EXPECTF-- | ||
float(0) | ||
float(0) | ||
float(0) | ||
float(10) | ||
float(10) | ||
float(-10) | ||
float(10) | ||
float(10) | ||
float(10) | ||
float(%s) | ||
float(%s) | ||
float(%s) | ||
float(-%s) | ||
float(-%s) | ||
float(%s) | ||
|
||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
|
||
float(1.5) | ||
float(1.5) | ||
|
||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
|
||
float(INF) | ||
float(-INF) | ||
float(NAN) | ||
float(%s) | ||
float(%s) | ||
float(%s) | ||
float(%s) | ||
|
||
float(0.00075) | ||
float(310000000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
--TEST-- | ||
to_int() | ||
--FILE-- | ||
<?php | ||
|
||
function to_int_wrapper($value) { | ||
try { | ||
return to_int($value); | ||
} catch (CastException $e) { | ||
return NULL; | ||
} | ||
} | ||
|
||
// should pass | ||
var_dump(to_int_wrapper("0")); | ||
var_dump(to_int_wrapper(0)); | ||
var_dump(to_int_wrapper(0.0)); | ||
var_dump(to_int_wrapper("10")); | ||
var_dump(to_int_wrapper("+10")); | ||
var_dump(to_int_wrapper("-10")); | ||
var_dump(to_int_wrapper(10)); | ||
var_dump(to_int_wrapper(10.0)); | ||
var_dump(to_int_wrapper((string)PHP_INT_MAX)); | ||
var_dump(to_int_wrapper(PHP_INT_MAX)); | ||
var_dump(to_int_wrapper((string)PHP_INT_MIN)); | ||
var_dump(to_int_wrapper(PHP_INT_MIN)); | ||
|
||
echo PHP_EOL; | ||
|
||
// shouldn't pass | ||
var_dump(to_int_wrapper("")); | ||
var_dump(to_int_wrapper("10.0")); | ||
var_dump(to_int_wrapper("75e-5")); | ||
var_dump(to_int_wrapper("31e+7")); | ||
var_dump(to_int_wrapper("0x10")); | ||
|
||
echo PHP_EOL; | ||
|
||
// disallowed types | ||
var_dump(to_int_wrapper(NULL)); | ||
var_dump(to_int_wrapper(TRUE)); | ||
var_dump(to_int_wrapper(FALSE)); | ||
var_dump(to_int_wrapper(new StdClass)); | ||
var_dump(to_int_wrapper(fopen("data:text/html,foobar", "r"))); | ||
var_dump(to_int_wrapper([])); | ||
|
||
echo PHP_EOL; | ||
|
||
// check truncation | ||
var_dump(to_int_wrapper(1.5)); | ||
var_dump(to_int_wrapper("1.5")); | ||
|
||
echo PHP_EOL; | ||
|
||
// reject leading, trailing chars | ||
var_dump(to_int_wrapper("010")); | ||
var_dump(to_int_wrapper("10abc")); | ||
var_dump(to_int_wrapper("abc10")); | ||
var_dump(to_int_wrapper(" 100 ")); | ||
var_dump(to_int_wrapper("\n\t\v\r\f 78 \n \t\v\r\f \n")); | ||
var_dump(to_int_wrapper("\n\t\v\r\f78")); | ||
var_dump(to_int_wrapper("78\n\t\v\r\f")); | ||
|
||
echo PHP_EOL; | ||
|
||
// overflow/nan/inf | ||
var_dump(to_int_wrapper(INF)); | ||
var_dump(to_int_wrapper(-INF)); | ||
var_dump(to_int_wrapper(NAN)); | ||
var_dump(to_int_wrapper(PHP_INT_MAX * 2)); | ||
var_dump(to_int_wrapper(PHP_INT_MIN * 2)); | ||
var_dump(to_int_wrapper((string)(PHP_INT_MAX * 2))); | ||
var_dump(to_int_wrapper((string)(PHP_INT_MIN * 2))); | ||
--EXPECTF-- | ||
int(0) | ||
int(0) | ||
int(0) | ||
int(10) | ||
int(10) | ||
int(-10) | ||
int(10) | ||
int(10) | ||
int(%d) | ||
int(%d) | ||
int(-%d) | ||
int(-%d) | ||
|
||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
|
||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
|
||
NULL | ||
NULL | ||
|
||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
|
||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--TEST-- | ||
to_string() | ||
--FILE-- | ||
<?php | ||
|
||
function to_string_wrapper($value) { | ||
try { | ||
return to_string($value); | ||
} catch (CastException $e) { | ||
return NULL; | ||
} | ||
} | ||
|
||
// valid | ||
var_dump(to_string_wrapper("foobar")); | ||
var_dump(to_string_wrapper(123)); | ||
var_dump(to_string_wrapper(123.45)); | ||
var_dump(to_string_wrapper(INF)); | ||
var_dump(to_string_wrapper(-INF)); | ||
var_dump(to_string_wrapper(NAN)); | ||
var_dump(to_string_wrapper("")); | ||
|
||
echo PHP_EOL; | ||
|
||
// invalid | ||
var_dump(to_string_wrapper(NULL)); | ||
var_dump(to_string_wrapper(TRUE)); | ||
var_dump(to_string_wrapper(FALSE)); | ||
var_dump(to_string_wrapper([])); | ||
var_dump(to_string_wrapper(fopen('data:text/plan,foobar', 'r'))); | ||
|
||
echo PHP_EOL; | ||
|
||
class NotStringable {} | ||
class Stringable { | ||
public function __toString() { | ||
return "foobar"; | ||
} | ||
} | ||
|
||
// objects | ||
var_dump(to_string_wrapper(new StdClass)); | ||
var_dump(to_string_wrapper(new NotStringable)); | ||
var_dump(to_string_wrapper(new Stringable)); | ||
|
||
--EXPECT-- | ||
string(6) "foobar" | ||
string(3) "123" | ||
string(6) "123.45" | ||
string(3) "INF" | ||
string(4) "-INF" | ||
string(3) "NAN" | ||
string(0) "" | ||
|
||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
NULL | ||
|
||
NULL | ||
NULL | ||
string(6) "foobar" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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...