Better errors for empty registry in AnyMap (#2405)

* Better errors for empty registry

* Make errors non-panic

* clippy
This commit is contained in:
Dominik Maier 2024-07-16 18:10:18 +02:00 committed by GitHub
parent f00470ddaa
commit 713652e5d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,6 +23,9 @@ pub type TypeRepr = u128;
#[cfg(feature = "stable_anymap")] #[cfg(feature = "stable_anymap")]
pub type TypeRepr = Cow<'static, str>; pub type TypeRepr = Cow<'static, str>;
/// Error string when no types at all have been registered yet.
pub(crate) const ERR_EMPTY_TYPES_REGISTER: &str = "Empty types registry. Please enable the `serdeany_autoreg` feature in libafl_bolts or register all required types manually using RegistryBuilder::register().";
#[cfg(not(feature = "stable_anymap"))] #[cfg(not(feature = "stable_anymap"))]
fn type_repr<T>() -> TypeRepr fn type_repr<T>() -> TypeRepr
where where
@ -121,7 +124,7 @@ pub mod serdeany_registry {
hash_map::{Values, ValuesMut}, hash_map::{Values, ValuesMut},
HashMap, HashMap,
}; };
use serde::{Deserialize, Serialize}; use serde::{de, Deserialize, Serialize};
use crate::{ use crate::{
serdeany::{ serdeany::{
@ -156,9 +159,10 @@ pub mod serdeany_registry {
REGISTRY REGISTRY
.deserializers .deserializers
.as_ref() .as_ref()
.expect("Empty types registry") .ok_or_else(||
de::Error::custom(super::ERR_EMPTY_TYPES_REGISTER))?
.get(&id) .get(&id)
.expect("Cannot deserialize an unregistered type") .ok_or_else(|| de::Error::custom(format_args!("Cannot deserialize the unregistered type with id {id}. Enable the `serde_autoreg` feature in libafl_bolts or register all requried types manually.")))?
.0 .0
}; };
let seed = DeserializeCallbackSeed::<dyn crate::serdeany::SerdeAny> { cb }; let seed = DeserializeCallbackSeed::<dyn crate::serdeany::SerdeAny> { cb };
@ -365,7 +369,7 @@ pub mod serdeany_registry {
REGISTRY REGISTRY
.deserializers .deserializers
.as_ref() .as_ref()
.expect("Empty types registry") .expect(super::ERR_EMPTY_TYPES_REGISTER)
.get(type_repr) .get(type_repr)
.is_some() .is_some()
}, },
@ -628,7 +632,7 @@ pub mod serdeany_registry {
REGISTRY REGISTRY
.deserializers .deserializers
.as_ref() .as_ref()
.expect("Empty types registry") .expect(super::ERR_EMPTY_TYPES_REGISTER)
.get(type_repr) .get(type_repr)
.is_some() .is_some()
}, },