From f4443c85e197993826ee810c99cec8bd577a23af Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Fri, 21 Jan 2022 18:12:45 -0800 Subject: [PATCH] Handle the null case in realpath The destination pointer might be null, in which case we are supposed to malloc some memory for it: https://man7.org/linux/man-pages/man3/realpath.3.html --- src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 393a7aa..3385b95 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,9 +24,15 @@ extern "C" fn posix_memalign( #[no_mangle] unsafe extern "C" fn realpath( path: *const libc::c_char, - resolved_path: *mut libc::c_char, + mut resolved_path: *mut libc::c_char, ) -> *mut libc::c_char { - libc::memcpy(resolved_path as _, path as _, libc::strlen(path)); + let path_len = libc::strlen(path); + + if resolved_path.is_null() { + resolved_path = libc::malloc(path_len + 1) as _; + } + + libc::strncpy(resolved_path as _, path as _, path_len + 1); resolved_path }