1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 18:24:05 +02:00

fix panic in log_base2 in malformed demos

This commit is contained in:
Robin Appelman 2020-01-19 21:15:03 +01:00
commit e412399309

View file

@ -286,5 +286,7 @@ pub fn read_var_int(stream: &mut Stream) -> ReadResult<u32> {
}
pub fn log_base2<T: PrimInt + Unsigned>(num: T) -> u32 {
(std::mem::size_of::<T>() as u32 * 8 - 1) - num.leading_zeros()
// log(0) = inf, but that's a useless result
// since this would only happen in malformed demos, we just return 0
(std::mem::size_of::<T>() as u32 * 8 - 1).saturating_sub(num.leading_zeros())
}