From 859bc83bd251464714c0632408b07695f6f98835 Mon Sep 17 00:00:00 2001 From: Kotha Dhakshin <179742818+Dhakshin2007@users.noreply.github.com> Date: Fri, 3 Apr 2026 10:11:11 +0530 Subject: [PATCH] fix(ir/validator): fix crash + typos in validator.rs 1. BUG FIX (crash): validate_no_branch_in_the_middle_of_block_instructions() calls block.instructions[0..block.instructions.len() - 1] which panics in debug mode (integer underflow) when block.instructions is empty. Fixed by using .saturating_sub(1) so that an empty slice is returned for empty instruction lists. 2. fix typo: "consition" -> "condition" in validate_if_condition_is_bool error message (introduced in commit 90fb0aa) 3. fix typo: "variabls" -> "variables" in validate_all_ids_are_present_in_block error message 4. fix typo: "at lease 1" -> "at least 1" in OpCode::Switch validation error message 5. fix typo: "paird" -> "paired" in TODO comment --- src/compiler/translator/ir/src/validator.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/compiler/translator/ir/src/validator.rs b/src/compiler/translator/ir/src/validator.rs index c1d0b3a0ad5..8a9cf8e0099 100644 --- a/src/compiler/translator/ir/src/validator.rs +++ b/src/compiler/translator/ir/src/validator.rs @@ -35,7 +35,7 @@ // - If conditions are boolean: validate_if_condition_is_bool() // TODO(http://anglebug.com/349994211): to validate: -// - Referenced IDs are not dead-code-eliminated: Checking against max_*_count can be paird with a +// - Referenced IDs are not dead-code-eliminated: Checking against max_*_count can be paired with a // look up and checking that !is_dead_code_eliminated // - If there's a cached "has side effect", that it's correct. // - No operations should have entirely constant arguments, that should be folded (and @@ -408,7 +408,7 @@ impl<'a> Validator<'a> { for variable in &block.variables { if variable.id >= self.max_variable_count { self.on_error(format_args!( - "invalid variable id {} found in block variabls", + "invalid variable id {} found in block variables", variable.id )); } @@ -1286,7 +1286,7 @@ impl<'a> Validator<'a> { if current_block.case_blocks.is_empty() { self.on_error(format_args!( "OpCode::Switch is missing a valid target. Current block should contain \ - at lease 1 case_block" + at least 1 case_block" )); } if current_block.merge_block.is_none() { @@ -1452,7 +1452,7 @@ impl<'a> Validator<'a> { } fn validate_no_branch_in_the_middle_of_block_instructions(&self, block: &Block) { - for instruction in &block.instructions[0..block.instructions.len() - 1] { + for instruction in &block.instructions[0..block.instructions.len().saturating_sub(1)] { if instruction.is_branch() { self.on_error(format_args!( "branch instruction is only allowed in the last instruction in the block" @@ -1566,7 +1566,7 @@ impl<'a> Validator<'a> { if condition.type_id != TYPE_ID_BOOL => { self.on_error(format_args!( - "invalid If/LoopIf instruction: {:?}, consition is not a boolean: {:?}", + "invalid If/LoopIf instruction: {:?}, condition is not a boolean: {:?}", opcode, condition.type_id )); }