-
Notifications
You must be signed in to change notification settings - Fork 136
b64ct: subtle overflow bug #205
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Good catch! Perhaps as a simple solution we can configure a maximum allowed length. |
Hm, for some reason compiler is unable to remove panic in this code. Even replacing UPD: Reported as rust-lang/rust#80963. |
Is there some reason you're not using |
It's a simplified, minimal example. The idea is that I plan to loop over the |
Aah, okay, well in that case how about bounds checking the slicing operation like this? |
I am not sure it will help with the loop which I want to write. Consider this code: https://rust.godbolt.org/z/8eK49M I want to take Changing the loop to: for i in (0..buf.len()).step_by(4) {
&buf[i..];
} does not help either (either way, such loop can not be used for code which I have in mind). |
While working on the in-place decoding function, I noticed a subtle overflow bug. Currently
decoded_len
is implemented as:For lengths greater than
usize::MAX/3
it will produce incorrect results. Obviously in practice it's really unlikely that someone will work with more than gigabyte B64-encoded strings and on 64-bit platforms it will be practically impossible to trigger this bug. But nevertheless it's a clear bug.The easiest solution would be to cast
usize
tou128
and back, but it's quite inelegant solution and it looks like a bit hard for compiler to properly infer properties of a resulting value... Another solution would be to use two branches for values bigger thanusize::MAX/3
and not. And the last solution will be to use an alternative formula which would produce identical results.The same applies to
encoded_len
as well, since it containsbytes.len() * 4
.The text was updated successfully, but these errors were encountered: