From 0b4e189c06169e3a362b5b41a76c4cbbba972811 Mon Sep 17 00:00:00 2001 From: Markus Stange Date: Wed, 31 Jul 2024 10:52:23 -0400 Subject: [PATCH] Don't panic when /proc/{pid}/cmdline is empty. Fixes #311. --- samply/src/linux/profiler.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/samply/src/linux/profiler.rs b/samply/src/linux/profiler.rs index 97f8eec0..2858d299 100644 --- a/samply/src/linux/profiler.rs +++ b/samply/src/linux/profiler.rs @@ -746,7 +746,8 @@ pub fn read_string_lossy>(path: P) -> std::io::Result { } fn get_process_cmdline(pid: u32) -> std::io::Result<(String, Vec)> { - 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) { @@ -755,7 +756,13 @@ fn get_process_cmdline(pid: u32) -> std::io::Result<(String, Vec)> { 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(),