On Wed, 2010-10-27 at 13:31 +0200, Karel Zak wrote: > On Wed, Oct 27, 2010 at 06:46:42AM -0300, Davidlohr Bueso wrote: > > From: Davidlohr Bueso <dave@xxxxxxx> > > Date: Wed, 27 Oct 2010 06:44:17 -0300 > > Subject: [PATCH] taskset: fix memory leaks when exiting on error > > > > Signed-off-by: Davidlohr Bueso <dave@xxxxxxx> > > --- > > schedutils/taskset.c | 48 +++++++++++++++++++++++++++++++++++------------- > > 1 files changed, 35 insertions(+), 13 deletions(-) > > > > diff --git a/schedutils/taskset.c b/schedutils/taskset.c > > index 201fc15..8094c5d 100644 > > --- a/schedutils/taskset.c > > +++ b/schedutils/taskset.c > > @@ -125,8 +125,10 @@ int main(int argc, char *argv[]) > > > > buflen = 7 * cur_nbits; > > buf = malloc(buflen); > > - if (!buf) > > + if (!buf) { > > + cpuset_free(cur_set); > > err(EXIT_FAILURE, _("malloc failed")); > > + } > > > > /* > > * new_set is always used for the sched_setaffinity call > > @@ -134,12 +136,17 @@ int main(int argc, char *argv[]) > > * cpumask_t if the user's mask is shorter. > > */ > > new_set = cpuset_alloc(ncpus, &new_setsize, NULL); > > - if (!new_set) > > + if (!new_set) { > > + free(buf); > > + cpuset_free(cur_set); > > err(EXIT_FAILURE, _("cpuset_alloc failed")); > > + } > > > > if (pid) { > > - if (sched_getaffinity(pid, cur_setsize, cur_set) < 0) > > - err(EXIT_FAILURE, _("failed to get pid %d's affinity"), pid); > > + if (sched_getaffinity(pid, cur_setsize, cur_set) < 0) { > > + fprintf(stderr, _("failed to get pid %d's affinity"), pid); > > + goto err_free; > > I know about this problem, but I have doubts that this pedantic point > of view and the extra code makes any real effect. After exit() all > memory will be deallocated... I think it's better in this example to > call err() and keep the code simple and readable. > Fair enough. I ran into this when looking to use the xalloc stuff, so we just might want to change cpuset_alloc() to use xmalloc instead of malloc and be done with it. Davidlohr -- To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html