[PATCH] avoid malloc(0) in getdents

Mike Frysinger vapier at gentoo.org
Wed Oct 7 09:25:01 UTC 2009


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.

* file.c (sys_getdents, sys_getdents64): Ignore malloc(0) == NULL.

Signed-off-by: Mike Frysinger <vapier at gentoo.org>
---
 file.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

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





More information about the Strace-devel mailing list