This is the mail archive of the libc-alpha@sourceware.cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Ad PR libc/1730: glibc bug in memmem()


>>>>> Greg Hudson writes:

Greg> The code part of the patch looks fine.  (I would probably avoid even
Greg> computing the invalid pointer value in the haystack_len < needle_len,
Greg> but I hardly think that's important.)

>> * sysdeps/generic/memmem.c (memmem): Check for invalid parameter.

Greg> I don't think having haystack_len < needle_len constitutes an invalid
Greg> parameter.

>> +  /* Sanity check, otherwise the loop will search through the whole
>> +     memory.  */

Greg> This comment might be confusing to some readers.  On most C
Greg> implementations, most of the time, the loop will not search through
Greg> the whole memory.  The particular case that bit me was that haystack
Greg> was NULL (the result of malloc(0)), so the invalid computed pointer
Greg> wrapped around to a very high value.

Greg> Replacing "will" with "might" would probably fix the situation.

Greg> Sorry to nitpick so much.  Thanks for your time.
I agree with Mark here ;-).  You're welcome.

Any problems with the appended patch?  I've optimized even a little
bit.

Andreas

	* 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>.

============================================================
Index: sysdeps/generic/memmem.c
--- sysdeps/generic/memmem.c	1998/01/30 13:39:22	1.10
+++ sysdeps/generic/memmem.c	2000/05/17 16:05:25
@@ -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
@@ -37,6 +37,11 @@
     /* The first occurrence of the empty string is deemed to occur at
        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] &&

-- 
 Andreas Jaeger
  SuSE Labs aj@suse.de
   private aj@arthur.inka.de


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]