Skip to content

Commit 6441d61

Browse files
committed
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.
1 parent 802d475 commit 6441d61

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/libcore/os.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,19 @@ pub fn self_exe_path() -> Option<Path> {
452452
fn load_self() -> Option<~str> {
453453
unsafe {
454454
use libc::funcs::posix01::unistd::readlink;
455-
do fill_charp_buf() |buf, sz| {
455+
456+
let mut path_str = str::with_capacity(tmpbuf_sz);
457+
let len = do str::as_c_str(path_str) |buf| {
458+
let buf = buf as *mut c_char;
456459
do as_c_charp("/proc/self/exe") |proc_self_buf| {
457-
readlink(proc_self_buf, buf, sz) != (-1 as ssize_t)
460+
readlink(proc_self_buf, buf, tmpbuf_sz as size_t)
458461
}
462+
};
463+
if len == -1 {
464+
None
465+
} else {
466+
str::raw::set_len(&mut path_str, len as uint);
467+
Some(path_str)
459468
}
460469
}
461470
}

0 commit comments

Comments
 (0)