From eba477358f272e99caf6d3cf99ca345f38eab037 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 26 Aug 2026 08:19:04 +0200 Subject: {run,read}rom: validate the path from --nasm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miquel Sabaté Solà --- crates/readrom/src/main.rs | 17 +++++++++++++---- crates/runrom/src/main.rs | 19 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/crates/readrom/src/main.rs b/crates/readrom/src/main.rs index 8d70e16..f461fe9 100644 --- a/crates/readrom/src/main.rs +++ b/crates/readrom/src/main.rs @@ -16,7 +16,7 @@ struct Args { all: bool, disassemble: Option, mapping: Option, - nasm: Option, + nasm: Option, raw: bool, config: Option, } @@ -75,7 +75,16 @@ fn parse_arguments() -> Args { } }, "-n" | "--nasm" => match args.next() { - Some(a) => res.nasm = Some(a), + Some(a) => { + let pb = PathBuf::from(a.clone()); + if !pb.exists() { + die(format!("directory '{a}' does not exist")); + } + if !pb.is_dir() { + die(format!("path '{a}' does not point to a directory")); + } + res.nasm = Some(pb); + } None => die("you need to specify a file for the '-n/--nasm' flag".to_string()), }, "-r" | "--raw" => res.raw = true, @@ -336,7 +345,7 @@ fn parse_hex_value(address: &str) -> Option { fn do_disassemble( bytes: &[u8], address: Option<&str>, - nasm_path: &Option, + nasm_path: &Option, mut start: Option, mut end: Option, raw: bool, @@ -347,7 +356,7 @@ fn do_disassemble( // Fill up the 'addresses' and the 'memories' maps. if let Some(path) = nasm_path { - if let Ok(file) = File::open(PathBuf::from(path).join("addresses.txt")) { + if let Ok(file) = File::open(path.join("addresses.txt")) { let reader = BufReader::new(file); for line in reader.lines() { let line = line.map_err(|e| e.to_string())?; diff --git a/crates/runrom/src/main.rs b/crates/runrom/src/main.rs index 3cf8ae0..cd886a8 100644 --- a/crates/runrom/src/main.rs +++ b/crates/runrom/src/main.rs @@ -13,7 +13,7 @@ struct Args { file: String, start: Option, assume_function: bool, - nasm: Option, + nasm: Option, dump_memory: bool, until_address: u16, } @@ -72,7 +72,7 @@ fn parse_hex_argument(given: &str) -> Result { // Fetch the address mapping from the .nasm/addresses.txt file. You need to pass // the full 'path' to the .nasm/ directory for the project (i.e. the '-n/--nasm' // option). -fn fetch_addresses(path: PathBuf) -> Result, String> { +fn fetch_addresses(path: &PathBuf) -> Result, String> { let mut addresses: HashMap = HashMap::default(); if let Ok(file) = File::open(path.join("addresses.txt")) { @@ -100,7 +100,7 @@ fn fetch_addresses(path: PathBuf) -> Result, String> { // directory. fn parse_hex_or_reference( val: String, - nasm: &Option, + nasm: &Option, addresses: &mut HashMap, ) -> u16 { match parse_hex_argument(&val) { @@ -108,7 +108,7 @@ fn parse_hex_or_reference( Err(e) => match nasm { Some(nasm_path) => { if addresses.is_empty() { - *addresses = match fetch_addresses(PathBuf::from(nasm_path)) { + *addresses = match fetch_addresses(nasm_path) { Ok(addr) => addr, Err(err) => die(err), }; @@ -151,7 +151,16 @@ fn parse_arguments() -> Args { res.assume_function = true; } "-n" | "--nasm" => match args.next() { - Some(a) => res.nasm = Some(a), + Some(a) => { + let pb = PathBuf::from(a.clone()); + if !pb.exists() { + die(format!("directory '{a}' does not exist")); + } + if !pb.is_dir() { + die(format!("path '{a}' does not point to a directory")); + } + res.nasm = Some(pb); + } None => die("you need to specify a file for the '-n/--nasm' flag".to_string()), }, "--until-address" => { -- cgit v1.2.3