[Bf-blender-cvs] [70fcecc] master: Fix BLI_strncasestr use with a single character

Campbell Barton noreply at git.blender.org
Thu Mar 24 13:32:32 CET 2016


Commit: 70fcecc1f788772ac0ecdbd647583afe995423bb
Author: Campbell Barton
Date:   Thu Mar 24 23:17:43 2016 +1100
Branches: master
https://developer.blender.org/rB70fcecc1f788772ac0ecdbd647583afe995423bb

Fix BLI_strncasestr use with a single character

===================================================================

M	source/blender/blenlib/intern/string.c
M	tests/gtests/blenlib/BLI_string_test.cc

===================================================================

diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c
index 2f67b0e..f62ffe9 100644
--- a/source/blender/blenlib/intern/string.c
+++ b/source/blender/blenlib/intern/string.c
@@ -534,13 +534,24 @@ char *BLI_strncasestr(const char *s, const char *find, size_t len)
 
 	if ((c = *find++) != 0) {
 		c = tolower(c);
-		do {
+		if (len > 1) {
 			do {
-				if ((sc = *s++) == 0)
-					return (NULL);
-				sc = tolower(sc);
-			} while (sc != c);
-		} while (BLI_strncasecmp(s, find, len - 1) != 0);
+				do {
+					if ((sc = *s++) == 0)
+						return NULL;
+					sc = tolower(sc);
+				} while (sc != c);
+			} while (BLI_strncasecmp(s, find, len - 1) != 0);
+		}
+		else {
+			{
+				do {
+					if ((sc = *s++) == 0)
+						return NULL;
+					sc = tolower(sc);
+				} while (sc != c);
+			}
+		}
 		s--;
 	}
 	return ((char *)s);
diff --git a/tests/gtests/blenlib/BLI_string_test.cc b/tests/gtests/blenlib/BLI_string_test.cc
index 5559b8d..17a4b5e 100644
--- a/tests/gtests/blenlib/BLI_string_test.cc
+++ b/tests/gtests/blenlib/BLI_string_test.cc
@@ -451,6 +451,9 @@ TEST(string, StringStrncasestr)
 	res = BLI_strncasestr(str_test0, "", 0);
 	EXPECT_EQ(str_test0, res);
 
+	res = BLI_strncasestr(str_test0, " ", 1);
+	EXPECT_EQ(str_test0 + 6, res);
+
 	res = BLI_strncasestr(str_test0, "her", 3);
 	EXPECT_EQ(str_test0 + 7, res);




More information about the Bf-blender-cvs mailing list