Skip to content

Commit 7790e85

Browse files
committed
pe: remove allocation from iteration of mvid; fix some unused warnings; remove unused num_resources from ResoureceEntryIterator
1 parent ef7998f commit 7790e85

File tree

3 files changed

+21
-42
lines changed

3 files changed

+21
-42
lines changed

src/pe/clr.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
use alloc::borrow::Cow;
2-
use alloc::string::String;
3-
use alloc::vec::Vec;
41
use core::fmt;
52
use core::iter::FusedIterator;
6-
use core::ops::Not;
7-
use log::debug;
83
use scroll::{Pread, Pwrite, SizeWith};
94

105
use crate::error;
@@ -13,8 +8,6 @@ use crate::pe::options;
138
use crate::pe::section_table;
149
use crate::pe::utils;
1510

16-
use super::import::Bitfield;
17-
1811
/// [`ClrData::signature`]: Indicates a valid signature gathered from first 4-bytes of [`Cor20Header::metadata`] (`'BSJB'`)
1912
pub const DOTNET_SIGNATURE: u32 = 0x424A5342;
2013

@@ -147,12 +140,9 @@ impl<'a> ClrData<'a> {
147140
///
148141
/// If this is [`None`] and the [`crate::pe::debug::ReproInfo`] presents, you should use that instead.
149142
pub fn mvid(&self) -> error::Result<Option<&'a [u8]>> {
150-
Ok(self
151-
.sections()
152-
.collect::<Result<Vec<_>, _>>()?
153-
.iter()
154-
.find(|x| x.name == "#GUID")
155-
.map(|x| {
143+
for stream in self.sections() {
144+
let x = stream?;
145+
if x.name == "#GUID" {
156146
let offset = (x.offset as usize)
157147
.checked_sub(self.offset_of_metadata)
158148
.ok_or_else(|| {
@@ -161,11 +151,14 @@ impl<'a> ClrData<'a> {
161151
x.offset, self.offset_of_metadata
162152
))
163153
})?;
164-
self.metadata_data
154+
return self
155+
.metadata_data
165156
.pread_with::<&[u8]>(offset, x.size as usize)
166157
.map_err(Into::<error::Error>::into)
167-
})
168-
.transpose()?)
158+
.map(Some);
159+
}
160+
}
161+
Ok(None)
169162
}
170163
}
171164

src/pe/exception.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use crate::pe::section_table;
5454
use crate::pe::utils;
5555

5656
/// **N**o handlers.
57+
#[allow(unused)]
5758
const UNW_FLAG_NHANDLER: u8 = 0x00;
5859
/// The function has an exception handler that should be called when looking for functions that need
5960
/// to examine exceptions.
@@ -1140,21 +1141,21 @@ mod tests {
11401141
const UNWIND_INFO_C_SCOPE_TABLE: &[u8] = &[
11411142
// UNWIND_INFO_HDR
11421143
0x09, 0x0F, 0x06, 0x00,
1143-
1144+
11441145
// UNWIND_CODEs
11451146
0x0F, 0x64, // UWOP_SAVE_NONVOL (Offset=6, Reg=0x0F)
11461147
0x09, 0x00,
11471148
0x0F, 0x34, // UWOP_SAVE_NONVOL (Offset=3, Reg=0x0F)
11481149
0x08, 0x00,
11491150
0x0F, 0x52, // UWOP_ALLOC_SMALL (Size = (2 * 8) + 8 = 24 bytes)
11501151
0x0B, 0x70, // UWOP_PUSH_NONVOL (Reg=0x0B)
1151-
1152+
11521153
// Exception handler RVA
11531154
0xC0, 0x1F, 0x00, 0x00, // __C_specific_handler
1154-
1155+
11551156
// Scope count
11561157
0x02, 0x00, 0x00, 0x00, // Scope table count = 2
1157-
1158+
11581159
// First C_SCOPE_TABLE entry
11591160
0x01, 0x15, 0x00, 0x00, // BeginAddress = 0x00001501
11601161
0x06, 0x16, 0x00, 0x00, // EndAddress = 0x00001606
@@ -1172,21 +1173,21 @@ mod tests {
11721173
const UNWIND_INFO_C_SCOPE_TABLE_INVALID: &[u8] = &[
11731174
// UNWIND_INFO_HDR
11741175
0x09, 0x0F, 0x06, 0x00,
1175-
1176+
11761177
// UNWIND_CODEs
11771178
0x0F, 0x64, // UWOP_SAVE_NONVOL (Offset=6, Reg=0x0F)
11781179
0x09, 0x00,
11791180
0x0F, 0x34, // UWOP_SAVE_NONVOL (Offset=3, Reg=0x0F)
11801181
0x08, 0x00,
11811182
0x0F, 0x52, // UWOP_ALLOC_SMALL (Size = (2 * 8) + 8 = 24 bytes)
11821183
0x0B, 0x70, // UWOP_PUSH_NONVOL (Reg=0x0B)
1183-
1184+
11841185
// Exception handler RVA
11851186
0xC0, 0x1F, 0x00, 0x00, // __C_specific_handler
1186-
1187+
11871188
// Scope count
11881189
0x02, 0x00, 0x00, 0x00, // Scope table count = 2
1189-
1190+
11901191
// First C_SCOPE_TABLE entry
11911192
0x01, 0x15, 0x00, 0x00, // BeginAddress = 0x00001501
11921193
0x06, 0x16, 0x00, 0x00, // EndAddress = 0x00001606

src/pe/resource.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,14 @@ pub(crate) struct Utf16String<'a>(&'a [u8]);
3434

3535
impl<'a> Utf16String<'a> {
3636
/// Converts underlying bytes into an owned [String].
37-
pub fn to_string(&self) -> Option<String> {
37+
pub(crate) fn to_string(&self) -> Option<String> {
3838
to_utf16_string(self.0)
3939
}
4040

4141
/// Returns the underlying bytes length. This includes null terminator.
42-
pub fn len(&self) -> usize {
42+
pub(crate) fn len(&self) -> usize {
4343
self.0.len()
4444
}
45-
46-
/// Returns the underlying bytes.
47-
pub fn bytes(&self) -> &'a [u8] {
48-
self.0
49-
}
5045
}
5146

5247
impl<'a> ctx::TryFromCtx<'a, scroll::Endian> for Utf16String<'a> {
@@ -190,20 +185,13 @@ impl<'a> ImageResourceDirectory {
190185
) -> error::Result<ResourceEntryIterator<'a>> {
191186
let bytes = bytes.pread_with::<&[u8]>(offset, self.entries_size())?;
192187

193-
Ok(ResourceEntryIterator {
194-
num_resources: self.count() as usize,
195-
data: bytes,
196-
})
188+
Ok(ResourceEntryIterator { data: bytes })
197189
}
198190
}
199191

200192
/// Iterator over [`ResourceData`]
201193
#[derive(Debug, Copy, Clone)]
202194
pub struct ResourceEntryIterator<'a> {
203-
/// Total number of ID entries and named entries
204-
///
205-
/// Must be equals to [`ImageResourceDirectory::number_of_named_entries`] + [`ImageResourceDirectory::number_of_id_entries`]
206-
num_resources: usize,
207195
/// Raw data of resource direcrory without [`ImageResourceDirectory`] and scoped to [`RESOURCE_ENTRY_SIZE`] * [`Self::num_resources`]
208196
data: &'a [u8],
209197
}
@@ -474,7 +462,6 @@ impl<'a> ResourceData<'a> {
474462
))
475463
})?;
476464

477-
let count = image_resource_directory.count() as usize;
478465
let offset = core::mem::size_of::<ImageResourceDirectory>();
479466
let size = image_resource_directory.entries_size();
480467
if offset > data.len() {
@@ -486,7 +473,6 @@ impl<'a> ResourceData<'a> {
486473
error::Error::Malformed(format!("Resource entry offset ({offset:#x}) out of bounds"))
487474
})?;
488475
let iterator = ResourceEntryIterator {
489-
num_resources: count,
490476
data: iterator_data,
491477
};
492478
let version_info =
@@ -519,7 +505,6 @@ impl<'a> ResourceData<'a> {
519505
let size = self.image_resource_directory.entries_size();
520506
// Safety: Panic-free is guaranteed here by Self::parse_with_opts
521507
ResourceEntryIterator {
522-
num_resources: self.count() as usize,
523508
data: &self.data[offset..offset + size],
524509
}
525510
}

0 commit comments

Comments
 (0)