From 32771eecc673d8ce832b4da7aade5ac7ab7efdc5 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Tue, 6 Sep 2022 19:21:56 -0700 Subject: [PATCH] Rearranged order of trimming and parsing --- procinfo/src/macos.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/procinfo/src/macos.rs b/procinfo/src/macos.rs index 485bd8752..4c2aeedae 100644 --- a/procinfo/src/macos.rs +++ b/procinfo/src/macos.rs @@ -218,14 +218,19 @@ fn parse_exe_and_argv_sysctl(buf: Vec) -> Option<(PathBuf, Vec)> { ptr = &ptr[std::mem::size_of::()..]; fn consume_cstr(ptr: &mut &[u8]) -> Option { - // Trim out trailing nulls - let not_nul = ptr.iter().position(|&c| c != 0)?; - *ptr = ptr.get(not_nul..)?; - - // Then parse to the end of the null terminated string + // Parse to the end of a null terminated string let nul = ptr.iter().position(|&c| c == 0)?; let s = String::from_utf8_lossy(&ptr[0..nul]).to_owned().to_string(); *ptr = ptr.get(nul + 1..)?; + + // Find the position of the first non null byte. `.position()` + // will return None if we run off the end. + if let Some(not_nul) = ptr.iter().position(|&c| c != 0) { + // If there are no trailing nulls, not_nul will be 0 + // and this call will be a noop + *ptr = ptr.get(not_nul..)?; + } + Some(s) }