From edb95246d4455ecb922892c5997946e475e7b9db Mon Sep 17 00:00:00 2001 From: jD91mZM2 Date: Sat, 13 Oct 2018 16:55:01 +0200 Subject: [PATCH] Fix bug in strncasecmp --- Cargo.toml | 2 +- src/header/strings/mod.rs | 8 ++++---- tests/strings.c | 4 ++++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4670d53fca..dfb97528db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ redox_syscall = { git = "https://gitlab.redox-os.org/redox-os/syscall.git", bran spin = "0.4" [features] -default = ["trace"] +#default = ["trace"] trace = [] [profile.dev] diff --git a/src/header/strings/mod.rs b/src/header/strings/mod.rs index d681f2223d..a0afc460d4 100644 --- a/src/header/strings/mod.rs +++ b/src/header/strings/mod.rs @@ -76,14 +76,14 @@ pub unsafe extern "C" fn strncasecmp( mut second: *const c_char, mut n: size_t, ) -> c_int { - while *first & !32 == *second & !32 { - if n == 0 || *first == 0 && *second == 0 { - return 0; + while n > 0 && (*first != 0 || *second != 0) { + if *first & !32 != *second & !32 { + return -1; } first = first.offset(1); second = second.offset(1); n -= 1; } - -1 + 0 } diff --git a/tests/strings.c b/tests/strings.c index 3f43175c78..1e46854883 100644 --- a/tests/strings.c +++ b/tests/strings.c @@ -11,8 +11,12 @@ int main() { bcopy("hi", new, 3); // include nul byte assert(!strcasecmp("hi", new)); + assert(strcasecmp("he", new)); assert(!strcasecmp("hello", "HEllO")); + assert(strcasecmp("hello", "HEllOo")); assert(!strncasecmp("hello", "Hello World", 5)); + assert(!strncasecmp("FLOOR0_1", "FLOOR0_1FLOOR4_1", 8)); + assert(strncasecmp("FL00RO_1", "FLOOR0_1FLOOR4_1", 8)); bzero(new, 1); assert(*new == 0);