From bb6dfd8f11081f4a7b3af815a8e3ef51afb3b0d9 Mon Sep 17 00:00:00 2001 From: Nelson Elhage Date: Thu, 17 Feb 2011 20:55:11 -0500 Subject: [PATCH] cgrulesengd: Improve handling of out-of-memory. Don't leak the old array if the allocation fails, and don't touch num_allocation if the allocation fails. Signed-off-by: Nelson Elhage Signed-off-by: Jan Safranek --- src/daemon/cgrulesengd.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/daemon/cgrulesengd.c b/src/daemon/cgrulesengd.c index a2f9549e..10fd2cd7 100644 --- a/src/daemon/cgrulesengd.c +++ b/src/daemon/cgrulesengd.c @@ -173,13 +173,15 @@ static int cgre_store_parent_info(pid_t pid) uptime_ns = ((__u64)tp.tv_sec * 1000 * 1000 * 1000 ) + tp.tv_nsec; if (array_pi.index >= array_pi.num_allocation) { - array_pi.num_allocation += NUM_PER_REALLOCATIOM; - array_pi.parent_info = realloc(array_pi.parent_info, - sizeof(info) * array_pi.num_allocation); - if (!array_pi.parent_info) { + int alloc = array_pi.num_allocation + NUM_PER_REALLOCATIOM; + void *new_array = realloc(array_pi.parent_info, + sizeof(info) * alloc); + if (!new_array) { flog(LOG_WARNING, "Failed to allocate memory"); return 1; } + array_pi.parent_info = new_array; + array_pi.num_allocation = alloc; } info = calloc(1, sizeof(struct parent_info)); if (!info) { @@ -258,13 +260,15 @@ static int cgre_store_unchanged_process(pid_t pid, int flags) return 0; } if (array_unch.index >= array_unch.num_allocation) { - array_unch.num_allocation += NUM_PER_REALLOCATIOM; - array_unch.proc = realloc(array_unch.proc, - sizeof(unchanged_pid_t) * array_unch.num_allocation); - if (!array_unch.proc) { + int alloc = array_unch.num_allocation + NUM_PER_REALLOCATIOM; + void *new_array = realloc(array_unch.proc, + sizeof(unchanged_pid_t) * alloc); + if (!new_array) { flog(LOG_WARNING, "Failed to allocate memory"); return 1; } + array_unch.proc = new_array; + array_unch.num_allocation = alloc; } array_unch.proc[array_unch.index].pid = pid; array_unch.proc[array_unch.index].flags = flags; -- 2.47.2