alloy-rs / rlp

Fast implementation of Ethereum RLP serialization
Apache License 2.0
77 stars 16 forks source link

[Bug] Encodable::length returns incorrect values on 32-bit targets #1

Closed Wollac closed 12 months ago

Wollac commented 12 months ago

Component

rlp

What version of Alloy are you on?

alloy-rlp v0.1.0 (higher version are also affected)

Operating System

None

Describe the bug

When deriving Encodable::length, the underlying function alloy_rlp::length_of_length uses the hard-coded value of 8 for the number of bytes in a usize. However, on a 32-bit target, this is incorrect. In certain cases, this results in invalid encodings on 32-bit targets.

Using the correct number of bytes by changing the affected line to the following fixes this problem:

1 + (usize::BITS as usize / 8) - payload_length.leading_zeros() as usize / 8

Of course, if it would be helpful in any way, I can also create a PR for this tiny fix.

DaniPopes commented 12 months ago

Ah nice catch, you can open a PR for it if you want. I've moved this to the new RLP repo.