[PATCH] avoid malloc(0) in getdents

Mike Frysinger vapier at gentoo.org
Wed Oct 7 23:48:50 UTC 2009


On Wednesday 07 October 2009 19:32:39 Dmitry V. Levin wrote:
> On Wed, Oct 07, 2009 at 05:25:01AM -0400, Mike Frysinger wrote:
> > When getdents finishes processing, it returns 0.  Strace uses this to
> > then try and do malloc(0), but on some systems this will always return
> > NULL. Since the code won't read the pointer in question if len is 0, then
> > don't abort on the malloc(0) == NULL case.
> 
> When len == 0, the buffer allocated by malloc(0) is not used anyway, so
> there are no need to malloc(0) here even on regular systems where
> malloc(0) allocates memory, right?

yes, this is true.  it would be nice to write it like:
	if (len && (buf = malloc(len)) == NULL) {
but then gcc whines that buf might be used uninitialized, which is why i wrote 
it the way i did (less code change that way).

it could be written like so:
	buf = len ? malloc(len) : NULL;
	if (len && !buf) {

the point really is to make sure the rest of the code is still executed even 
when (len == 0 && buf == NULL) so that you get the nice decoded output of "/* 
0 entries */" rather than an ugly hex address of the struct.  any other issues 
(whether to call malloc(0)/etc...) dont really matter to me.
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20091007/be63a417/attachment.bin>


More information about the Strace-devel mailing list