From ee824f3b19247b042337e4f9fb9aaabd9b4c8e54 Mon Sep 17 00:00:00 2001
From: Alex Lyon <arcterus@mail.com>
Date: Wed, 7 Mar 2018 15:59:58 -0800
Subject: [PATCH] string: implement strndup and strnlen

---
 src/string/src/lib.rs | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/string/src/lib.rs b/src/string/src/lib.rs
index 4d1f6f56..4b091e39 100644
--- a/src/string/src/lib.rs
+++ b/src/string/src/lib.rs
@@ -87,8 +87,13 @@ pub extern "C" fn strcspn(s1: *const c_char, s2: *const c_char) -> c_ulong {
 
 #[no_mangle]
 pub unsafe extern "C" fn strdup(s1: *const c_char) -> *mut c_char {
+    strndup(s1, usize::MAX)
+}
+
+#[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 = strlen(s1) + 1;
+    let len = strnlen(s1, size) + 1;
 
     let buffer = stdlib::malloc(len) as *mut _;
     if buffer.is_null() {
@@ -110,7 +115,12 @@ pub extern "C" fn strerror(errnum: c_int) -> *mut c_char {
 
 #[no_mangle]
 pub unsafe extern "C" fn strlen(s: *const c_char) -> size_t {
-    platform::c_str(s).len() as size_t
+    strnlen(s, usize::MAX)
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn strnlen(s: *const c_char, size: usize) -> size_t {
+    platform::c_str_n(s, size).len() as size_t
 }
 
 #[no_mangle]
-- 
GitLab