fix that Microsoft's _strnicmp crashes when a string is NULL although count == 0

Wed, 31 Dec 2025 15:25:30 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 31 Dec 2025 15:25:30 +0100
changeset 1693
c2d05cf1a062
parent 1692
56731bb98508
child 1694
a2757c6427cc

fix that Microsoft's _strnicmp crashes when a string is NULL although count == 0

src/string.c file | annotate | diff | comparison | revisions
tests/test_string.c file | annotate | diff | comparison | revisions
--- a/src/string.c	Wed Dec 31 15:11:12 2025 +0100
+++ b/src/string.c	Wed Dec 31 15:25:30 2025 +0100
@@ -41,7 +41,12 @@
 #include <ctype.h>
 
 #ifdef _WIN32
-#define cx_strcasecmp_impl _strnicmp
+static int cx_fixed_strnicmp(const char* s1, const char* s2, size_t count) {
+    // Microsoft's implementation crashes when count == 0 and either string is NULL
+    if (count == 0) return 0;
+    return _strnicmp(s1, s2, count);
+}
+#define cx_strcasecmp_impl cx_fixed_strnicmp
 #else
 #include <strings.h>
 #define cx_strcasecmp_impl strncasecmp
--- a/tests/test_string.c	Wed Dec 31 15:11:12 2025 +0100
+++ b/tests/test_string.c	Wed Dec 31 15:25:30 2025 +0100
@@ -343,7 +343,7 @@
 
     CX_TEST_DO {
         // the entire string
-        for (size_t i = 0; i < str.length; i++) {
+        for (off_t i = 0; i < str.length; i++) {
             CX_TEST_ASSERT(cx_strat(str, i) == str.ptr[i]);
         }
         // the entire string backwards

mercurial