[Freeswitch-svn] [commit] r6080 - freeswitch/trunk/src

Freeswitch SVN mikej at freeswitch.org
Mon Oct 29 02:16:42 EDT 2007


Author: mikej
Date: Mon Oct 29 02:16:41 2007
New Revision: 6080

Modified:
   freeswitch/trunk/src/switch_utils.c

Log:
replace switch_stristr implementation with one that shouldn't segfault.

Modified: freeswitch/trunk/src/switch_utils.c
==============================================================================
--- freeswitch/trunk/src/switch_utils.c	(original)
+++ freeswitch/trunk/src/switch_utils.c	Mon Oct 29 02:16:41 2007
@@ -199,24 +199,46 @@
 
 SWITCH_DECLARE(const char *) switch_stristr(const char *str, const char *instr)
 {
-	switch_size_t score = strlen(str), x = 0;
-	const char *a = str, *b = instr, *p = NULL;
-	while(*a && *b) {
-		if (tolower(*b) == tolower(*a)) {
-			if (++x == score) {
-				return b - x + 1;
-			}
-			a++;
-		} else {
-			a = str;
-			p = b+1;
-			x = 0;
+/*
+** Rev History:  16/07/97  Greg Thayer		Optimized
+**               07/04/95  Bob Stout		ANSI-fy
+**               02/03/94  Fred Cole		Original
+**               09/01/03  Bob Stout		Bug fix (lines 40-41) per Fred Bulback
+**
+** Hereby donated to public domain.
+*/
+
+	const char *pptr, *sptr, *start;
+
+	if (!str || !instr)
+		return NULL;
+
+	for (start = str; *start; start++) {
+		/* find start of pattern in string */
+		for ( ; ((*start) && (toupper(*start) != toupper(*instr))); start++);
+
+		if (!*start)
+			return NULL;
+
+		pptr = instr;
+		sptr = start;
+
+		while (toupper(*sptr) == toupper(*pptr)) {
+			sptr++;
+			pptr++;
+
+			/* if end of pattern then pattern was found */
+			if (!*pptr)
+				return (start);
+
+			if (!*sptr)
+				return NULL;
 		}
-		b++;
 	}
 	return NULL;
 }
 
+
 SWITCH_DECLARE(switch_status_t) switch_find_local_ip(char *buf, int len, int family)
 {
 	switch_status_t status = SWITCH_STATUS_FALSE;



More information about the Freeswitch-svn mailing list