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

PHP 7: Fix negative bitshift error when no buckets are enabled #38

Merged
merged 2 commits into from
Sep 5, 2017

Conversation

ctrlrsf
Copy link
Contributor

@ctrlrsf ctrlrsf commented Sep 1, 2017

If a slug exists in the database but no buckets are enabled, we get back
an empty string. The empty string gets coreced into 0, then we subtact
1 to do the bitshift, and we end up with the negative bitshift error. In
php 5.x this is a silent error where the value ends up being
int(-9223372036854775808), but in php 7.1 it's a fatal error.

With this change when we reduce the array, if $index is 0 we just return
the $mask we have accumulated so far. There's no need to shift the 0
$index so we avoid it by returning early.

Closes: #33

Below are some examples of what reduceToBitmask was returning:

Before:

When Test.a swivel_features = 1,2,3,4,5,6,7,8,9,10
reduceToBitmask() returned int(1023)

When Test.a swivel_features = ""
reduceToBitmask() returned int(-9223372036854775808)

Now:

When Test.a swivel_features = 1,2,3,4,5,6,7,8,9,10
reduceToBitmask() returns int(1023)

When Test.a swivel_features = ""
reduceToBitmask() returns int(0)

If a slug exists in the database but no buckets are enabled, we get back
an empty string. The empty string gets coreced into 0, then we subtact
1 to do the bitshift, and we end up with the negative bitshift error. In
php 5.x this is a silent error where the value ends up being
int(-9223372036854775808), but in php 7.1 it's a fatal error.

With this change when we reduce the array, if $index is 0 we just return
the $mask we have accumulated so far. There's no need to shift the 0
$index so we avoid it by returning early.

Below are some examples of what reduceToBitmask was returning:

Before:
```
When Test.a swivel_features = 1,2,3,4,5,6,7,8,9,10
reduceToBitmask() returned int(1023)

When Test.a swivel_features = ""
reduceToBitmask() returned int(-9223372036854775808)
```

Now:
```
When Test.a swivel_features = 1,2,3,4,5,6,7,8,9,10
reduceToBitmask() returns int(1023)

When Test.a swivel_features = ""
reduceToBitmask() returns int(0)
```

Signed-off-by: Rene Fragoso <ctrlrsf@gmail.com>
These tests confirm graceful handling of swivels with no buckets
enabled. If not handled properly, they would result in a negative
bitshift error.

Signed-off-by: Rene Fragoso <ctrlrsf@gmail.com>
@jrbasso jrbasso merged commit 2a42f99 into zumba:v1.3 Sep 5, 2017
@jrbasso
Copy link
Member

jrbasso commented Sep 5, 2017

Tagged as v1.3.3.

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.

2 participants