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

libstd: modify wrong shift width #6108

Merged
merged 2 commits into from
Apr 30, 2013
Merged

libstd: modify wrong shift width #6108

merged 2 commits into from
Apr 30, 2013

Conversation

gifnksm
Copy link
Contributor

@gifnksm gifnksm commented Apr 29, 2013

std::bigint contains the following code.

borrow = *elem << (uint::bits - n_bits);

The code above contains a bug that the value of the right operand of the shift operator exceeds the size of the left operand,
because sizeof(*elem) == 32, and 0 <= n_bits < 32 in 64bit architecture.

If --opt-level option is not given to rustc, the code above runs as if the right operand is (uint::bits - n_bits) % 32,
but if --opt-level is given, borrow is always zero.

I wonder why this bug is not catched in the libstd's testsuite (I try the rustc --test --opt-level=2 bigint.rs before fixing the bug,
but the unittest passes normally.)

This pull request also removes the implicit vector copies in bigint.rs.

borrow = *elem << (uint::bits - n_bits);

The code above contains a bug that the value of the right operand of the shift operator exceeds the size of the left operand,
because sizeof(*elem) == 32, and 0 <= n_bits < 32 in 64bit architecture.

If `--opt-level` option is not given to rustc, the code above runs as if the right operand is `(uint::bits - n_bits) % 32`,
but if --opt-level is given, `borrow` is always zero.

I wonder why this bug is not catched in the libstd's testsuite (I try the `rustc --test --opt-level=2 bigint.rs` before fixing the bug,
but the unittest passes normally.)
bors added a commit that referenced this pull request Apr 30, 2013
`std::bigint` contains the following code.
```rust
borrow = *elem << (uint::bits - n_bits);
```
The code above contains a bug that the value of the right operand of the shift operator exceeds the size of the left operand,
because sizeof(*elem) == 32, and 0 <= n_bits < 32 in 64bit architecture.

If `--opt-level` option is not given to rustc, the code above runs as if the right operand is `(uint::bits - n_bits) % 32`,
but if --opt-level is given, `borrow` is always zero.

I wonder why this bug is not catched in the libstd's testsuite (I try the `rustc --test --opt-level=2 bigint.rs` before fixing the bug,
but the unittest passes normally.)

This pull request also removes the implicit vector copies in `bigint.rs`.
@bors bors closed this Apr 30, 2013
@bors bors merged commit ffa31d2 into rust-lang:incoming Apr 30, 2013
@gifnksm gifnksm deleted the bigint-shift-bug branch April 30, 2013 08:33
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