aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog4
-rw-r--r--sysdeps/generic/memmem.c7
2 files changed, 10 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 086f8e4..91e2c19 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -21,6 +21,10 @@
* string/strings.h: Add pure and const attributes if possible.
+ * sysdeps/generic/memmem.c (memmem): Check arguments to avoid
+ possibly searching through the whole memory.
+ Closes PR libc/1730, reported by Greg Hudson <ghudson@mit.edu>.
+
2000-05-17 Jakub Jelinek <jakub@redhat.com>
* sysdeps/generic/dl-cache.h (_DL_CACHE_DEFAULT_ID): Only define if
diff --git a/sysdeps/generic/memmem.c b/sysdeps/generic/memmem.c
index 9e32f51..e9b2708 100644
--- a/sysdeps/generic/memmem.c
+++ b/sysdeps/generic/memmem.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 92, 93, 94, 96, 97, 98 Free Software Foundation, Inc.
+/* Copyright (C) 1991,92,93,94,96,97,98,2000 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
@@ -38,6 +38,11 @@ memmem (haystack, haystack_len, needle, needle_len)
the beginning of the string. */
return (void *) haystack;
+ /* Sanity check, otherwise the loop might search through the whole
+ memory. */
+ if (__builtin_expect (haystack_len < needle_len, 0))
+ return NULL;
+
for (begin = (const char *) haystack; begin <= last_possible; ++begin)
if (begin[0] == ((const char *) needle)[0] &&
!memcmp ((const void *) &begin[1],