[PATCH] Fix a memory leak in object-attribute evaluation

Jim Meyering jim at meyering.net
Wed Oct 13 04:49:09 UTC 2010


Pete Zaitcev wrote:
> On Tue, 12 Oct 2010 12:37:26 -0400
> Jeff Darcy <jdarcy at redhat.com> wrote:
>
>> -	copy = strdup((char *)right);
>> +	copy = xstrdup((char *)right);
>
> Ouch, I noticed that missed hooking xmalloc for auto_stop().
> Maybe atexit() is not as evil as it looks.

atexit is your friend.
Without it, maintaining exit-related code is too error prone,
whenever there is more than one exit point.

In fact, I already have a patch that adds an atexit handler to makes it so
./iwhd --version > /dev/full does not ignore the write failure.
Currently it ignores failure:

    $ ./iwhd --version > /dev/full; echo $?
    0
    $

With the tiny patch (using gnulib's closeout module), it does this:

    $ ./iwhd --version > /dev/full; echo $?
    cat: write error: No space left on device
    1
    $

For reference, here's the actual patch
(but without the accompanying infrastructure):

>From 55e4a68f073a3504ca1c4f4e6c198233b40ce6e3 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Fri, 1 Oct 2010 15:33:48 +0200
Subject: [PATCH] use gnulib's closeout module

---
 rest.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/rest.c b/rest.c
index db2a4b5..d066f0e 100644
--- a/rest.c
+++ b/rest.c
@@ -37,6 +37,7 @@
 #include "iwh.h"
 #include "progname.h"
 #include "meta.h"
+#include "closeout.h"
 #include "backend.h"
 #include "setup.h"
 #include "replica.h"
@@ -1908,6 +1909,7 @@ main (int argc, char **argv)
 	char			*port_tmp;

 	set_program_name (argv[0]);
+	atexit (close_stdout);

 	for (;;) switch (getopt_long(argc,argv,"c:d:m:p:v",my_options,NULL)) {
 	case 'c':
--
1.7.3.1.104.gc752e


More information about the iwhd-devel mailing list