From 5368dd5209e902157f65ab424d3420d99064d42e Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Mon, 16 Aug 2021 15:06:40 -0600 Subject: [PATCH] Fix panics on invalid indexes --- ablescript/src/variables.rs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/ablescript/src/variables.rs b/ablescript/src/variables.rs index eeaab8d..f088b16 100644 --- a/ablescript/src/variables.rs +++ b/ablescript/src/variables.rs @@ -1,6 +1,6 @@ use std::{ - cell::RefCell, collections::HashMap, fmt::Display, hash::Hash, io::Write, mem::discriminant, - rc::Rc, + cell::RefCell, collections::HashMap, convert::TryFrom, fmt::Display, hash::Hash, io::Write, + mem::discriminant, rc::Rc, }; use rand::Rng; @@ -138,15 +138,26 @@ impl Value { pub fn index(&self, index: &Value) -> Rc> { Rc::new(RefCell::new(match self { Value::Nul => Value::Nul, - Value::Str(s) => Value::Int(s.as_bytes()[index.to_i32() as usize] as i32), - Value::Int(i) => { - Value::Int((format!("{}", i).as_bytes()[index.to_i32() as usize] - b'0') as i32) - } + Value::Str(s) => Value::Int( + usize::try_from(index.to_i32() - 1) + .ok() + .and_then(|idx| s.as_bytes().get(idx).cloned()) + .map(|value| value as i32) + .unwrap_or(0), + ), + Value::Int(i) => Value::Int( + usize::try_from(index.to_i32() - 1) + .ok() + .and_then(|idx| format!("{}", i).as_bytes().get(idx).cloned()) + .map(|value| value as i32) + .unwrap_or(0), + ), Value::Bool(b) => Value::Int( - format!("{}", b) - .chars() - .nth(index.to_i32() as usize) - .unwrap_or('?') as i32, + usize::try_from(index.to_i32() - 1) + .ok() + .and_then(|idx| format!("{}", b).as_bytes().get(idx).cloned()) + .map(|value| value as i32) + .unwrap_or(0), ), Value::Abool(b) => Value::Int(*b as i32), Value::Functio(_) => Value::Int(42),