diff --git a/src/errno/src/lib.rs b/src/errno/src/lib.rs index 8a28cb6d3e9c956858cd3923c12c357deded209d..3cb5cd036c4cdd178d54ab15f1fc25d18aa9c124 100644 --- a/src/errno/src/lib.rs +++ b/src/errno/src/lib.rs @@ -162,5 +162,5 @@ pub enum Errno { // Operation would block (may be the same value as [EAGAIN]) EWOULDBLOCK, // Cross-device link - EXDEV + EXDEV, } diff --git a/src/string/src/lib.rs b/src/string/src/lib.rs index 744e5411a9bafbe5be415e5492fc0a6dea2c3171..f8a6f4250943d3d4e0d8408b49b7250f61174df7 100644 --- a/src/string/src/lib.rs +++ b/src/string/src/lib.rs @@ -2,9 +2,9 @@ #![no_std] +extern crate errno; extern crate platform; extern crate stdlib; -extern crate errno; use platform::types::*; use errno::*; @@ -94,10 +94,10 @@ pub unsafe extern "C" fn strdup(s1: *const c_char) -> *mut c_char { #[no_mangle] pub unsafe extern "C" fn strndup(s1: *const c_char, size: usize) -> *mut c_char { - // the "+ 1" is to account for the NUL byte - let len = strnlen(s1, size) + 1; + let len = strnlen(s1, size); - let buffer = stdlib::malloc(len) as *mut c_char; + // the "+ 1" is to account for the NUL byte + let buffer = stdlib::malloc(len + 1) as *mut c_char; if buffer.is_null() { platform::errno = Errno::ENOMEM as c_int; } else { @@ -105,6 +105,7 @@ pub unsafe extern "C" fn strndup(s1: *const c_char, size: usize) -> *mut c_char for i in 0..len as isize { *buffer.offset(i) = *s1.offset(i); } + *buffer.offset(len as isize) = 0; } buffer