]> granicus.if.org Git - strace/commitdiff
Avoid malloc(0) in getdents parsers
authorMike Frysinger <vapier@gentoo.org>
Thu, 8 Oct 2009 00:41:56 +0000 (20:41 -0400)
committerDmitry V. Levin <ldv@altlinux.org>
Thu, 8 Oct 2009 14:08:57 +0000 (14:08 +0000)
On end of directory, getdents returns 0.  This return value is used 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 bother calling malloc(0) and set the pointer to NULL ourself.
* file.c (sys_getdents, sys_getdents64): Avoid malloc(0) call.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
file.c

diff --git a/file.c b/file.c
index c6e3b52b928ca9591132aa1c29f0894be4489588..4a2512f4c7611666dbec1421cbd84a01b3ff0c7e 100644 (file)
--- a/file.c
+++ b/file.c
@@ -2337,7 +2337,8 @@ sys_getdents(struct tcb *tcp)
                return 0;
        }
        len = tcp->u_rval;
-       if ((buf = malloc(len)) == NULL) {
+       buf = len ? malloc(len) : NULL;
+       if (len && !buf) {
                tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
                fprintf(stderr, "out of memory\n");
                return 0;
@@ -2420,7 +2421,8 @@ sys_getdents64(struct tcb *tcp)
                return 0;
        }
        len = tcp->u_rval;
-       if ((buf = malloc(len)) == NULL) {
+       buf = len ? malloc(len) : NULL;
+       if (len && !buf) {
                tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
                fprintf(stderr, "out of memory\n");
                return 0;