This is the mail archive of the
gdb@sources.redhat.com
mailing list for the GDB project.
Re: memset (0, 0, 0);
- From: Geoff Keating <geoffk at geoffk dot org>
- To: "Thomas,Stephen" <stephen dot thomas at superh dot com>
- Cc: <gdb at sources dot redhat dot com>, <newlib at sources dot redhat dot com>, <bug-glibc at gnu dot org>, "McGoogan,Sean" <sean dot mcgoogan at superh dot com>
- Date: 07 Apr 2003 10:17:38 -0700
- Subject: Re: memset (0, 0, 0);
- References: <9FF3133289A7A84E81E2ED8F5E56B379604384@sh-uk-ex01.uk.w2k.superh.com>
"Thomas,Stephen" <stephen dot thomas at superh dot com> writes:
> Hi,
>
> gdb appears to call memset(0,0,0) from build_regcache() in gdb/regcache.c. I can't really claim to understand how this works, but this function appears to get called 3 times during gdb initialization:
>
> static void build_regcache (void)
> {
> ...
> int sizeof_register_valid;
> ...
> sizeof_register_valid = ((NUM_REGS + NUM_PSEUDO_REGS) * sizeof (*register_valid));
> register_valid = xmalloc (sizeof_register_valid);
> memset (register_valid, 0, sizeof_register_valid);
> }
>
> On the 1st time of calling, none of the gdbarch stuff is set up, so NUM_REGS = NUM_PSEUDO_REGS = 0. So xmalloc gets called with size=0. That returns 0 as the 'address', which gets passed to memset. I guess this just works OK on other architectures (it does on x86 anyway).
>
> Easy enough to fix I suppose, but is that really the point?
xmalloc is never supposed to return 0, and in fact, there's code to
prevent it:
if (size == 0)
size = 1;
newmem = malloc (size);
if (!newmem)
xmalloc_failed (size);
return (newmem);
xmalloc_failed finishes with
xexit (1);
so xmalloc should never return NULL.
--
- Geoffrey Keating <geoffk at geoffk dot org>