mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] write strcasestr
@ 2013-02-21 15:40 Strake
  2013-02-21 15:44 ` Strake
  2013-02-21 16:50 ` Rich Felker
  0 siblings, 2 replies; 3+ messages in thread
From: Strake @ 2013-02-21 15:40 UTC (permalink / raw)
  To: musl

From 6a228eaf8bc2b3830c56bd2c8caa2ffa44245531 Mon Sep 17 00:00:00 2001
From: Strake <strake888@gmail.com>
Date: Thu, 21 Feb 2013 10:41:09 -0500
Subject: [PATCH] write strcasestr

---
 src/string/strcasestr.c | 36 ++++++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)

diff --git a/src/string/strcasestr.c b/src/string/strcasestr.c
index f1cb0e8..c67c606 100644
--- a/src/string/strcasestr.c
+++ b/src/string/strcasestr.c
@@ -1,7 +1,35 @@
+#include <stdlib.h>
 #include <string.h>
+#include <wchar.h>
+#include <stdio.h>

-char *strcasestr(const char *h, const char *n)
-{
-	//FIXME!
-	return strstr(h, n);
+static void mbstolower (char *s) {
+	mbstate_t mbst;
+	wchar_t wx;
+	
+	while (*s) {
+		memset (&mbst, 0, sizeof (mbstate_t));
+		if (mbrtowc (&wx, s, MB_CUR_MAX, &mbst) < 0) break;
+		wx = towlower (wx);
+		memset (&mbst, 0, sizeof (mbstate_t));
+		s += wcrtomb (s, wx, &mbst);
+	}
+}
+
+char *strcasestr (const char *s, const char *t) {
+	char *x, *y, *z;
+	
+	x = strdup (s); if (!x) return 0;
+	y = strdup (t); if (!y) return 0;
+
+	mbstolower (x);
+	mbstolower (y);
+	
+	z = strstr (x, y);
+	if (z) s += z - x; else s = 0;
+	
+	free (x);
+	free (y);
+	
+	return s;
 }
-- 
1.7.11.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] write strcasestr
  2013-02-21 15:40 [PATCH] write strcasestr Strake
@ 2013-02-21 15:44 ` Strake
  2013-02-21 16:50 ` Rich Felker
  1 sibling, 0 replies; 3+ messages in thread
From: Strake @ 2013-02-21 15:44 UTC (permalink / raw)
  To: musl

Sorry, included stdio for fault-finding and forgot to exclude it again.


From 60b96f3e18f75d989c09ab4d6dc3958558d766fb Mon Sep 17 00:00:00 2001
From: Strake <strake888@gmail.com>
Date: Thu, 21 Feb 2013 10:41:09 -0500
Subject: [PATCH] write strcasestr

---
 src/string/strcasestr.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)

diff --git a/src/string/strcasestr.c b/src/string/strcasestr.c
index f1cb0e8..b21a7b5 100644
--- a/src/string/strcasestr.c
+++ b/src/string/strcasestr.c
@@ -1,7 +1,34 @@
+#include <stdlib.h>
 #include <string.h>
+#include <wchar.h>

-char *strcasestr(const char *h, const char *n)
-{
-	//FIXME!
-	return strstr(h, n);
+static void mbstolower (char *s) {
+	mbstate_t mbst;
+	wchar_t wx;
+	
+	while (*s) {
+		memset (&mbst, 0, sizeof (mbstate_t));
+		if (mbrtowc (&wx, s, MB_CUR_MAX, &mbst) < 0) break;
+		wx = towlower (wx);
+		memset (&mbst, 0, sizeof (mbstate_t));
+		s += wcrtomb (s, wx, &mbst);
+	}
+}
+
+char *strcasestr (const char *s, const char *t) {
+	char *x, *y, *z;
+	
+	x = strdup (s); if (!x) return 0;
+	y = strdup (t); if (!y) return 0;
+
+	mbstolower (x);
+	mbstolower (y);
+	
+	z = strstr (x, y);
+	if (z) s += z - x; else s = 0;
+	
+	free (x);
+	free (y);
+	
+	return s;
 }
-- 
1.7.11.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] write strcasestr
  2013-02-21 15:40 [PATCH] write strcasestr Strake
  2013-02-21 15:44 ` Strake
@ 2013-02-21 16:50 ` Rich Felker
  1 sibling, 0 replies; 3+ messages in thread
From: Rich Felker @ 2013-02-21 16:50 UTC (permalink / raw)
  To: musl

On Thu, Feb 21, 2013 at 10:40:38AM -0500, Strake wrote:
> >From 6a228eaf8bc2b3830c56bd2c8caa2ffa44245531 Mon Sep 17 00:00:00 2001
> From: Strake <strake888@gmail.com>
> Date: Thu, 21 Feb 2013 10:41:09 -0500
> Subject: [PATCH] write strcasestr
> 
> ---
>  src/string/strcasestr.c | 36 ++++++++++++++++++++++++++++++++----
>  1 file changed, 32 insertions(+), 4 deletions(-)
> 
> diff --git a/src/string/strcasestr.c b/src/string/strcasestr.c
> index f1cb0e8..c67c606 100644
> --- a/src/string/strcasestr.c
> +++ b/src/string/strcasestr.c
> @@ -1,7 +1,35 @@
> +#include <stdlib.h>
>  #include <string.h>
> +#include <wchar.h>
> +#include <stdio.h>
> 
> -char *strcasestr(const char *h, const char *n)
> -{
> -	//FIXME!
> -	return strstr(h, n);
> +static void mbstolower (char *s) {
> +	mbstate_t mbst;
> +	wchar_t wx;
> +	
> +	while (*s) {
> +		memset (&mbst, 0, sizeof (mbstate_t));
> +		if (mbrtowc (&wx, s, MB_CUR_MAX, &mbst) < 0) break;

The return value of mbrtowc is unsigned so it can never be < 0.

> +		wx = towlower (wx);
> +		memset (&mbst, 0, sizeof (mbstate_t));
> +		s += wcrtomb (s, wx, &mbst);
> +	}
> +}
> +
> +char *strcasestr (const char *s, const char *t) {
> +	char *x, *y, *z;
> +	
> +	x = strdup (s); if (!x) return 0;
> +	y = strdup (t); if (!y) return 0;

This function does not have a way to report failure, so it cannot
fail; therefore, allocating storage is not an option. Returning zero
means the needle is not present in the haystack, not that the
implementation ran out of resources to look for it.

For a concrete example of where false negatives would be a security
problem, see Busybox's use of the function.

> +	z = strstr (x, y);
> +	if (z) s += z - x; else s = 0;

This logic is also incorrect. It assumes the length of the lowercase
characters is the same as the length of the corresponding uppercase
ones; this is in general false. Actually, this means your algorithm
has another serious security flaw: it may overrun the allocated
buffer when performing the case conversion.

Rich


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-02-21 16:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-21 15:40 [PATCH] write strcasestr Strake
2013-02-21 15:44 ` Strake
2013-02-21 16:50 ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).