Skip to content

Commit bea8914

Browse files
committed
Prevent potential panic when slicing .reginfo
1 parent 0f4904a commit bea8914

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

objdiff-core/src/arch/mips.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{borrow::Cow, collections::BTreeMap, sync::Mutex};
22

33
use anyhow::{anyhow, bail, Result};
4+
use log::log;
45
use object::{
56
elf, Endian, Endianness, File, FileFlags, Object, ObjectSection, ObjectSymbol, Relocation,
67
RelocationFlags, RelocationTarget,
@@ -64,13 +65,13 @@ impl ObjArchMips {
6465
// Parse the ri_gp_value stored in .reginfo to be able to correctly
6566
// calculate R_MIPS_GPREL16 relocations later. The value is stored
6667
// 0x14 bytes into .reginfo (on 32 bit platforms)
67-
let ri_gp_value = match object.section_by_name(".reginfo").and_then(|s| s.data().ok()) {
68-
Some(reginfo) => {
69-
let gp_ri_value_bytes = reginfo[0x14..0x18].try_into().unwrap_or([0u8; 4]);
70-
object.endianness().read_i32_bytes(gp_ri_value_bytes)
71-
}
72-
None => 0,
73-
};
68+
let ri_gp_value = object
69+
.section_by_name(".reginfo")
70+
.and_then(|section| section.data().ok())
71+
.and_then(|data| data.get(0x14..0x18))
72+
.and_then(|s| s.try_into().ok())
73+
.map(|bytes| object.endianness().read_i32_bytes(bytes))
74+
.unwrap_or(0);
7475

7576
Ok(Self { endianness: object.endianness(), abi, instr_category, ri_gp_value })
7677
}

0 commit comments

Comments
 (0)