gemachain_program/sanitize.rs
1use thiserror::Error;
2
3#[derive(PartialEq, Debug, Error, Eq, Clone)]
4pub enum SanitizeError {
5 #[error("index out of bounds")]
6 IndexOutOfBounds,
7 #[error("value out of bounds")]
8 ValueOutOfBounds,
9 #[error("invalid value")]
10 InvalidValue,
11}
12
13/// Trait for sanitizing values and members of over the wire messages.
14/// Implementation should recursively decent through the data structure
15/// and sanitize all struct members and enum clauses. Sanitize excludes
16/// signature verification checks, those are handled by another pass.
17/// Sanitize checks should include but are not limited too:
18/// * All index values are in range
19/// * All values are within their static max/min bounds
20pub trait Sanitize {
21 fn sanitize(&self) -> Result<(), SanitizeError> {
22 Ok(())
23 }
24}
25
26impl<T: Sanitize> Sanitize for Vec<T> {
27 fn sanitize(&self) -> Result<(), SanitizeError> {
28 for x in self.iter() {
29 x.sanitize()?;
30 }
31 Ok(())
32 }
33}