std: allocate less memory in current_exe for OpenBSD#158183
Conversation
|
rustbot has assigned @Mark-Simulacrum. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
it seems a bit over engineered to me : |
|
Yes, but this isn't about an array of characters but of pointers, and |
| @@ -234,13 +234,19 @@ | |||
| unsafe { | |||
| let mut mib = [libc::CTL_KERN, libc::KERN_PROC_ARGS, libc::getpid(), libc::KERN_PROC_ARGV]; | |||
There was a problem hiding this comment.
Studying https://man.openbsd.org/sysctl.2#KERN_PROC_ARGS
...So uh is there a reason, since we have to make two sysctl calls, that we don't just ask for KERN_PROC_NARGV the first time?
...though "KERN_PROC_NARGV and KERN_PROC_NENV return the number of elements as an int in the argv or env array." has got to be the most curious phrasing possible for that.
| let mut argv = Vec::<*const libc::c_char>::with_capacity(argv_len as usize); | ||
| // ... allocate a buffer for it ... | ||
| let mut argv = | ||
| Vec::<*const libc::c_char>::with_capacity(argv_len / size_of::<*const libc::c_char>()); |
There was a problem hiding this comment.
Should we checked_div_exact and panic here just in case? I guess it seems a bit unlikely that this goes wrong (and would be a platform/libc bug anyway).
|
r=me if you're happy |
| return Err(io::const_error!(io::ErrorKind::Uncategorized, "no current exe available")); | ||
| } | ||
| let argv0 = CStr::from_ptr(argv[0]).to_bytes(); | ||
| if argv0[0] == b'.' || argv0.iter().any(|b| *b == b'/') { |
This bug was introduced back in 2f42ac4 when Alex ported the
current_exeimplementation from C to Rust.Vec::with_capacitymeasures capacity in the number of elements, butsysctlmeasures it in bytes, so we need to do some conversions.CC @semarie