From 3270120d16405169c0ceef02961fb6033bcde735 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 9 Oct 2019 19:00:54 +0200 Subject: [PATCH] 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]