Skip to content

Commit 8613c41

Browse files
committed
feat: TryFrom<MMap> for File to allow more arbitrary inputs.
This is particularly useful for fuzzing, but can certainly be interesting for other low-level applications as well.
1 parent 0c4976a commit 8613c41

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

gix-commitgraph/src/file/init.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::path::PathBuf;
12
use std::{
23
convert::{TryFrom, TryInto},
34
path::Path,
@@ -62,24 +63,13 @@ impl File {
6263
pub fn at(path: impl AsRef<Path>) -> Result<File, Error> {
6364
Self::try_from(path.as_ref())
6465
}
65-
}
6666

67-
impl TryFrom<&Path> for File {
68-
type Error = Error;
69-
70-
fn try_from(path: &Path) -> Result<Self, Self::Error> {
71-
let data = std::fs::File::open(path)
72-
.and_then(|file| {
73-
// SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file.
74-
#[allow(unsafe_code)]
75-
unsafe {
76-
Mmap::map(&file)
77-
}
78-
})
79-
.map_err(|e| Error::Io {
80-
err: e,
81-
path: path.to_owned(),
82-
})?;
67+
/// A lower-level constructor which constructs a new instance directly from the mapping in `data`,
68+
/// assuming that it originated from `path`.
69+
///
70+
/// Note that `path` is only used for verification of the hash its basename contains, but otherwise
71+
/// is not of importance.
72+
pub fn new(data: memmap2::Mmap, path: PathBuf) -> Result<File, Error> {
8373
let data_size = data.len();
8474
if data_size < MIN_FILE_SIZE {
8575
return Err(Error::Corrupt(
@@ -240,13 +230,33 @@ impl TryFrom<&Path> for File {
240230
extra_edges_list_range,
241231
fan,
242232
oid_lookup_offset,
243-
path: path.to_owned(),
233+
path,
244234
hash_len: object_hash.len_in_bytes(),
245235
object_hash,
246236
})
247237
}
248238
}
249239

240+
impl TryFrom<&Path> for File {
241+
type Error = Error;
242+
243+
fn try_from(path: &Path) -> Result<Self, Self::Error> {
244+
let data = std::fs::File::open(path)
245+
.and_then(|file| {
246+
// SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file.
247+
#[allow(unsafe_code)]
248+
unsafe {
249+
Mmap::map(&file)
250+
}
251+
})
252+
.map_err(|e| Error::Io {
253+
err: e,
254+
path: path.to_owned(),
255+
})?;
256+
Self::new(data, path.to_owned())
257+
}
258+
}
259+
250260
// Copied from gix-odb/pack/index/init.rs
251261
fn read_fan(d: &[u8]) -> ([u32; FAN_LEN], usize) {
252262
let mut fan = [0; FAN_LEN];

0 commit comments

Comments
 (0)