Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
From: <michael.donahue@nist.gov>
To: axp-list@redhat.com
Subject: memchr bug?  Request for enlightenment (lowercase 'e')
Date: Mon, 26 Mar 2001 15:13:26 -0500	[thread overview]
Message-ID: <15039.41702.809052.794191@fuggles.cam.nist.gov> (raw)

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/




             reply	other threads:[~2001-03-26 20:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-26 20:13 michael.donahue [this message]
2001-03-26 20:37 ` memchr bug? Request for enlightenment (lowercase 'e') Chris Eleveld (Sector 7 Group)
2001-03-26 21:55   ` michael.donahue
read-only archive

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