summaryrefslogtreecommitdiff
path: root/libc/string
diff options
context:
space:
mode:
Diffstat (limited to 'libc/string')
-rw-r--r--libc/string/Makefile3
-rw-r--r--libc/string/bug-strcasestr1.c39
-rw-r--r--libc/string/bug-strchr1.c14
-rw-r--r--libc/string/bug-strstr1.c26
-rw-r--r--libc/string/str-two-way.h37
-rw-r--r--libc/string/strcasestr.c4
-rw-r--r--libc/string/strstr.c4
-rw-r--r--libc/string/test-strcasestr.c28
-rw-r--r--libc/string/test-strchr.c27
-rw-r--r--libc/string/test-strstr.c47
10 files changed, 121 insertions, 108 deletions
diff --git a/libc/string/Makefile b/libc/string/Makefile
index 096147dcc..de3ce2734 100644
--- a/libc/string/Makefile
+++ b/libc/string/Makefile
@@ -59,8 +59,7 @@ tests := tester inl-tester noinl-tester testcopy test-ffs \
bug-strncat1 bug-strspn1 bug-strpbrk1 tst-bswap \
tst-strtok tst-strfry \
bug-strtok1 $(addprefix test-,$(strop-tests)) \
- tst-strxfrm2 tst-endian tst-svc2 bug-strstr1 \
- bug-strcasestr1 bug-strchr1 tst-strtok_r
+ tst-strxfrm2 tst-endian tst-svc2 tst-strtok_r
tests-$(OPTION_EGLIBC_ENVZ) += bug-envz1
tests-$(OPTION_EGLIBC_LOCALE_CODE) \
+= tst-strxfrm bug-strcoll1
diff --git a/libc/string/bug-strcasestr1.c b/libc/string/bug-strcasestr1.c
deleted file mode 100644
index e8334d709..000000000
--- a/libc/string/bug-strcasestr1.c
+++ /dev/null
@@ -1,39 +0,0 @@
-/* Test for non-submitted strcasestr bug.
- Copyright (C) 2012 Free Software Foundation, Inc.
- This file is part of the GNU C Library.
-
- The GNU C Library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
-
- The GNU C Library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with the GNU C Library; if not, see
- <http://www.gnu.org/licenses/>. */
-
-#include <stdio.h>
-#include <string.h>
-
-#define TEST_FUNCTION do_test ()
-static int
-do_test (void)
-{
- const char haystack[] = "AOKB";
- const char needle[] = "OK";
- const char *sub = strcasestr (haystack, needle);
-
- if (sub == NULL)
- {
- fprintf (stderr, "BUG: didn't find \"%s\" in \"%s\"\n", needle, haystack);
- return 1;
- }
-
- return 0;
-}
-
-#include "../test-skeleton.c"
diff --git a/libc/string/bug-strchr1.c b/libc/string/bug-strchr1.c
deleted file mode 100644
index 21155d8a7..000000000
--- a/libc/string/bug-strchr1.c
+++ /dev/null
@@ -1,14 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-
-static int
-do_test (void)
-{
- char s[] __attribute__((aligned(16))) = "\xff";
- char *p = strchr (s, '\xfe');
- printf ("%p\n", p);
- return p != NULL;
-}
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
diff --git a/libc/string/bug-strstr1.c b/libc/string/bug-strstr1.c
deleted file mode 100644
index 889de1295..000000000
--- a/libc/string/bug-strstr1.c
+++ /dev/null
@@ -1,26 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-
-int main (int argc, char** argv)
-{
- const char haystack[] =
- "F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD_C3_A7_20_EF_BF_BD";
-
- const char needle[] =
- "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD";
-
- const char* sub = strstr (haystack, needle);
-
- if (sub != NULL)
- {
- int j;
-
- fprintf (stderr, "BUG: expected NULL, got:\n%s\n%s\n", sub, needle);
- for (j = 0; needle[j] != '\0'; ++j)
- putchar (needle[j] == sub[j] ? ' ' : '^');
- puts ("");
- return 1;
- }
-
- return 0;
-}
diff --git a/libc/string/str-two-way.h b/libc/string/str-two-way.h
index 59609b868..d2572da40 100644
--- a/libc/string/str-two-way.h
+++ b/libc/string/str-two-way.h
@@ -75,18 +75,17 @@
# define CMP_FUNC memcmp
#endif
-#ifndef AVAILABLE1
-# define AVAILABLE1(h, h_l, j, n_l) AVAILABLE (h, h_l, j, n_l)
-#endif
-#ifndef AVAILABLE2
-# define AVAILABLE2(h, h_l, j, n_l) (1)
+/* Check for end-of-line in strstr and strcasestr routines.
+ We piggy-back matching procedure for detecting EOL where possible,
+ and use AVAILABLE macro otherwise. */
+#ifndef CHECK_EOL
+# define CHECK_EOL (0)
#endif
+
+/* Return NULL if argument is '\0'. */
#ifndef RET0_IF_0
# define RET0_IF_0(a) /* nothing */
#endif
-#ifndef AVAILABLE1_USES_J
-# define AVAILABLE1_USES_J (1)
-#endif
/* Perform a critical factorization of NEEDLE, of length NEEDLE_LEN.
Return the index of the first byte in the right half, and set
@@ -283,11 +282,23 @@ two_way_short_needle (const unsigned char *haystack, size_t haystack_len,
and use an optimized first-character loop. */
unsigned char needle_suffix = CANON_ELEMENT (needle[suffix]);
+#if CHECK_EOL
+ /* We start matching from the SUFFIX'th element, so make sure we
+ don't hit '\0' before that. */
+ if (haystack_len < suffix + 1
+ && !AVAILABLE (haystack, haystack_len, 0, suffix + 1))
+ return NULL;
+#endif
+
/* The two halves of needle are distinct; no extra memory is
required, and any mismatch results in a maximal shift. */
period = MAX (suffix, needle_len - suffix) + 1;
j = 0;
- while (AVAILABLE1 (haystack, haystack_len, j, needle_len))
+ while (1
+#if !CHECK_EOL
+ && AVAILABLE (haystack, haystack_len, j, needle_len)
+#endif
+ )
{
unsigned char haystack_char;
const unsigned char *pneedle;
@@ -298,13 +309,13 @@ two_way_short_needle (const unsigned char *haystack, size_t haystack_len,
!= (haystack_char = CANON_ELEMENT (*phaystack++)))
{
RET0_IF_0 (haystack_char);
-#if AVAILABLE1_USES_J
+#if CHECK_EOL
++j;
#endif
continue;
}
-#if !AVAILABLE1_USES_J
+#if !CHECK_EOL
/* Calculate J if it wasn't kept up-to-date in the first-character
loop. */
j = phaystack - &haystack[suffix] - 1;
@@ -346,8 +357,10 @@ two_way_short_needle (const unsigned char *haystack, size_t haystack_len,
else
j += i - suffix + 1;
- if (!AVAILABLE2 (haystack, haystack_len, j, needle_len))
+#if CHECK_EOL
+ if (!AVAILABLE (haystack, haystack_len, j, needle_len))
break;
+#endif
phaystack = &haystack[suffix + j];
}
diff --git a/libc/string/strcasestr.c b/libc/string/strcasestr.c
index 9467b7a75..6aaf45f3a 100644
--- a/libc/string/strcasestr.c
+++ b/libc/string/strcasestr.c
@@ -43,10 +43,8 @@
#define AVAILABLE(h, h_l, j, n_l) \
(!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
&& ((h_l) = (j) + (n_l)))
-#define AVAILABLE1(h, h_l, j, n_l) (true)
-#define AVAILABLE2(h, h_l, j, n_l) AVAILABLE (h, h_l, j, n_l)
+#define CHECK_EOL (1)
#define RET0_IF_0(a) if (!a) goto ret0
-#define AVAILABLE1_USES_J (0)
#define CANON_ELEMENT(c) TOLOWER (c)
#define CMP_FUNC(p1, p2, l) \
__strncasecmp ((const char *) (p1), (const char *) (p2), l)
diff --git a/libc/string/strstr.c b/libc/string/strstr.c
index cfed77119..d74cb4697 100644
--- a/libc/string/strstr.c
+++ b/libc/string/strstr.c
@@ -35,10 +35,8 @@
#define AVAILABLE(h, h_l, j, n_l) \
(!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
&& ((h_l) = (j) + (n_l)))
-#define AVAILABLE1(h, h_l, j, n_l) (true)
-#define AVAILABLE2(h, h_l, j, n_l) AVAILABLE (h, h_l, j, n_l)
+#define CHECK_EOL (1)
#define RET0_IF_0(a) if (!a) goto ret0
-#define AVAILABLE1_USES_J (0)
#include "str-two-way.h"
#undef strstr
diff --git a/libc/string/test-strcasestr.c b/libc/string/test-strcasestr.c
index 6c1a87861..fc0185863 100644
--- a/libc/string/test-strcasestr.c
+++ b/libc/string/test-strcasestr.c
@@ -57,8 +57,9 @@ IMPL (simple_strcasestr, 0)
IMPL (strcasestr, 1)
-static void
-do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
+static int
+check_result (impl_t *impl, const char *s1, const char *s2,
+ char *exp_result)
{
char *result = CALL (impl, s1, s2);
if (result != exp_result)
@@ -66,8 +67,16 @@ do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
error (0, 0, "Wrong result in function %s %s %s", impl->name,
result, exp_result);
ret = 1;
- return;
+ return -1;
}
+ return 0;
+}
+
+static void
+do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
+{
+ if (check_result (impl, s1, s2, exp_result) < 0)
+ return;
if (HP_TIMING_AVAIL)
{
@@ -136,12 +145,25 @@ do_test (size_t align1, size_t align2, size_t len1, size_t len2,
putchar ('\n');
}
+static void
+check1 (void)
+{
+ const char s1[] = "AOKB";
+ const char s2[] = "OK";
+ char *exp_result;
+
+ exp_result = stupid_strcasestr (s1, s2);
+ FOR_EACH_IMPL (impl, 0)
+ check_result (impl, s1, s2, exp_result);
+}
static int
test_main (void)
{
test_init ();
+ check1 ();
+
printf ("%23s", "");
FOR_EACH_IMPL (impl, 0)
printf ("\t%s", impl->name);
diff --git a/libc/string/test-strchr.c b/libc/string/test-strchr.c
index e544aa715..161ac458c 100644
--- a/libc/string/test-strchr.c
+++ b/libc/string/test-strchr.c
@@ -79,8 +79,8 @@ IMPL (stupid_STRCHR, 0)
IMPL (simple_STRCHR, 0)
IMPL (STRCHR, 1)
-static void
-do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
+static int
+check_result (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
{
CHAR *res = CALL (impl, s, c);
if (res != exp_res)
@@ -88,8 +88,16 @@ do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
error (0, 0, "Wrong result in function %s %#x %p %p", impl->name,
c, res, exp_res);
ret = 1;
- return;
+ return -1;
}
+ return 0;
+}
+
+static void
+do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
+{
+ if (check_result (impl, s, c, exp_res) < 0)
+ return;
if (HP_TIMING_AVAIL)
{
@@ -224,6 +232,17 @@ do_random_tests (void)
}
}
+static void
+check1 (void)
+{
+ char s[] __attribute__((aligned(16))) = "\xff";
+ char c = '\xfe';
+ char *exp_result = stupid_STRCHR (s, c);
+
+ FOR_EACH_IMPL (impl, 0)
+ check_result (impl, s, c, exp_result);
+}
+
int
test_main (void)
{
@@ -231,6 +250,8 @@ test_main (void)
test_init ();
+ check1 ();
+
printf ("%20s", "");
FOR_EACH_IMPL (impl, 0)
printf ("\t%s", impl->name);
diff --git a/libc/string/test-strstr.c b/libc/string/test-strstr.c
index 6be460168..d4c0efc5b 100644
--- a/libc/string/test-strstr.c
+++ b/libc/string/test-strstr.c
@@ -55,8 +55,9 @@ IMPL (simple_strstr, 0)
IMPL (strstr, 1)
-static void
-do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
+static int
+check_result (impl_t *impl, const char *s1, const char *s2,
+ char *exp_result)
{
char *result = CALL (impl, s1, s2);
if (result != exp_result)
@@ -64,9 +65,18 @@ do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
error (0, 0, "Wrong result in function %s %s %s", impl->name,
result, exp_result);
ret = 1;
- return;
+ return -1;
}
+ return 0;
+}
+
+static void
+do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
+{
+ if (check_result (impl, s1, s2, exp_result) < 0)
+ return;
+
if (HP_TIMING_AVAIL)
{
hp_timing_t start __attribute ((unused));
@@ -133,12 +143,43 @@ do_test (size_t align1, size_t align2, size_t len1, size_t len2,
putchar ('\n');
}
+static void
+check1 (void)
+{
+ const char s1[] =
+ "F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD_C3_A7_20_EF_BF_BD";
+ const char s2[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD";
+ char *exp_result;
+
+ exp_result = stupid_strstr (s1, s2);
+ FOR_EACH_IMPL (impl, 0)
+ check_result (impl, s1, s2, exp_result);
+}
+
+static void
+check2 (void)
+{
+ const char s1[] = ", enable_static, \0, enable_shared, ";
+ char *exp_result;
+ char *s2 = (void *) buf1 + page_size - 18;
+
+ strcpy (s2, s1);
+ exp_result = stupid_strstr (s1, s1 + 18);
+ FOR_EACH_IMPL (impl, 0)
+ {
+ check_result (impl, s1, s1 + 18, exp_result);
+ check_result (impl, s2, s1 + 18, exp_result);
+ }
+}
static int
test_main (void)
{
test_init ();
+ check1 ();
+ check2 ();
+
printf ("%23s", "");
FOR_EACH_IMPL (impl, 0)
printf ("\t%s", impl->name);