Nmap Development mailing list archives

[PATCH] Rewrite strcasestr()


From: Kris Katterjohn <kjak () ispwest com>
Date: Thu, 02 Mar 2006 20:08:22 -0600

The attached patch simplifies strcasestr() in nbase/nbase_str.c to be similar
to our str[n]casecmp() functions.

-rwxr-xr-x  1 kjak root 1964705 2006-03-02 19:46 nmap*
-rwxr-xr-x  1 kjak root 1964985 2006-03-02 19:38 nmap.orig*

-rw-r--r--  1 kjak root 5104 2006-03-02 19:46 nbase/nbase_str.o
-rw-r--r--  1 kjak root 5320 2006-03-02 19:38 nbase/nbase_str.o.orig

I copied my strcasestr() to another file and did like 8 tests and printed the
details about the return value from mine and the one in my library (I had to
define _GNU_SOURCE to get it). The results all matched, so it seems to work
fine and it's smaller than the current one in nmap. In case you're wondering,
I undefined HAVE_STRCASESTR when compiling the above when testing, because (I
didn't check where) somewhere USE_GNU is defined and gives me my library's
strcasestr(). So the above is actually using the current nmap implementation
of strcasestr() and mine.

I also added an if in there to check for strlen(needle) > strlen(haystack) so
the rest won't be executed if we know it won't match; though this probably
won't happen much, if at all.

I do have one question, though. Why is it okay for strcasestr() to crash if
needle is NULL? I just copied it over to my version because there's probably a
good reason behind it that I'm not seeing.

Thanks,
Kris Katterjohn
--- nbase/nbase_str.c.orig      2006-03-02 19:12:06.000000000 -0600
+++ nbase/nbase_str.c   2006-03-02 19:48:09.000000000 -0600
@@ -103,37 +103,40 @@
 #include <string.h>
 
 #ifndef HAVE_STRCASESTR
-char *strcasestr(const char *haystack, const char *pneedle) {
-char buf[512];
-unsigned int needlelen;
-const char *p;
-char *needle, *q, *foundto;
-
-/* Should crash if !pneedle -- this is OK */
-if (!*pneedle) return (char *) haystack;
-if (!haystack) return NULL;
-
-needlelen = (unsigned int) strlen(pneedle);
- if (needlelen >= sizeof(buf)) {
-   needle = (char *) malloc(needlelen + 1);
- } else needle = buf;
- p = pneedle; q = needle;
- while((*q++ = tolower(*p++)))
-   ;
- p = haystack - 1; foundto = needle;
- while(*++p) {
-   if(tolower(*p) == *foundto) {
-     if(!*++foundto) {
-       /* Yeah, we found it */
-       if (needlelen >= sizeof(buf))
-         free(needle);
-       return (char *) (p - needlelen + 1);
-     }
-   } else foundto = needle;
- }
- if (needlelen >= sizeof(buf))
-   free(needle);
- return NULL;
+char *strcasestr(const char *haystack, const char *needle)
+{
+       char *h, *n, *ret;
+       int x;
+
+       if (!haystack)
+               return NULL;
+
+       /* Should crash if !needle -- this is OK */
+       if (!*needle)
+               return (char *) haystack;
+
+       /* needle can't be in haystack if it's longer than it */
+       if (strlen(needle) > strlen(haystack))
+               return NULL;
+
+       h = malloc(strlen(haystack) + 1);
+       n = malloc(strlen(needle) + 1);
+
+       for (x = 0; x < strlen(haystack) + 1; x++)
+               h[x] = tolower(haystack[x]);
+       for (x = 0; x < strlen(needle) + 1; x++)
+               n[x] = tolower(needle[x]);
+
+       ret = strstr(h, n);
+
+       /* If n was found in h, set ret to needle's position in haystack */
+       if (ret)
+               ret = (char *) &haystack[ret - h];
+
+       free(h);
+       free(n);
+
+       return ret;
 }
 #endif
 


_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev

Current thread: