-
Notifications
You must be signed in to change notification settings - Fork 25
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
Index out of range panic when displaying ParseError::InvalidByteCount #36
Comments
I recreated the bug before discovering this issue. # Cargo.toml
[package]
name = "test-eui48"
version = "0.1.0"
edition = "2021"
[dependencies]
eui48 = { version = "1.1.0", features = ["serde"] }
serde_json = "1.0.138" // src/main.rs
use serde_json::from_str;
type Res = Result<eui48::MacAddress, serde_json::Error>;
fn main() {
// Ok(MacAddress("01:23:45:01:23:45"))
let addr: Res = from_str("\"01:23:45:01:23:45\"");
println!("{addr:?}");
// Ok(MacAddress("01:23:45:01:23:45"))
let addr: Res = from_str("\"012345012345\"");
println!("{addr:?}");
// Err(Error("Invalid length; expecting 11 to 17 chars, found 23", line: 1, column: 25))
let addr: Res = from_str("\"01:23:45:01:23:45:01:23\"");
println!("{addr:?}");
// thread 'main' panicked at /home/foo/.cargo/registry/src/index.crates.io-6f17d22bba15001f/eui48-1.1.0/src/lib.rs:300:21:
// range end index 8 out of range for slice of length 6
// note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
let _addr: Res = from_str("\"0123450123450123\"");
} I was planning to parse JSON provided by the user. It's good that I tested bad strings before deploying the code, but this needs to be fixed as it's too easy for a malicious user to crash the software. |
FWIW, my proposed one line fix from a year ago would work here:
(and if you add one last
but alas, it hasn't been merged, nor has this crate seen any released updates since 2020. |
The following code panics:
The culprit is this line: https://github.com/abaumhauer/eui48/blob/master/src/lib.rs#L300
In
ParseError::InvalidByteCount(found, eui)
,found
is the number of bytes encountered (in this case seven bytes, more than the expected six bytes), and eui is the eui48 that was created out of the first six bytes. You cannot index into eui beyond six, which is what&eui[..found]
on line 300 appears to be trying to do.The text was updated successfully, but these errors were encountered: