From 6441d619ba8984f469ecbd5e46358e982366b678 Mon Sep 17 00:00:00 2001 From: Tycho Sci Date: Mon, 14 Jan 2013 07:33:47 +0900 Subject: [PATCH] core: Use libc::readlink function properly The specification of readlink() says it's not guaranteed that the returned contents of the symbolic link is null-terminated. --- src/libcore/os.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/libcore/os.rs b/src/libcore/os.rs index 6ceb71d25ae26..f602b230f45e5 100644 --- a/src/libcore/os.rs +++ b/src/libcore/os.rs @@ -452,10 +452,19 @@ pub fn self_exe_path() -> Option { fn load_self() -> Option<~str> { unsafe { use libc::funcs::posix01::unistd::readlink; - do fill_charp_buf() |buf, sz| { + + let mut path_str = str::with_capacity(tmpbuf_sz); + let len = do str::as_c_str(path_str) |buf| { + let buf = buf as *mut c_char; do as_c_charp("/proc/self/exe") |proc_self_buf| { - readlink(proc_self_buf, buf, sz) != (-1 as ssize_t) + readlink(proc_self_buf, buf, tmpbuf_sz as size_t) } + }; + if len == -1 { + None + } else { + str::raw::set_len(&mut path_str, len as uint); + Some(path_str) } } }