Skip to content
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

abuse bitwise ops to check for num vs string to avoid B.pm #18

Merged
merged 2 commits into from
Oct 21, 2016

Conversation

haarg
Copy link
Contributor

@haarg haarg commented Feb 1, 2016

B.pm is a rather large module to load, and numbers and strings can be
detected by abusing the behavior of bitwise operators. Using a bitwise
operator is faster as well.

A string & "" results in ""
A number & "" results in 0 (with a warning)

Some variables won't be fully caught by this check alone. A string like
"0.0" that has been used in a numeric context will pass the check as if
it was a number. We detect this by making sure the string is the same
as stringifing the numeric form.

"nan" and "inf" that have been used as numbers will also pass that
check, so we detect them by making sure multiplying by 0 gives 0.

B.pm is a rather large module to load, and numbers and strings can be
detected by abusing the behavior of bitwise operators.  Using a bitwise
operator is faster as well.

A string & "" results in ""
A number & "" results in 0 (with a warning)

Some variables won't be fully caught by this check alone.  A string like
"0.0" that has been used in a numeric context will pass the check as if
it was a number.  We detect this by making sure the string is the same
as stringifing the numeric form.

"nan" and "inf" that have been used as numbers will also pass that
check, so we detect them by making sure multiplying by 0 gives 0.
@haarg
Copy link
Contributor Author

haarg commented Feb 2, 2016

This code also isn't effected by the deprecation added in perl 5.23.6 of using bitwise ops on code points >0xFF. Because one of the strings we are &ing is an empty string, processing stops before it gets to any of the actual characters in the value we're checking. This avoids tripping the deprecation warning, so it should also avoid a future promotion of that deprecation to an exception.

@makamaka makamaka assigned makamaka and unassigned makamaka Mar 15, 2016
@charsbar charsbar merged commit 9885d52 into makamaka:master Oct 21, 2016
charsbar added a commit that referenced this pull request Oct 21, 2016
- we need to modify this part anyway because inf/nan is not valid input from the start, and their representations vary depending on the OS
@charsbar
Copy link
Collaborator

Thanks for the cool patch. I tweaked it a bit to address the inf/nan issue later, in a different issue (#14, and makamaka/JSON#21 ).

charsbar added a commit that referenced this pull request Oct 21, 2016
…master

- merged only t/117_numbers.t as the issue has already been addressed by #18
@skaji
Copy link

skaji commented Jun 8, 2017

It seems that in perl 5.28, it will be a fatal error if an operand contains a character whose ordinal value is above 0xFF. https://perl5.git.perl.org/perl.git/commitdiff/fac71630f045e7ab325b37548d59b6558735e5c3

In fact, with the current blead, the hack A string & "" does not work anymore.

❯ perl -v
This is perl 5, version 27, subversion 1 (v5.27.1 (v5.27.0-263-g7b3443d31f)) built for darwin-2level

❯ perl -Ilib -Mutf8 -MJSON::PP -e 'JSON::PP->new->encode(["あ"])'
Use of strings with code points over 0xFF as arguments to bitwise and (&) operator is not allowed at lib/JSON/PP.pm line 423.

# minimal case
❯ perl -Mutf8 -e '"" & "あ"'
Use of strings with code points over 0xFF as arguments to bitwise and (&) operator is not allowed at -e line 1.

@haarg What should we do?

@haarg
Copy link
Contributor Author

haarg commented Jun 8, 2017

That seems like a bug in blead. This PR may have to be reverted, but the current change in blead is taking something that previously didn't warn and makes it fatal, which doesn't follow the deprecation policy.

@skaji
Copy link

skaji commented Jun 8, 2017

the current change in blead is taking something that previously didn't warn and makes it fatal

Indeed.

❯ perl -Mutf8 -Mwarnings -E 'say $^V; print "" & "あ"'
v5.26.0
❯ echo $?
0

❯ perl -Mutf8 -Mwarnings -E 'say $^V; print "" & "あ"'
v5.27.1
Use of strings with code points over 0xFF as arguments to bitwise and (&) operator is not allowed at -e line 1.
❯ echo $?
255

@charsbar
Copy link
Collaborator

charsbar commented Jun 9, 2017

@haarg, thanks for starting a thread in p5p ML.

@skaji
Copy link

skaji commented Jun 15, 2017

It seems that the behavior was restored for a while.
https://perl5.git.perl.org/perl.git/commitdiff/b50535da2f4aaf97d13e96cda0069755fb6bbf76

Thanks, @haarg !

@haarg haarg deleted the bitwise branch May 30, 2022 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants