Skip to content

Commit 75a8971

Browse files
authored
Merge pull request #322 from mstange/dont-crash-on-empty-cmdline
Don't panic when /proc/{pid}/cmdline is empty.
2 parents e091d19 + 0b4e189 commit 75a8971

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

samply/src/linux/profiler.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,8 @@ pub fn read_string_lossy<P: AsRef<Path>>(path: P) -> std::io::Result<String> {
746746
}
747747

748748
fn get_process_cmdline(pid: u32) -> std::io::Result<(String, Vec<String>)> {
749-
let cmdline_bytes = std::fs::read(format!("/proc/{pid}/cmdline"))?;
749+
let path = format!("/proc/{pid}/cmdline");
750+
let cmdline_bytes = std::fs::read(&path)?;
750751
let mut remaining_bytes = &cmdline_bytes[..];
751752
let mut cmdline = Vec::new();
752753
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<String>)> {
755756
cmdline.push(String::from_utf8_lossy(arg_slice).to_string());
756757
}
757758

758-
let exe_arg = &cmdline[0];
759+
let exe_arg = cmdline.first().ok_or_else(|| {
760+
std::io::Error::new(
761+
std::io::ErrorKind::Other,
762+
format!("Empty cmdline at {path}"),
763+
)
764+
})?;
765+
759766
let exe_name = match exe_arg.rfind('/') {
760767
Some(pos) => exe_arg[pos + 1..].to_string(),
761768
None => exe_arg.to_string(),

0 commit comments

Comments
 (0)