Browse Source

str: faux_str_casecmp() can 'compare' NULL strings. Consider NULL string to be less than empty string

Serj Kalichev 2 years ago
parent
commit
23151fe9d5
1 changed files with 9 additions and 0 deletions
  1. 9 0
      faux/str/str.c

+ 9 - 0
faux/str/str.c

@@ -372,6 +372,15 @@ int faux_str_casecmp(const char *str1, const char *str2)
 	const char *p1 = str1;
 	const char *p2 = str2;
 
+	if (!p1 && !p2) // Empty strings are equal
+		return 0;
+
+	if (!p1) // Consider NULL string to be less then empty string
+		return -1;
+
+	if (!p2) // Consider NULL string to be less then empty string
+		return 1;
+
 	while (*p1 != '\0' && *p2 != '\0') {
 		int res = faux_str_cmp_chars(
 			faux_ctype_tolower(*p1), faux_ctype_tolower(*p2));