From 6779d7c898bba515e7c98f770668d900cb0b9b73 Mon Sep 17 00:00:00 2001 From: T-Dark Date: Tue, 19 Oct 2021 17:29:53 +0100 Subject: [PATCH 1/2] Fixed integer overflows. Why did I even write in the first place? --- ablescript/src/brian.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/ablescript/src/brian.rs b/ablescript/src/brian.rs index 1a4bf12..1073f9b 100644 --- a/ablescript/src/brian.rs +++ b/ablescript/src/brian.rs @@ -110,14 +110,14 @@ impl<'a, I: BootlegRead> Interpreter<'a, I> { let val = self .get_or_resize_tape_mut() .ok_or(ProgramError::TapeSizeExceededLimit)?; - *val = val.checked_add(1).ok_or(ProgramError::IntegerOverflow)?; + *val = val.wrapping_add(1) } b'-' => { let val = self .get_or_resize_tape_mut() .ok_or(ProgramError::TapeSizeExceededLimit)?; - *val = val.checked_sub(1).ok_or(ProgramError::IntegerUnderflow)?; + *val = val.wrapping_sub(1) } b'.' => { @@ -282,8 +282,6 @@ pub enum IoStatus { /// An error that occurred while the interpreter was advancing pub enum ProgramError { DataPointerUnderflow, - IntegerOverflow, - IntegerUnderflow, InputReadError, UnmatchedOpeningBracket, UnmatchedClosingBracket, @@ -297,8 +295,6 @@ impl Display for ProgramError { "{}", match self { ProgramError::DataPointerUnderflow => "data pointer underflow", - ProgramError::IntegerOverflow => "integer overflow", - ProgramError::IntegerUnderflow => "integer underflow", ProgramError::InputReadError => "input read error", ProgramError::UnmatchedOpeningBracket => "unmatched `[`", ProgramError::UnmatchedClosingBracket => "unmatched `]`", @@ -442,4 +438,23 @@ mod tests { )) ); } + + #[test] + fn positive_integer_overflow() { + interpret_with_io(b"+[+]", std::io::empty(), std::io::sink()).unwrap(); + } + + #[test] + fn negative_integer_overflow() { + interpret_with_io(b"-", std::io::empty(), std::io::sink()).unwrap(); + } + + #[test] + fn in_the_past_this_used_to_crash_but_not_anymore() { + let mut interpreter = Interpreter::from_ascii( + b"[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<------------>>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<", + std::io::empty() + ); + interpreter.interpret_with_output(std::io::sink()).unwrap(); + } } From 3e8b80af2a24c28490482e94d5c3e109474d861c Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 19 Oct 2021 23:35:46 +0200 Subject: [PATCH 2/2] Removed `in_the_past_this_used_to_crash_but_not_anymore` test --- ablescript/src/brian.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/ablescript/src/brian.rs b/ablescript/src/brian.rs index 1073f9b..16cb25e 100644 --- a/ablescript/src/brian.rs +++ b/ablescript/src/brian.rs @@ -448,13 +448,4 @@ mod tests { fn negative_integer_overflow() { interpret_with_io(b"-", std::io::empty(), std::io::sink()).unwrap(); } - - #[test] - fn in_the_past_this_used_to_crash_but_not_anymore() { - let mut interpreter = Interpreter::from_ascii( - b"[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++>[-]>[-]<<<[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<<------------>>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<", - std::io::empty() - ); - interpreter.interpret_with_output(std::io::sink()).unwrap(); - } }