Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

I256 SHR can overflow usize #2174

Closed
Jon-Becker opened this issue Feb 21, 2023 · 5 comments · Fixed by #2180
Closed

I256 SHR can overflow usize #2174

Jon-Becker opened this issue Feb 21, 2023 · 5 comments · Fixed by #2180
Labels
bug Something isn't working

Comments

@Jon-Becker
Copy link

Jon-Becker commented Feb 21, 2023

Version
ethers = "1.0.0"

but also occurs on all more recent versions.

Platform
Darwin macbook 21.3.0 Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_ARM64_T8101 arm64

Note that this issue can only be reproduced on certain machines.

Description
Essentially, calling

ethers_core::types::i256::I256
fn shr(self, rhs: I256) -> Self::Output

overflowed usize and throws:
image

I tried this code:

b.value.shr(a.value)

where b.value and a.value are both valid I256 objects. The operation that is causing this issue is

340282366920938463463374607431768190071 >> 0

@Jon-Becker Jon-Becker added the bug Something isn't working label Feb 21, 2023
@Jon-Becker Jon-Becker changed the title SHR can overflow usize I256 SHR can overflow usize Feb 21, 2023
@dbelv
Copy link
Contributor

dbelv commented Feb 23, 2023

So the following code doesn't panic:

let a = I256::from_dec_str("340282366920938463463374607431768190071").unwrap();
let b = I256::zero();
assert_eq!(a >> b, a);
assert_eq!(a.shr(b), a);

Even when messing around, 0 will be able to fit in all types, however this code does panic with,
'Integer overflow when casting to usize', when performing the shr.

let a = I256::from_dec_str("340282366920938463463374607431768190071").unwrap();
let b = I256::zero();
assert_eq!(b >> a, b);
assert_eq!(b.shr(a), b);

I'm assuming this is the error as above.

@Jon-Becker
Copy link
Author

Yes my bad. b is 0 and a is 340282366920938463463374607431768190071 in this case.

@DaniPopes
Copy link
Collaborator

DaniPopes commented Feb 23, 2023

The actual panic is here in primitive-types, trying to convert the rightside to usize which has to be handled before-hand, after being called in heimdall::VM::_step, while executing a SAR opcode. I256 Shr impl is not an arithmetic shift right, see: https://docs.rs/ethers/latest/ethers/types/struct.I256.html#diversion-from-standard-numeric-types

@dbelv
Copy link
Contributor

dbelv commented Feb 23, 2023

Yeah, noticed that the panic was within primitive-types last night due to U256 implementation.

I was going to approach the implementation with checking if we need to actually perform the method (i.e. the rhs of a shift will always fit in a u8 otherwise we know its 0) and then either return the known answer or perform the op.

Although looking at the changes linked seems there was a bit on your mind for clean-up :). I'll leave the implementation decision in your capable hands.

@Jon-Becker
Copy link
Author

Thank you guys, appreciate it!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants