Repository: lambdaclass/cairo-vm (language: rust)

Issue:
Double unwrap for `program_base` in Cairo runner `initialize_state`
### Issue Description

In the `initialize_state` function of the `CairoRunner` structure, there is redundant code in my opinion, that can be improved for clarity and efficiency.

At the beginning of the function, there is a conditional check (`if let Some(prog_base) = self.program_base`) to ensure that `self.program_base` is not `None`. 

https://github.com/lambdaclass/cairo-vm/blob/4ea224a1068b43aaa3899d24e834475106e8a78f/vm/src/vm/runners/cairo_runner.rs#L414-L431

However, a few lines later, the `unwrap` method is used again on `self.program_base` to obtain `base`, and in case of `None`, it sets `base` to `Relocatable::from((0, 0))`.

https://github.com/lambdaclass/cairo-vm/blob/4ea224a1068b43aaa3899d24e834475106e8a78f/vm/src/vm/runners/cairo_runner.rs#L425-L427

This redundant check and assignment can be eliminated because the `None` case will never happen inside the `if` statement, making the `base` declaration unnecessary. Instead, we can directly use the `prog_base` variable in the loop to mark memory segments as accessed.






Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1547001753..fee05bc0f3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,7 +3,9 @@
 #### Upcoming Changes
 * feat: add a `--tracer` option which hosts a web server that shows the line by line execution of cairo code along with memory registers [#1265](https://github.com/lambdaclass/cairo-vm/pull/1265)
 
-* feat: Make air public inputs deserializable [#1648](https://github.com/lambdaclass/cairo-vm/pull/1648)
+* feat: Fix error handling in `initialize_state`[#1657](https://github.com/lambdaclass/cairo-vm/pull/1657)
+
+* feat: Make air public inputs deserializable [#1657](https://github.com/lambdaclass/cairo-vm/pull/1648)
 
 * feat: Show only layout builtins in air private input [#1651](https://github.com/lambdaclass/cairo-vm/pull/1651)
 
diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs
index 4dcd533cc9..7becd79eea 100644
--- a/vm/src/vm/runners/cairo_runner.rs
+++ b/vm/src/vm/runners/cairo_runner.rs
@@ -436,30 +436,19 @@ impl CairoRunner {
         entrypoint: usize,
         stack: Vec<MaybeRelocatable>,
     ) -> Result<(), RunnerError> {
-        if let Some(prog_base) = self.program_base {
-            let initial_pc = Relocatable {
-                segment_index: prog_base.segment_index,
-                offset: prog_base.offset + entrypoint,
-            };
-            self.initial_pc = Some(initial_pc);
-            vm.load_data(prog_base, &self.program.shared_program_data.data)
-                .map_err(RunnerError::MemoryInitializationError)?;
-
-            // Mark all addresses from the program segment as accessed
-            let base = self
-                .program_base
-                .unwrap_or_else(|| Relocatable::from((0, 0)));
-            for i in 0..self.program.shared_program_data.data.len() {
-                vm.segments.memory.mark_as_accessed((base + i)?);
-            }
-        }
-        if let Some(exec_base) = self.execution_base {
-            vm.segments
-                .load_data(exec_base, &stack)
-                .map_err(RunnerError::MemoryInitializationError)?;
-        } else {
-            return Err(RunnerError::NoProgBase);
+        let prog_base = self.program_base.ok_or(RunnerError::NoProgBase)?;
+        let exec_base = self.execution_base.ok_or(RunnerError::NoExecBase)?;
+        self.initial_pc = Some((prog_base + entrypoint)?);
+        vm.load_data(prog_base, &self.program.shared_program_data.data)
+            .map_err(RunnerError::MemoryInitializationError)?;
+
+        // Mark all addresses from the program segment as accessed
+        for i in 0..self.program.shared_program_data.data.len() {
+            vm.segments.memory.mark_as_accessed((prog_base + i)?);
         }
+        vm.segments
+            .load_data(exec_base, &stack)
+            .map_err(RunnerError::MemoryInitializationError)?;
         Ok(())
     }
 


Test patch: