Skip to content

Adjust base convert to allow negative numbers #3911

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 2 commits into from

Conversation

exussum12
Copy link
Contributor

@exussum12 exussum12 commented Mar 3, 2019

Base convert currently does not handle negative numbers for example
"-010" returns "8", "-8" would be expected

This also emits a warning when using invalid chars

Currently

"01#0" is parsed exactly the same as "010" with no warning shown.

The tests do fail currently due to conflicting behavior, base_convert
used to use zend_ulong, this PR would make that a zend_long. There are
tests around the high ends of this which also fail. Not added new tests due to this as both sets couldn't pass and didn't want to change old tests without at least a discussion.

tests would be the two cases described above "-010" and "01#0" showing -8 and 8 (with a warning) respectively)

I imagine this will need an RFC but not sure how to start that process

Targeting PHP8 would likely be best due to the BC changes

Base convert currently does not handle negative numbers for example
"-010" returns "8", "-8" would be expected

This also emits a warning when using invalid chars

Currently

"01#0" is parsed exactly the same as "010" with no warning shown.

The tests do fail currently due to conflicting behaviour, base_convert
used to use zend_ulong, this PR would make that a zend_long. There are
tests around the high ends of this which also fail.

I imagine this will need an RFC but not sure how to start that process

Targeting PHP8 would likely be best due to the BC changes
@cmb69
Copy link
Member

cmb69 commented Mar 3, 2019

I imagine this will need an RFC but not sure how to start that process

See https://wiki.php.net/rfc/howto.

@exussum12
Copy link
Contributor Author

Thanks. I'm currently trying to get access to post to internals. Thank you

@@ -898,6 +904,15 @@ PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)
}
}

if (invalid_chars > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

You'll probably need some extra handling to make sure that 0xDEADBEEF, 0b10101001 and 0o640 do not generate a warning.

@nikic nikic added the Feature label Mar 4, 2019
@nikic
Copy link
Member

nikic commented Mar 4, 2019

Another thing to consider would be allowing leading and trailing whitespace.

@exussum12
Copy link
Contributor Author

I will adjust to fix that. I think the first three work currently. Will adjust the whitespace one

Still not had the email subscription to internals though though

@krakjoe
Copy link
Member

krakjoe commented Mar 15, 2019

@derickr can you help this person out with internals subscription please ?

@exussum12 send derick your email (privately) ...

@exussum12
Copy link
Contributor Author

Sara helped out with it. Ive posted there now

https://externals.io/message/104618


if (c >= base) {
if (i == length -1) {
if (base == 16 && c == 33) { /* allow the second char to be x */
Copy link
Contributor

Choose a reason for hiding this comment

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

Only if the first char is 0? I don't think we want to allow 9xAB for hex strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#4328 This has been addressed here

@exussum12
Copy link
Contributor Author

Closing a new PR will be opened for 7.4 (#4328) and master

@exussum12 exussum12 closed this Jun 20, 2020
@exussum12
Copy link
Contributor Author

Closed. This was 2 parts. The part remaining didn't pass the RFC

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.

5 participants