浏览代码

Fix strcasecmp

jD91mZM2 6 年之前
父节点
当前提交
9c57c222f6
共有 3 个文件被更改,包括 15 次插入9 次删除
  1. 12 6
      src/strings/src/lib.rs
  2. 1 1
      src/sys_socket/cbindgen.toml
  3. 2 2
      tests/strings.c

+ 12 - 6
src/strings/src/lib.rs

@@ -109,10 +109,13 @@ pub unsafe extern "C" fn strcasecmp(mut first: *const c_char, mut second: *const
         }
 
         first = first.offset(1);
-        second = first.offset(1);
+        second = second.offset(1);
     }
-    // Both strings ended at the same time too
-    (*first == *second) as c_int
+    // Both strings didn't end with NUL bytes
+    if *first != *second {
+        return -1;
+    }
+    0
 }
 #[no_mangle]
 pub unsafe extern "C" fn strncasecmp(
@@ -136,9 +139,12 @@ pub unsafe extern "C" fn strncasecmp(
         }
 
         first = first.offset(1);
-        second = first.offset(1);
+        second = second.offset(1);
         n -= 1;
     }
-    // Both strings ended at the same time too
-    (n == 0 || *first == *second) as c_int
+    // Both strings didn't end with NUL bytes (unless we reached the limit)
+    if n != 0 && *first != *second {
+        return -1;
+    }
+    0
 }

+ 1 - 1
src/sys_socket/cbindgen.toml

@@ -1,4 +1,4 @@
-sys_includes = ["stddef.h", "sys/types.h"]
+sys_includes = ["stddef.h", "stdint.h", "sys/types.h"]
 include_guard = "_SYS_SOCKET_H"
 style = "Tag"
 language = "C"

+ 2 - 2
tests/strings.c

@@ -11,8 +11,8 @@ int main() {
     bcopy("hi", new, 3); // include nul byte
 
     assert(!strcasecmp("hi", new));
-    assert(!strcasecmp("hi", "HI"));
-    assert(!strncasecmp("hi", "HIHI", 2));
+    assert(!strcasecmp("hello", "HEllO"));
+    assert(!strncasecmp("hello", "Hello World", 5));
 
     bzero(new, 1);
     assert(*new == 0);