From: Bastien Nocera Date: Thu, 28 Aug 2008 12:53:59 +0000 (+0000) Subject: Bug 548612 – g_strstr_len() should use memmem when available X-Git-Url: http://git.openbox.org/?a=commitdiff_plain;h=f84518e35b81f39c075df3563d86e494af836778;p=dana%2Fcg-glib.git Bug 548612 – g_strstr_len() should use memmem when available 2008-08-28 Bastien Nocera Bug 548612 – g_strstr_len() should use memmem when available * glib/tests/strfuncs.c (test_strstr): * tests/string-test.c (main): Patch by Paolo Borelli to move the tests to the right place, and add more tests * glib/gstrfuncs.c (g_strstr_len): Fix problem with memmem ignoring nul-terminators in strings, and using the haystack_len instead svn path=/trunk/; revision=7409 --- diff --git a/ChangeLog b/ChangeLog index 24089ff3..b00ca1aa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2008-08-28 Bastien Nocera + + Bug 548612 – g_strstr_len() should use memmem when available + + * glib/tests/strfuncs.c (test_strstr): + * tests/string-test.c (main): Patch by Paolo Borelli + to move the tests to the right place, + and add more tests + + * glib/gstrfuncs.c (g_strstr_len): Fix problem with memmem ignoring + nul-terminators in strings, and using the haystack_len instead + 2008-08-28 Bastien Nocera Bug 548612 – g_strstr_len() should use memmem when available diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c index a1e8c4c4..7c5e56d4 100644 --- a/glib/gstrfuncs.c +++ b/glib/gstrfuncs.c @@ -2603,7 +2603,10 @@ g_strstr_len (const gchar *haystack, else { #ifdef HAVE_MEMMEM - return memmem (haystack, haystack_len, needle, strlen (needle)); + size_t len; + + len = MIN(haystack_len, strlen (haystack)); + return memmem (haystack, len, needle, strlen (needle)); #else const gchar *p = haystack; gsize needle_len = strlen (needle); diff --git a/glib/tests/strfuncs.c b/glib/tests/strfuncs.c index 4915b400..a6a5f7ff 100644 --- a/glib/tests/strfuncs.c +++ b/glib/tests/strfuncs.c @@ -556,10 +556,19 @@ test_strstr (void) res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar"); g_assert (res == NULL); + res = g_strstr_len (haystack, 3, "Bar"); + g_assert (res == NULL); + res = g_strstr_len (haystack, 6, ""); g_assert (res == haystack); + g_assert_cmpstr (res, ==, "FooBarFooBarFoo"); res = g_strstr_len (haystack, 6, "Bar"); + g_assert (res == haystack + 3); + g_assert_cmpstr (res, ==, "BarFooBarFoo"); + + res = g_strstr_len (haystack, -1, "Bar"); + g_assert (res == haystack + 3); g_assert_cmpstr (res, ==, "BarFooBarFoo"); /* strrstr */ @@ -571,8 +580,10 @@ test_strstr (void) res = g_strrstr (haystack, ""); g_assert (res == haystack); + g_assert_cmpstr (res, ==, "FooBarFooBarFoo"); res = g_strrstr (haystack, "Bar"); + g_assert (res == haystack + 9); g_assert_cmpstr (res, ==, "BarFoo"); /* strrstr_len */ @@ -582,9 +593,26 @@ test_strstr (void) res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar"); g_assert (res == NULL); + res = g_strrstr_len (haystack, 3, "Bar"); + g_assert (res == NULL); + res = g_strrstr_len (haystack, 14, "BarFoo"); + g_assert (res == haystack + 3); g_assert_cmpstr (res, ==, "BarFooBarFoo"); + res = g_strrstr_len (haystack, 15, "BarFoo"); + g_assert (res == haystack + 9); + g_assert_cmpstr (res, ==, "BarFoo"); + + res = g_strrstr_len (haystack, -1, "BarFoo"); + g_assert (res == haystack + 9); + g_assert_cmpstr (res, ==, "BarFoo"); + + /* test case for strings with \0 in the middle */ + *(haystack + 7) = '\0'; + res = g_strstr_len (haystack, 15, "BarFoo"); + g_assert (res == NULL); + g_free (haystack); } diff --git a/tests/string-test.c b/tests/string-test.c index d8aa728d..7da4128f 100644 --- a/tests/string-test.c +++ b/tests/string-test.c @@ -307,11 +307,6 @@ main (int argc, g_assert (strcmp (tmp_string, "b a") == 0); g_free (tmp_string); - tmp_string = g_strdup (GLIB_TEST_STRING); - g_assert (g_strstr_len (tmp_string, 4, "rado") == NULL); - g_assert (g_strstr_len (tmp_string, -1, "rado") == tmp_string + 5); - g_free (tmp_string); - return 0; }