From 03dae41b2ec20905af780bbcf5c033f683f19efb Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 18 Dec 2024 10:25:12 +0100 Subject: Prevent addresses which are out of bounds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In some bad scenarios addresses might be pointing out of bounds (e.g. a reference further than 0xFFFF). This has to be avoided and through fuzzy testing we even got Rust panics for out of bounds u16 arithmetic. Hence, just go through usize for the actual computation and check with u16::MAX. Signed-off-by: Miquel Sabaté Solà --- lib/xixanta/src/errors.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'lib/xixanta/src/errors.rs') diff --git a/lib/xixanta/src/errors.rs b/lib/xixanta/src/errors.rs index d64183d..f6317d4 100644 --- a/lib/xixanta/src/errors.rs +++ b/lib/xixanta/src/errors.rs @@ -37,6 +37,7 @@ pub enum ContextErrorReason { UnknownVariable, BadScope, Label, + Bounds, Other, } @@ -45,18 +46,23 @@ pub struct ContextError { pub line: usize, pub reason: ContextErrorReason, pub message: String, + pub global: bool, } impl std::error::Error for ContextError {} impl fmt::Display for ContextError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "Context error (line {}): {}.", - self.line + 1, - self.message - ) + if self.global { + write!(f, "Context error: {}.", self.message) + } else { + write!( + f, + "Context error (line {}): {}.", + self.line + 1, + self.message + ) + } } } -- cgit v1.2.3