solana_sanitize/
lib.rs

1//! A trait for sanitizing values and members of over the wire messages.
2
3use {core::fmt, std::error::Error};
4
5#[derive(PartialEq, Debug, Eq, Clone)]
6pub enum SanitizeError {
7    IndexOutOfBounds,
8    ValueOutOfBounds,
9    InvalidValue,
10}
11
12impl Error for SanitizeError {}
13
14impl fmt::Display for SanitizeError {
15    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16        match self {
17            SanitizeError::IndexOutOfBounds => f.write_str("index out of bounds"),
18            SanitizeError::ValueOutOfBounds => f.write_str("value out of bounds"),
19            SanitizeError::InvalidValue => f.write_str("invalid value"),
20        }
21    }
22}
23
24/// A trait for sanitizing values and members of over-the-wire messages.
25///
26/// Implementation should recursively descend through the data structure and
27/// sanitize all struct members and enum clauses. Sanitize excludes signature-
28/// verification checks, those are handled by another pass. Sanitize checks
29/// should include but are not limited to:
30///
31/// - All index values are in range.
32/// - All values are within their static max/min bounds.
33pub trait Sanitize {
34    fn sanitize(&self) -> Result<(), SanitizeError> {
35        Ok(())
36    }
37}
38
39impl<T: Sanitize> Sanitize for Vec<T> {
40    fn sanitize(&self) -> Result<(), SanitizeError> {
41        for x in self.iter() {
42            x.sanitize()?;
43        }
44        Ok(())
45    }
46}