Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/uu/pmap/src/pmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if let Some(range) = matches.get_one::<String>(options::RANGE) {
match range.matches(',').count() {
0 => {
let address = u64::from_str_radix(range, 16).map_err(|_| {
let address = parse_address(range).map_err(|_| {
USimpleError::new(1, format!("failed to parse argument: '{range}'"))
})?;
pmap_config.range_low = address;
Expand All @@ -112,14 +112,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
pmap_config.range_low = if low.is_empty() {
0
} else {
u64::from_str_radix(low, 16).map_err(|_| {
parse_address(low).map_err(|_| {
USimpleError::new(1, format!("failed to parse argument: '{range}'"))
})?
};
pmap_config.range_high = if high.is_empty() {
u64::MAX
} else {
u64::from_str_radix(high, 16).map_err(|_| {
parse_address(high).map_err(|_| {
USimpleError::new(1, format!("failed to parse argument: '{range}'"))
})?
};
Expand Down Expand Up @@ -433,6 +433,16 @@ fn output_device_format(pid: &str, pmap_config: &PmapConfig) -> Result<(), Error
Ok(())
}

/// Parse an address for --range: hexadecimal, with or without a 0x prefix,
/// as procps-ng takes it.
fn parse_address(address: &str) -> Result<u64, std::num::ParseIntError> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since you are touching the three callers, could parse_address return the USimpleError directly?
the same map_err is there 3 times

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 2e673d2: parse_address takes the whole argument for the message and returns the USimpleError, so the three callers just use ?.

let digits = address
.strip_prefix("0x")
.or_else(|| address.strip_prefix("0X"))
.unwrap_or(address);
u64::from_str_radix(digits, 16)
}

pub fn uu_app() -> Command {
Command::new(env!("CARGO_PKG_NAME"))
.version(crate_version!())
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_pmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ fn test_range_arg() {
"ffffffffffffffff,",
",ffffffffffffffff",
"ffffffffffffffff,ffffffffffffffff",
// 0x prefix
"0xc00fee",
"0Xc00fee",
"0xc00,0xfee",
",0xffffffffffffffff",
] {
new_ucmd!().arg(&pid_s).arg(opt).arg(arg).succeeds();
}
Expand All @@ -397,6 +402,10 @@ fn test_range_arg() {
"someinvalidtext,",
",someinvalidtext",
"someinvalidtext,someinvalidtext",
// 0x prefix without digits
"0x",
"0x,",
"0x0xc00fee",
// too large value (> u64)
"f0000000000000000",
"f0000000000000000,f0000000000000000",
Expand Down
Loading