From 9ce5b8153f80bd6be226a714ad2b6cf7c51a382a Mon Sep 17 00:00:00 2001 From: 9names <60134748+9names@users.noreply.github.com> Date: Tue, 28 May 2024 13:45:27 +1000 Subject: [PATCH] Panic if bit_count > 32 --- src/lib.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f6323f0..dd6e6cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -233,11 +233,21 @@ impl InstructionOperands { *index | (if *relative { 0b10000 } else { 0 }), ) } - InstructionOperands::IN { source, bit_count } => (*source as u8, *bit_count & 0b11111), + InstructionOperands::IN { source, bit_count } => { + if *bit_count > 32 { + panic!("bit_count must be from 1 to 32"); + } + (*source as u8, *bit_count & 0b11111) + } InstructionOperands::OUT { destination, bit_count, - } => (*destination as u8, *bit_count & 0b11111), + } => { + if *bit_count > 32 { + panic!("bit_count must be from 1 to 32"); + } + (*destination as u8, *bit_count & 0b11111) + } InstructionOperands::PUSH { if_full, block } => { ((*if_full as u8) << 1 | (*block as u8), 0) }