From 3270120d16405169c0ceef02961fb6033bcde735 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 9 Oct 2019 19:00:54 +0200 Subject: [PATCH 1/3] strncasecmp() compares the given number of characters but not after a 0-byte --- mmime/src/other.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/mmime/src/other.rs b/mmime/src/other.rs index 20cd0ad93..a15d8275b 100644 --- a/mmime/src/other.rs +++ b/mmime/src/other.rs @@ -8,7 +8,7 @@ use crate::mailmime::types_helper::*; pub(crate) use libc::{ calloc, close, free, isalpha, isdigit, malloc, memcmp, memcpy, memmove, memset, realloc, - strcpy, strlen, strncmp, strncpy, + strcpy, strlen, strncmp, strncpy, strnlen, }; pub(crate) unsafe fn strcasecmp(s1: *const libc::c_char, s2: *const libc::c_char) -> libc::c_int { @@ -40,8 +40,14 @@ pub(crate) unsafe fn strncasecmp( // s1 and s2 might not be null terminated. - let s1_slice = std::slice::from_raw_parts(s1 as *const u8, n); - let s2_slice = std::slice::from_raw_parts(s2 as *const u8, n); + let s1_slice = std::slice::from_raw_parts( + s1 as *const u8, + std::cmp::min(n, strnlen(s1 as *const i8, n)), + ); + let s2_slice = std::slice::from_raw_parts( + s2 as *const u8, + std::cmp::min(n, strnlen(s2 as *const i8, n)), + ); let s1 = std::ffi::CStr::from_bytes_with_nul_unchecked(s1_slice) .to_string_lossy() @@ -1693,6 +1699,14 @@ mod tests { 4, ) }); + + assert_eq!(0, unsafe { + strncasecmp( + CString::new("hell").unwrap().as_ptr(), + CString::new("Hell").unwrap().as_ptr(), + 100_000_000, + ) + }); } #[test] From 2c98e91276d4555aecaa429bfdd61d9d95476d8f Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Thu, 10 Oct 2019 00:22:50 +0200 Subject: [PATCH 2/3] target comments of @link2xt --- mmime/src/other.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mmime/src/other.rs b/mmime/src/other.rs index a15d8275b..19ebbfdd3 100644 --- a/mmime/src/other.rs +++ b/mmime/src/other.rs @@ -42,11 +42,11 @@ pub(crate) unsafe fn strncasecmp( let s1_slice = std::slice::from_raw_parts( s1 as *const u8, - std::cmp::min(n, strnlen(s1 as *const i8, n)), + strnlen(s1 as *const i8, n), ); let s2_slice = std::slice::from_raw_parts( s2 as *const u8, - std::cmp::min(n, strnlen(s2 as *const i8, n)), + strnlen(s2 as *const i8, n), ); let s1 = std::ffi::CStr::from_bytes_with_nul_unchecked(s1_slice) From bc99d9d196e3da89192f43a2ada042fe357e9371 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Thu, 10 Oct 2019 00:39:35 +0200 Subject: [PATCH 3/3] cargo fmt --- mmime/src/other.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/mmime/src/other.rs b/mmime/src/other.rs index 19ebbfdd3..40f295764 100644 --- a/mmime/src/other.rs +++ b/mmime/src/other.rs @@ -40,14 +40,8 @@ pub(crate) unsafe fn strncasecmp( // s1 and s2 might not be null terminated. - let s1_slice = std::slice::from_raw_parts( - s1 as *const u8, - strnlen(s1 as *const i8, n), - ); - let s2_slice = std::slice::from_raw_parts( - s2 as *const u8, - strnlen(s2 as *const i8, n), - ); + let s1_slice = std::slice::from_raw_parts(s1 as *const u8, strnlen(s1 as *const i8, n)); + let s2_slice = std::slice::from_raw_parts(s2 as *const u8, strnlen(s2 as *const i8, n)); let s1 = std::ffi::CStr::from_bytes_with_nul_unchecked(s1_slice) .to_string_lossy()