solana_sanitize/
lib.rs

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