Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* memchr bug?  Request for enlightenment (lowercase 'e')
@ 2001-03-26 20:13 michael.donahue
  2001-03-26 20:37 ` Chris Eleveld (Sector 7 Group)
  0 siblings, 1 reply; 3+ messages in thread
From: michael.donahue @ 2001-03-26 20:13 UTC (permalink / raw)
  To: axp-list

While doing some code testing with Electric Fence, I came across the
following curiosity, on a stock RedHat 6.2/AXP system.

Consider the short test program:

   #include <string.h>
   #include <stdlib.h>

   #define TESTSTRING "12345678"

   int main()
   {
     char *p = (char *)malloc(strlen(TESTSTRING)+1);
     strcpy(p,TESTSTRING);
     memchr(p,'\0',79);
     free(p);
     return 0;
   }

When I compile and run I get

   $ gcc memchr-test.c -lefence
   $ ./a.out

     Electric Fence 2.0.5 Copyright (C) 1987-1998 Bruce Perens.
   Segmentation fault

It appears that memchr is reading memory past the end of the malloc'ed
region.  I don't know if this is actually a problem or not, but I don't
see this behavior on Linux/x86 or Solaris.  Incidentally, I can replace
'\0' above with '5', '6', '7' or '8' with the same results.  OTOH, '4'
is okay, and I see no segfault with strlen().

So, is this a bug?  Has it been fixed in newer libc?

BTW, the *printf family uses memchr in its %.ns conversions, e.g.,
the following code exhibits the same behavior:

   #include <stdio.h>
   #include <string.h>
   #include <stdlib.h>

   #define TESTSTRING "12345678"

   int main()
   {
     char *p = (char *)malloc(strlen(TESTSTRING)+1);
     strcpy(p,TESTSTRING);
     printf("%.79s\n",p);
     free(p);
     return 0;
   }

-m

-- 
Michael J. Donahue
National Institute of Standards and Technology
Mathematical & Computational Sciences Division
100 Bureau Dr Stop 8910
Gaithersburg, MD 20899-8910
email: michael.donahue@nist.gov
voice: (301) 975-5424
  fax: (301) 990-4127
  web: http://math.nist.gov/~MDonahue/




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

* Re: memchr bug?  Request for enlightenment (lowercase 'e')
  2001-03-26 20:13 memchr bug? Request for enlightenment (lowercase 'e') michael.donahue
@ 2001-03-26 20:37 ` Chris Eleveld (Sector 7 Group)
  2001-03-26 21:55   ` michael.donahue
  0 siblings, 1 reply; 3+ messages in thread
From: Chris Eleveld (Sector 7 Group) @ 2001-03-26 20:37 UTC (permalink / raw)
  To: axp-list

	Actually this may not be.  You allocate 9 bytes.  Memchr is probably
implemented using full word or longword fetches.  As such it can be optimized
to 3 32bit fetches and bit shifts.  Or even 2 64 bit on an alpha.  Since you
lied and siad your buffer was 79 bytes long.  If on the other hand you used
9 instead of 79 you could call this a bug.

On Mon, Mar 26, 2001 at 03:13:26PM -0500, michael.donahue@nist.gov wrote:
> While doing some code testing with Electric Fence, I came across the
> following curiosity, on a stock RedHat 6.2/AXP system.
> 
> Consider the short test program:
> 
>    #include <string.h>
>    #include <stdlib.h>
> 
>    #define TESTSTRING "12345678"
> 
>    int main()
>    {
>      char *p = (char *)malloc(strlen(TESTSTRING)+1);
>      strcpy(p,TESTSTRING);
>      memchr(p,'\0',79);
>      free(p);
>      return 0;
>    }
> 
> When I compile and run I get
> 
>    $ gcc memchr-test.c -lefence
>    $ ./a.out
> 
>      Electric Fence 2.0.5 Copyright (C) 1987-1998 Bruce Perens.
>    Segmentation fault
> 
> It appears that memchr is reading memory past the end of the malloc'ed
> region.  I don't know if this is actually a problem or not, but I don't
> see this behavior on Linux/x86 or Solaris.  Incidentally, I can replace
> '\0' above with '5', '6', '7' or '8' with the same results.  OTOH, '4'
> is okay, and I see no segfault with strlen().
> 
> So, is this a bug?  Has it been fixed in newer libc?
> 
> BTW, the *printf family uses memchr in its %.ns conversions, e.g.,
> the following code exhibits the same behavior:
> 
>    #include <stdio.h>
>    #include <string.h>
>    #include <stdlib.h>
> 
>    #define TESTSTRING "12345678"
> 
>    int main()
>    {
>      char *p = (char *)malloc(strlen(TESTSTRING)+1);
>      strcpy(p,TESTSTRING);
>      printf("%.79s\n",p);
>      free(p);
>      return 0;
>    }
> 
> -m
> 
> -- 
> Michael J. Donahue
> National Institute of Standards and Technology
> Mathematical & Computational Sciences Division
> 100 Bureau Dr Stop 8910
> Gaithersburg, MD 20899-8910
> email: michael.donahue@nist.gov
> voice: (301) 975-5424
>   fax: (301) 990-4127
>   web: http://math.nist.gov/~MDonahue/
> 
> 
> 
> _______________________________________________
> Axp-list mailing list
> Axp-list@redhat.com
> https://listman.redhat.com/mailman/listinfo/axp-list




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

* Re: memchr bug?  Request for enlightenment (lowercase 'e')
  2001-03-26 20:37 ` Chris Eleveld (Sector 7 Group)
@ 2001-03-26 21:55   ` michael.donahue
  0 siblings, 0 replies; 3+ messages in thread
From: michael.donahue @ 2001-03-26 21:55 UTC (permalink / raw)
  To: axp-list

Chris Eleveld writes:
>	Actually this may not be.  You allocate 9 bytes.  Memchr is probably
>implemented using full word or longword fetches.  As such it can be optimized
>to 3 32bit fetches and bit shifts.  Or even 2 64 bit on an alpha.  Since you
>lied and siad your buffer was 79 bytes long.  If on the other hand you used
>9 instead of 79 you could call this a bug.

I believe you are right about the longword fetches.  I'm a little
surprised I haven't see this on other platforms, though.

As regards the length of the buffer, the point is that this call may be
buried inside a utility routine where I don't know the length of the
incoming buffer.  I have code similar to the printf example; I don't
recall seeing an admonition about buffer sizes in the printf docs.  In
fact, the Linux fprintf man page says:

   If a precision is given, no null character need be present; if the
   precision is not specified, or is greater than the size of the array,
   the array must contain a terminating NUL character.

The memchr man page states:

   The memchr() function scans the first n bytes of the memory area
   pointed to by s for the character c.  The first byte to match c
   (interpreted as an unsigned character) stops the operation.

This seems unclear.  Does it scan n bytes, or does it stop at a match?
Or is that left undefined?

For myself, I can code around this as necessary, but if it is
technically a bug in memchr(), it should be fixed for portability
reasons.

Just my $0.02,

-m

-- 
Michael J. Donahue
National Institute of Standards and Technology
Mathematical & Computational Sciences Division
100 Bureau Dr Stop 8910
Gaithersburg, MD 20899-8910
email: michael.donahue@nist.gov
voice: (301) 975-5424
  fax: (301) 990-4127
  web: http://math.nist.gov/~MDonahue/




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

end of thread, other threads:[~2001-03-26 21:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2001-03-26 20:13 memchr bug? Request for enlightenment (lowercase 'e') michael.donahue
2001-03-26 20:37 ` Chris Eleveld (Sector 7 Group)
2001-03-26 21:55   ` michael.donahue

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox