Fixes a few memory leaks, and leaked file handles.

[r...@gondor tests]# make                                                       
                                                
g++ -g -O2 -Wall -DDEBUG -I .. -o libcgrouptest01 libcgrouptest01.c -L .. 
-lcgroup -lpthread                                    
libcgrouptest01.c: In function ‘int group_modified(char*, int)’:            
                                                    
libcgrouptest01.c:1060: warning: format ‘%d’ expects type ‘int*’, but 
argument 3 has type ‘bool*’                               
s[r...@gondor tests]# sh runlibcgrouptest.sh                                    
                                                

Running first set of testcases
==============================
==14758== Memcheck, a memory error detector.
==14758== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==14758== Using LibVEX rev 1804, a library for dynamic binary translation.
==14758== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.       
==14758== Using valgrind-3.3.0, a dynamic binary instrumentation framework.
==14758== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.  
==14758== For more details, rerun with: -v                                 
==14758==                                                                  
C:DBG: fs_mounted as recieved from script=0                                
TEST 1:PASS : cgroup_init()                      Ret Value = 50001         
TEST 2:PASS : cgroup_attach_task()               Ret Value = 50014       
Parameter nullcgroup
TEST 3:PASS : cgroup_new_cgroup()                Ret Value = 0                  
             
TEST 4:PASS : cgroup_create_cgroup()             Ret Value = 50014              
             
TEST 5:PASS : cgroup_delete_cgroup()             Ret Value = 50014              
             
TEST 6:PASS : cgroup_create_cgroup()             Ret Value = 50014              
             
TEST 7:PASS : cgroup_delete_cgroup()             Ret Value = 50014              
             
TEST 8:PASS : cgroup_add_controller()            Ret Value = 0                  
             
TEST 9:PASS : cgroup_add_controller()            Ret Value = 0                  
             
TEST10:PASS : cgroup_add_controller()            Ret Value = 0                  
             
==14758==                                                                       
             
==14758== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 18 from 1)       
             
==14758== malloc/free: in use at exit: 0 bytes in 0 blocks.                     
             
==14758== malloc/free: 18 allocs, 18 frees, 32,293 bytes allocated.             
             
==14758== For counts of detected errors, rerun with: -v                         
             
==14758== All heap blocks were freed -- no leaks are possible.                  
             

Running second set of testcases
============================== 
==14774== Memcheck, a memory error detector.
==14774== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==14774== Using LibVEX rev 1804, a library for dynamic binary translation.
==14774== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.       
==14774== Using valgrind-3.3.0, a dynamic binary instrumentation framework.
==14774== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.  
==14774== For more details, rerun with: -v                                 
==14774==                                                                  
C:DBG: fs_mounted as recieved from script=1                                
C:DBG: mountpoint1 as recieved from script=/dev/cgroup_controllers-1       
sanity check pass. cgroup                                                  
TEST 1:PASS : cgroup_attach_task()               Ret Value = 50014       
Parameter nullcgroup
TEST 2:PASS : cgroup_init()                      Ret Value = 0                  
             
TEST 3:PASS : cgroup_attach_task()               Ret Value = 0   Task found in 
group/s       
TEST 4:PASS : cgroup_attach_task_pid()           Ret Value = 50016              
             
TEST 5:PASS : cgroup_new_cgroup()                Ret Value = 0                  
             
TEST 6:PASS : cgroup_create_cgroup()             Ret Value = 0   group found in 
filesystem   
TEST 7:PASS : cgroup_attach_task()               Ret Value = 0   Task found in 
group/s       
==14774== Conditional jump or move depends on uninitialised value(s)            
             
==14774==    at 0x40070D8: strncat (mc_replace_strmem.c:214)                    
             
==14774==    by 0x804BC92: main (libcgrouptest01.c:1271)                        
             
==14774==                                                                       
             
==14774== Conditional jump or move depends on uninitialised value(s)            
             
==14774==    at 0x4007271: strcpy (mc_replace_strmem.c:268)                     
             
==14774==    by 0x402F24B: cgroup_modify_cgroup (api.c:1012)                    
             
==14774==    by 0x804BC9D: main (libcgrouptest01.c:252)                         
             
==14774==                                                                       
             
==14774== Conditional jump or move depends on uninitialised value(s)            
             
==14774==    at 0x4007285: strcpy (mc_replace_strmem.c:268)                     
             
==14774==    by 0x402F24B: cgroup_modify_cgroup (api.c:1012)                    
             
==14774==    by 0x804BC9D: main (libcgrouptest01.c:252)                         
             
==14774==                                                                       
             
==14774== Conditional jump or move depends on uninitialised value(s)            
             
==14774==    at 0x8049471: _ZL14group_modifiedPci (libcgrouptest01.c:1076)      
             
==14774==    by 0x804C583: main (libcgrouptest01.c:254)                         
             
==14774==                                                                       
             
==14774== Conditional jump or move depends on uninitialised value(s)            
             
==14774==    at 0x8049479: _ZL14group_modifiedPci (libcgrouptest01.c:1076)      
             
==14774==    by 0x804C583: main (libcgrouptest01.c:254)                         
             
==14774==                                                                       
             
==14774== Conditional jump or move depends on uninitialised value(s)            
             
==14774==    at 0x4007470: strncmp (mc_replace_strmem.c:314)                    
             
==14774==    by 0x804949B: _ZL14group_modifiedPci (libcgrouptest01.c:1076)      
             
==14774==    by 0x804C583: main (libcgrouptest01.c:254)                         
             
TEST 8:PASS : cgroup_modify_cgroup()             Ret Value = 0   Parameter same 
cgroup       
TEST 9:PASS : cgroup_new_cgroup()                Ret Value = 0                  
             
==14774==                                                                       
             
==14774== Conditional jump or move depends on uninitialised value(s)            
             
==14774==    at 0x4007271: strcpy (mc_replace_strmem.c:268)                     
             
==14774==    by 0x402F24B: cgroup_modify_cgroup (api.c:1012)                    
             
==14774==    by 0x804A3E7: test_cgroup_modify_cgroup(int, cgroup*, char const*, 
int, int, int, int, int) (libcgrouptest01.c:889)
==14774==    by 0x804BD35: main (libcgrouptest01.c:286)                         
                                                
==14774==                                                                       
                                                
==14774== Conditional jump or move depends on uninitialised value(s)            
                                                
==14774==    at 0x4007285: strcpy (mc_replace_strmem.c:268)                     
                                                
==14774==    by 0x402F24B: cgroup_modify_cgroup (api.c:1012)                    
                                                
==14774==    by 0x804A3E7: test_cgroup_modify_cgroup(int, cgroup*, char const*, 
int, int, int, int, int) (libcgrouptest01.c:889)
==14774==    by 0x804BD35: main (libcgrouptest01.c:286)                         
                                                
TEST10:PASS : cgroup_modify_cgroup()             Ret Value = 0                  
                                                
TEST11:PASS : cgroup_modify_cgroup()             Ret Value = 50007              
                                                
TEST12:PASS : cgroup_new_cgroup()                Ret Value = 0                  
                                                
==14774==                                                                       
                                                
==14774== Conditional jump or move depends on uninitialised value(s)            
                                                
==14774==    at 0x4007271: strcpy (mc_replace_strmem.c:268)                     
                                                
==14774==    by 0x402F24B: cgroup_modify_cgroup (api.c:1012)                    
                                                
==14774==    by 0x804A3E7: test_cgroup_modify_cgroup(int, cgroup*, char const*, 
int, int, int, int, int) (libcgrouptest01.c:889)
==14774==    by 0x804BDF3: main (libcgrouptest01.c:314)                         
                                                
==14774==                                                                       
                                                
==14774== Conditional jump or move depends on uninitialised value(s)            
                                                
==14774==    at 0x4007285: strcpy (mc_replace_strmem.c:268)                     
                                                
==14774==    by 0x402F24B: cgroup_modify_cgroup (api.c:1012)                    
                                                
==14774==    by 0x804A3E7: test_cgroup_modify_cgroup(int, cgroup*, char const*, 
int, int, int, int, int) (libcgrouptest01.c:889)
==14774==    by 0x804BDF3: main (libcgrouptest01.c:314)                         
                                                
TEST13:PASS : cgroup_modify_cgroup()             Ret Value = 0                  
                                                
TEST14:PASS : cgroup_get_cgroup()                Ret Value = 50007       
Parameter nullcgroup                                   
TEST15:PASS : cgroup_get_cgroup()                Ret Value = 50002       
Parameter not created group                            
TEST16:PASS : cgroup_get_cgroup()                Ret Value = 0                  
                                                
TEST 0:PASS : cgroup_new_cgroup()                Ret Value = 0                  
                                                
TEST 0:PASS : cgroup_create_cgroup()             Ret Value = 0   group found in 
filesystem                                      
TEST17:FAIL : cgroup_get_cgroup()                Ret Value = 50017              
                                                
TEST16:PASS : cgroup_delete_cgroup()             Ret Value = 0   Group deleted 
from filesystem                                  
TEST17:PASS : cgroup_create_cgroup()             Ret Value = 50007              
                                                
TEST18:PASS : cgroup_delete_cgroup()             Ret Value = 50007              
                                                
TEST19:PASS : cgroup_compare_cgroup()            Ret Value = 50011       
Parameter nullcgroup                                   
TEST20:PASS : cgroup_compare_cgroup()            Ret Value = 0                  
                                                
TEST21:PASS : cgroup_compare_cgroup()            Ret Value = 50017              
                                                
==14774==                                                                       
                                                
==14774== ERROR SUMMARY: 45 errors from 10 contexts (suppressed: 18 from 1)     
                                                
==14774== malloc/free: in use at exit: 0 bytes in 0 blocks.                     
                                                
==14774== malloc/free: 187 allocs, 187 frees, 234,711 bytes allocated.          
                                                
==14774== For counts of detected errors, rerun with: -v                         
                                                
==14774== All heap blocks were freed -- no leaks are possible.                  
                                                
Cleanup done                                                                    
                                                

Running third set of testcases
==============================
==14827== Memcheck, a memory error detector.
==14827== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==14827== Using LibVEX rev 1804, a library for dynamic binary translation.
==14827== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.       
==14827== Using valgrind-3.3.0, a dynamic binary instrumentation framework.
==14827== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.  
==14827== For more details, rerun with: -v                                 
==14827==                                                                  
C:DBG: fs_mounted as recieved from script=2                                
C:DBG: mountpoint1 as recieved from script=/dev/cgroup_controllers-1       
C:DBG: mountpoint2 as recieved from script=/dev/cgroup_controllers-2       
sanity check pass. cgroup                                                  
TEST 1:PASS : cgroup_init()                      Ret Value = 0             
TEST 2:PASS : cgroup_attach_task()               Ret Value = 0   Task found in 
group/s
TEST 3:PASS : cgroup_new_cgroup()                Ret Value = 0                  
      
TEST 4:PASS : cgroup_create_cgroup()             Ret Value = 0   group found in 
filesystem
TEST 5:PASS : cgroup_new_cgroup()                Ret Value = 0                  
          
TEST 6:PASS : cgroup_create_cgroup()             Ret Value = 0   group found in 
filesystem
TEST 7:PASS : cgroup_create_cgroup()             Ret Value = 0   group found in 
filesystem
TEST 8:PASS : cgroup_attach_task()               Ret Value = 0   Task found in 
group/s    
TEST 9:PASS : cgroup_attach_task()               Ret Value = 0   Task found in 
group/s    
TEST10:PASS : cgroup_new_cgroup()                Ret Value = 0                  
          
TEST11:PASS : cgroup_attach_task()               Ret Value = 50002       
Parameter not created group
TEST12:PASS : cgroup_new_cgroup()                Ret Value = 0                  
                    
==14827== Conditional jump or move depends on uninitialised value(s)            
                    
==14827==    at 0x4007271: strcpy (mc_replace_strmem.c:268)                     
                    
==14827==    by 0x402F24B: cgroup_modify_cgroup (api.c:1012)                    
                    
==14827==    by 0x804A3E7: test_cgroup_modify_cgroup(int, cgroup*, char const*, 
int, int, int, int, int) (libcgrouptest01.c:889)
==14827==    by 0x804C1D6: main (libcgrouptest01.c:498)                         
                                                
TEST13:PASS : cgroup_modify_cgroup()             Ret Value = 0                  
                                                
TEST14:PASS : cgroup_new_cgroup()                Ret Value = 0                  
                                                
==14827==
==14827== Conditional jump or move depends on uninitialised value(s)
==14827==    at 0x4007271: strcpy (mc_replace_strmem.c:268)
==14827==    by 0x402F24B: cgroup_modify_cgroup (api.c:1012)
==14827==    by 0x804A3E7: test_cgroup_modify_cgroup(int, cgroup*, char const*, 
int, int, int, int, int) (libcgrouptest01.c:889)
==14827==    by 0x804C23C: main (libcgrouptest01.c:519)
==14827==
==14827== Conditional jump or move depends on uninitialised value(s)
==14827==    at 0x4007285: strcpy (mc_replace_strmem.c:268)
==14827==    by 0x402F24B: cgroup_modify_cgroup (api.c:1012)
==14827==    by 0x804A3E7: test_cgroup_modify_cgroup(int, cgroup*, char const*, 
int, int, int, int, int) (libcgrouptest01.c:889)
==14827==    by 0x804C23C: main (libcgrouptest01.c:519)
TEST15:PASS : cgroup_modify_cgroup()             Ret Value = 0
TEST16:PASS : cgroup_delete_cgroup()             Ret Value = 0   Group deleted 
from filesystem
TEST17:PASS : cgroup_delete_cgroup()             Ret Value = 0   Group deleted 
from filesystem
TEST18:PASS : cgroup_new_cgroup()                Ret Value = 0
TEST19:PASS : cgroup_create_cgroup()             Ret Value = 0   group found 
under both controllers
TEST20:PASS : cgroup_attach_task()               Ret Value = 0   Task found in 
group/s
TEST21:PASS : cgroup_new_cgroup()                Ret Value = 0
==14827==
==14827== Conditional jump or move depends on uninitialised value(s)
==14827==    at 0x4007271: strcpy (mc_replace_strmem.c:268)
==14827==    by 0x402F24B: cgroup_modify_cgroup (api.c:1012)
==14827==    by 0x804A3E7: test_cgroup_modify_cgroup(int, cgroup*, char const*, 
int, int, int, int, int) (libcgrouptest01.c:889)
==14827==    by 0x804C464: main (libcgrouptest01.c:622)
==14827==
==14827== Conditional jump or move depends on uninitialised value(s)
==14827==    at 0x4007285: strcpy (mc_replace_strmem.c:268)
==14827==    by 0x402F24B: cgroup_modify_cgroup (api.c:1012)
==14827==    by 0x804A3E7: test_cgroup_modify_cgroup(int, cgroup*, char const*, 
int, int, int, int, int) (libcgrouptest01.c:889)
==14827==    by 0x804C464: main (libcgrouptest01.c:622)
TEST22:PASS : cgroup_modify_cgroup()             Ret Value = 0   group modified 
under both controllers
TEST23:PASS : cgroup_delete_cgroup()             Ret Value = 0   Group deleted 
from filesystem
TEST 0:PASS : cgroup_new_cgroup()                Ret Value = 0
TEST 0:PASS : cgroup_create_cgroup()             Ret Value = 0   group found in 
filesystem
TEST24:FAIL : cgroup_get_cgroup()                Ret Value = 50018
==14827==
==14827== ERROR SUMMARY: 79 errors from 5 contexts (suppressed: 18 from 1)
==14827== malloc/free: in use at exit: 66,044 bytes in 15 blocks.
==14827== malloc/free: 196 allocs, 181 frees, 228,734 bytes allocated.
==14827== For counts of detected errors, rerun with: -v
==14827== searching for pointers to 15 not-freed blocks.
==14827== checked 6,381,180 bytes.
==14827==
==14827==
==14827== 66,044 (18,064 direct, 47,980 indirect) bytes in 4 blocks are 
definitely lost in loss record 1 of 3
==14827==    at 0x4004BA2: calloc (vg_replace_malloc.c:397)
==14827==    by 0x4030D9A: cgroup_new_cgroup (wrapper.c:28)
==14827==    by 0x8049656: new_cgroup(char*, char*, char*, int, int) 
(libcgrouptest01.c:1132)
==14827==    by 0x8049917: create_new_cgroup_ds(int, char const*, int, int) 
(libcgrouptest01.c:744)
==14827==    by 0x804C190: main (libcgrouptest01.c:485)
==14827==
==14827==
==14827== 20,980 bytes in 5 blocks are indirectly lost in loss record 2 of 3
==14827==    at 0x4004BA2: calloc (vg_replace_malloc.c:397)
==14827==    by 0x4030C2C: cgroup_add_value_string (wrapper.c:122)
==14827==    by 0x8048FDB: _ZL17add_control_valueP17cgroup_controllerPcS1_i 
(libcgrouptest01.c:1113)
==14827==    by 0x80496C9: new_cgroup(char*, char*, char*, int, int) 
(libcgrouptest01.c:1147)
==14827==    by 0x8049917: create_new_cgroup_ds(int, char const*, int, int) 
(libcgrouptest01.c:744)
==14827==    by 0x804C190: main (libcgrouptest01.c:485)
==14827==
==14827==
==14827== 27,000 bytes in 6 blocks are indirectly lost in loss record 3 of 3
==14827==    at 0x4004BA2: calloc (vg_replace_malloc.c:397)
==14827==    by 0x4030F4B: cgroup_add_controller (wrapper.c:62)
==14827==    by 0x80496AE: new_cgroup(char*, char*, char*, int, int) 
(libcgrouptest01.c:1144)
==14827==    by 0x8049917: create_new_cgroup_ds(int, char const*, int, int) 
(libcgrouptest01.c:744)
==14827==    by 0x804C190: main (libcgrouptest01.c:485)
==14827==
==14827== LEAK SUMMARY:
==14827==    definitely lost: 18,064 bytes in 4 blocks.
==14827==    indirectly lost: 47,980 bytes in 11 blocks.
==14827==      possibly lost: 0 bytes in 0 blocks.
==14827==    still reachable: 0 bytes in 0 blocks.
==14827==         suppressed: 0 bytes in 0 blocks.
Cleanup done
[r...@gondor tests]#


Signed-off-by: Dhaval Giani <[email protected]>

---
 tests/libcgrouptest01.c |   51 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 34 insertions(+), 17 deletions(-)

Index: trunk/tests/libcgrouptest01.c
===================================================================
--- trunk.orig/tests/libcgrouptest01.c  2009-01-04 22:05:30.000000000 +0530
+++ trunk/tests/libcgrouptest01.c       2009-01-04 22:12:20.000000000 +0530
@@ -15,6 +15,7 @@
  */
 
 #include "libcgrouptest.h"
+#include <errno.h>
 
 /* The messages that may be useful to the user */
 char info[NUM_MSGS][SIZE] = {
@@ -986,6 +987,8 @@ void get_controllers(const char *name, i
                if (strncmp(name, subsys_name, sizeof(*name)) == 0)
                        *exist = 1;
        }
+
+       fclose(fd);
 }
 
 static int group_exist(char *path_group)
@@ -1042,6 +1045,7 @@ static int group_modified(char *path_con
        u_int64_t uint64_val;
        char string_val[FILENAME_MAX]; /* Doubt: what should be the size ? */
        FILE *fd;
+       int error = 1;
 
        fd = fopen(path_control_file, "r");
        if (!fd) {
@@ -1055,31 +1059,32 @@ static int group_modified(char *path_con
        case BOOL:
                fscanf(fd, "%d", &bool_val);
                if (bool_val == val_bool)
-                       return 0;
+                       error = 0;
                break;
        case INT64:
                fscanf(fd, "%lld", &int64_val);
                if (int64_val == val_int64)
-                       return 0;
+                       error = 0;
                break;
        case UINT64:
                fscanf(fd, "%llu", &uint64_val);
                if (uint64_val == val_uint64)
-                       return 0;
+                       error = 0;
                break;
        case STRING:
                fscanf(fd, "%s", string_val);
                if (!strncmp(string_val, val_string, strlen(string_val)))
-                       return 0;
+                       error = 0;
                break;
        default:
                fprintf(stderr, "Wrong value_type passed "
                                                "in group_modified()\n");
                fprintf(stderr, "Skipping modified values check....\n");
-               return 0;       /* Can not report test result as failure */
+               error =  0;     /* Can not report test result as failure */
                break;
        }
-       return 1;
+       fclose(fd);
+       return error;
 }
 static int add_control_value(struct cgroup_controller *newcontroller,
                                 char * control_file, char *wr, int value_type)
@@ -1163,21 +1168,24 @@ struct cgroup *new_cgroup(char *group, c
 int check_fsmounted(int multimnt)
 {
        int count = 0;
-       struct mntent *entry, *tmp_entry;
+       struct mntent *entry, *tmp_entry = NULL;
        /* Need a better mechanism to decide memory allocation size here */
        char entry_buffer[FILENAME_MAX * 4];
-       FILE *proc_file;
+       FILE *proc_file = NULL;
+       int ret = 1;
 
        tmp_entry = (struct mntent *) malloc(sizeof(struct mntent));
        if (!tmp_entry) {
                perror("Error: failled to mallloc for mntent\n");
-               return 1;
+               ret = 1;
+               goto error;
        }
 
        proc_file = fopen("/proc/mounts", "r");
        if (!proc_file) {
                printf("Error in opening /proc/mounts.\n");
-               return EIO;
+               ret = errno;
+               goto error;
        }
        while ((entry = getmntent_r(proc_file, tmp_entry, entry_buffer,
                                                 FILENAME_MAX*4)) != NULL) {
@@ -1187,16 +1195,23 @@ int check_fsmounted(int multimnt)
                                if (count >= 2) {
                                        printf("sanity check pass. %s\n",
                                                         entry->mnt_type);
-                                       return 0;
+                                       ret = 0;
+                                       goto error;
                                }
                        } else {
                                printf("sanity check pass. %s\n",
                                                         entry->mnt_type);
-                               return 0;
+                               ret = 0;
+                               goto error;
                        }
                }
        }
-       return 1;
+error:
+       if (tmp_entry)
+               free(tmp_entry);
+       if (proc_file)
+               fclose(proc_file);
+       return ret;
 }
 
 static int check_task(char *tasksfile)
@@ -1220,6 +1235,7 @@ static int check_task(char *tasksfile)
                        break;
                }
        }
+       fclose(file);
 
        return pass;
 }
@@ -1246,13 +1262,13 @@ static inline void build_path(char *targ
        strncpy(target, mountpoint, FILENAME_MAX);
 
        if (group) {
-               strncat(target, "/", FILENAME_MAX);
-               strncat(target, group, FILENAME_MAX);
+               strncat(target, "/", FILENAME_MAX - strlen(target));
+               strncat(target, group, FILENAME_MAX - strlen(target));
        }
 
        if (file) {
-               strncat(target, "/", FILENAME_MAX);
-               strncat(target, file, FILENAME_MAX);
+               strncat(target, "/", FILENAME_MAX - strlen(target));
+               strncat(target, file, FILENAME_MAX - strlen(target));
        }
 }
 
@@ -1350,6 +1366,7 @@ void test_cgroup_get_cgroup(int ctl1, in
                else
                        message(i++, FAIL, "get_cgroup()", ret,
                                                         info[NOTCRTDGRP]);
+               cgroup_free(&cgroup_filled);
 
                /* 3.
                 * Test with name filled cgroup. Ensure the group group1 exists



------------------------------------------------------------------------------
_______________________________________________
Libcg-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to