Validate prefix size.

This commit is contained in:
Orne Brocaar 2023-04-27 14:25:08 +01:00
parent 21896e65d7
commit cfadb9c78a
2 changed files with 14 additions and 0 deletions

View File

@ -8,6 +8,12 @@ pub enum Error {
#[error("EuiPrefix must be in the form 0000000000000000/0")]
EuiPrefixFormat,
#[error("DevAddrPrefix max prefix size is 32")]
DevAddrPrefixSize,
#[error("EuiPrefix max prefix size is 64")]
EuiPrefixSize,
#[error(transparent)]
FromHexError(#[from] hex::FromHexError),
}

View File

@ -146,6 +146,10 @@ impl FromStr for DevAddrPrefix {
.parse()
.map_err(|_| error::Error::DevAddrPrefixFormat)?;
if size > 32 {
return Err(error::Error::DevAddrPrefixSize);
}
Ok(DevAddrPrefix(mask, size))
}
}
@ -237,6 +241,10 @@ impl FromStr for EuiPrefix {
.parse()
.map_err(|_| error::Error::EuiPrefixFormat)?;
if size > 64 {
return Err(error::Error::EuiPrefixSize);
}
Ok(EuiPrefix(mask, size))
}
}