Skip to content

Warn about invalid strings in arithmetic #1718

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

Conversation

hikari-no-yume
Copy link
Contributor

Counterpart to this RFC: https://wiki.php.net/rfc/invalid_strings_in_arithmetic

Corresponding language spec pull request: php/php-langspec#155

@@ -726,7 +738,7 @@ ZEND_API void multi_convert_to_string_ex(int argc, ...) /* {{{ */
}
/* }}} */

ZEND_API zend_long ZEND_FASTCALL _zval_get_long_func(zval *op) /* {{{ */
static zend_long ZEND_FASTCALL _zval_get_long_func_ex(zval *op, zend_bool silent) /* {{{ */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Might be worth making this into a zend_always_inline function and then have _zval_get_long_func and _zval_get_long_func_nonsilent or something. Dunno how compiler deals with this atm, haven't benchmarked.

zend_error(E_WARNING, "A non-numeric value encountered");
return 0;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

this will convert the same string twice. This also misses incorrect double string usage, e.g. (5 & "12.5")

Copy link
Member

Choose a reason for hiding this comment

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

"12.5" should probably still be allowed. After all, we also support 5 & 12.5. But agree that if we use is_numeric_string we no longer need the STRTOL call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This part is troublesome. If we use is_numeric_string to convert, then we start supporting scientific notation for numbers, which we don't at present because strtol() doesn't support it.

If we don't use it, though, then there's the problem Dmitry points out: is_numeric_string will accept things that strtol won't handle properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Supporting floats and their notation (scientific) would make PHP more consistent. We currently support this for long parameters in argument parsing, e.g.

$ php -r 'var_dump(intdiv("1e3", 1));'
int(1000)

Yet due to the use of strtol() here, we don't for the integer operators, nor for (int) and intval(). So this means that 3e3 is considered to be 3 for some integer operations and 3000 for others, which is not ideal.

Would it cause backwards-compatibility issues if we started allowing floats here? I don't think it would.

Copy link
Member

Choose a reason for hiding this comment

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

I think more consistent float support makes sense and worth the minor BC break in 7.1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so too. Anyway, this is a significant change, so I'll suspend the vote on the RFC and update it.

@hikari-no-yume hikari-no-yume force-pushed the operator_numeric_string_notices branch from 16047b5 to 6089355 Compare January 26, 2016 23:29
@hikari-no-yume hikari-no-yume force-pushed the operator_numeric_string_notices branch 2 times, most recently from be1e9e8 to fbfcc8b Compare February 5, 2016 11:34
@hikari-no-yume
Copy link
Contributor Author

Okay, this now handles intval(..., 10), settype(), etc. I rewrote history to incorporate this into the "Use is_numeric_string_ex for zval_get_long etc." commit.

@hikari-no-yume hikari-no-yume force-pushed the operator_numeric_string_notices branch 6 times, most recently from 1dbf5bd to 08ca614 Compare February 14, 2016 01:46
}

/* While basic arithmetic operators always produce numeric string errors,
* bitwise operators only produce errors when *both* operands are strings */
Copy link
Member

Choose a reason for hiding this comment

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

s/only/don't?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch.

@hikari-no-yume
Copy link
Contributor Author

Because I rewrote history so many times, the commit timestamps are wrong, and also in a different order to how the commits are applied.

@hikari-no-yume hikari-no-yume force-pushed the operator_numeric_string_notices branch 2 times, most recently from 32be4d6 to 429a75d Compare March 25, 2016 17:17
@hikari-no-yume
Copy link
Contributor Author

RFC has been accepted, so this should be merged sometime soon.

@hikari-no-yume
Copy link
Contributor Author

I think that last Travis failure was some false positive, looks like a MySQL issue.

@hikari-no-yume hikari-no-yume force-pushed the operator_numeric_string_notices branch from 429a75d to 6caf1d4 Compare March 30, 2016 00:37
@hikari-no-yume
Copy link
Contributor Author

Merged via: 1e82ad8

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