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 <nelh...@ksplice.com>
---
 src/daemon/cgrulesengd.c |   20 ++++++++++++--------
 1 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/daemon/cgrulesengd.c b/src/daemon/cgrulesengd.c
index a2f9549..10fd2cd 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;
-- 
1.7.2.43.g36c08.dirty


------------------------------------------------------------------------------
Free Software Download: Index, Search & Analyze Logs and other IT data in 
Real-Time with Splunk. Collect, index and harness all the fast moving IT data 
generated by your applications, servers and devices whether physical, virtual
or in the cloud. Deliver compliance at lower cost and gain new business 
insights. http://p.sf.net/sfu/splunk-dev2dev 
_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to