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; + } if (c_opt) printf(_("pid %d's current affinity list: %s\n"), pid, @@ -148,23 +155,33 @@ int main(int argc, char *argv[]) printf(_("pid %d's current affinity mask: %s\n"), pid, cpumask_create(buf, buflen, cur_set, cur_setsize)); - if (argc - optind == 1) + if (argc - optind == 1) { + free(buf); + cpuset_free(cur_set); + cpuset_free(new_set); return EXIT_SUCCESS; + } } rc = c_opt ? cpulist_parse(argv[optind], new_set, new_setsize) : cpumask_parse(argv[optind], new_set, new_setsize); - if (rc) - errx(EXIT_FAILURE, _("failed to parse %s %s"), - c_opt ? _("CPU list") : _("CPU mask"), - argv[optind]); + if (rc) { + fprintf(stderr, _("failed to parse %s %s"), + c_opt ? _("CPU list") : _("CPU mask"), + argv[optind]); + goto err_free; + } - if (sched_setaffinity(pid, new_setsize, new_set) < 0) - err(EXIT_FAILURE, _("failed to set pid %d's affinity"), pid); + if (sched_setaffinity(pid, new_setsize, new_set) < 0) { + fprintf(stderr, _("failed to set pid %d's affinity"), pid); + goto err_free; + } - 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; + } if (pid) { if (c_opt) @@ -186,4 +203,9 @@ int main(int argc, char *argv[]) } return EXIT_SUCCESS; +err_free: + free(buf); + cpuset_free(cur_set); + cpuset_free(new_set); + return EXIT_FAILURE; } -- 1.7.0.4 -- 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