Skip to content

Commit 1e4bd4d

Browse files
PE: fix TLS raw data parser (#473)
Co-authored-by: Jorge Alejandro Durán Royo <[email protected]>
1 parent 1b726cf commit 1e4bd4d

File tree

1 file changed

+8
-23
lines changed

1 file changed

+8
-23
lines changed

src/pe/tls.rs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -219,30 +219,15 @@ impl<'a> TlsData<'a> {
219219
// VA to RVA
220220
let rva = itd.start_address_of_raw_data - image_base;
221221
let size = itd.end_address_of_raw_data - itd.start_address_of_raw_data;
222-
let offset = utils::find_offset(rva as usize, sections, file_alignment, opts)
223-
.ok_or_else(|| {
224-
error::Error::Malformed(format!(
225-
"cannot map tls start_address_of_raw_data rva ({:#x}) into offset",
226-
rva
227-
))
228-
})?;
229-
230-
let offset_end = offset.checked_add(size as usize).ok_or_else(|| {
231-
error::Error::Malformed(format!(
232-
"tls start_address_of_raw_data ({:#x}) + size_of_raw_data ({:#x}) casues an integer overflow",
233-
offset, size
234-
))
235-
})?;
222+
let offset = utils::find_offset(rva as usize, sections, file_alignment, opts);
236223

237-
if offset > bytes.len() || offset_end > bytes.len() {
238-
return Err(error::Error::Malformed(format!(
239-
"tls raw data offset ({:#x}) and size ({:#x}) greater than byte slice len ({:#x})",
240-
offset,
241-
size,
242-
bytes.len()
243-
)));
244-
}
245-
raw_data = Some(&bytes[offset..offset + size as usize]);
224+
raw_data = offset.and_then(|offset| {
225+
if offset < bytes.len() {
226+
(&bytes[offset..]).pread_with(0, size as usize).ok()
227+
} else {
228+
None
229+
}
230+
});
246231
}
247232

248233
// Parse the index if any

0 commit comments

Comments
 (0)