Skip to content

Commit

Permalink
Merge pull request #322 from mstange/dont-crash-on-empty-cmdline
Browse files Browse the repository at this point in the history
Don't panic when /proc/{pid}/cmdline is empty.
  • Loading branch information
mstange authored Jul 31, 2024
2 parents e091d19 + 0b4e189 commit 75a8971
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions samply/src/linux/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,8 @@ pub fn read_string_lossy<P: AsRef<Path>>(path: P) -> std::io::Result<String> {
}

fn get_process_cmdline(pid: u32) -> std::io::Result<(String, Vec<String>)> {
let cmdline_bytes = std::fs::read(format!("/proc/{pid}/cmdline"))?;
let path = format!("/proc/{pid}/cmdline");
let cmdline_bytes = std::fs::read(&path)?;
let mut remaining_bytes = &cmdline_bytes[..];
let mut cmdline = Vec::new();
while let Some(nul_byte_pos) = memchr::memchr(b'\0', remaining_bytes) {
Expand All @@ -755,7 +756,13 @@ fn get_process_cmdline(pid: u32) -> std::io::Result<(String, Vec<String>)> {
cmdline.push(String::from_utf8_lossy(arg_slice).to_string());
}

let exe_arg = &cmdline[0];
let exe_arg = cmdline.first().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Empty cmdline at {path}"),
)
})?;

let exe_name = match exe_arg.rfind('/') {
Some(pos) => exe_arg[pos + 1..].to_string(),
None => exe_arg.to_string(),
Expand Down

0 comments on commit 75a8971

Please sign in to comment.