Hi,

Ken'ichi Ohmichi wrote:
> I reviewed the libcgroup code in the viewpoint of memory-leak and
> found some lacks of fclose(). This patch adds some fclose() calls
> for error handling.
> 
> 
> Thanks
> Ken'ichi Ohmichi
> 
> Signed-off-by: Ken'ichi Ohmichi <[email protected]>
> ---
> diff -rpuN a/api.c b/api.c
> --- a/api.c   2009-03-30 13:48:16.000000000 +0900
> +++ b/api.c   2009-03-30 13:47:54.000000000 +0900
> @@ -693,14 +693,16 @@ static int cg_test_mounted_fs()
>       temp_ent = (struct mntent *) malloc(sizeof(struct mntent));
>       if (!temp_ent) {
>               /* We just fail at the moment. */
> +             fclose(proc_mount);
>               return 0;
>       }
>  
>       ent = getmntent_r(proc_mount, temp_ent, mntent_buff,
>                                               sizeof(mntent_buff));
> -
> -     if (!ent)
> -             return 0;
> +     if (!ent) {
> +             ret = 0;
> +             goto done;
> +     }
>  
>       while (strcmp(ent->mnt_type, "cgroup") !=0) {
>               ent = getmntent_r(proc_mount, temp_ent, mntent_buff,
> diff -rpuN a/cgclassify.c b/cgclassify.c
> --- a/cgclassify.c    2009-03-30 13:48:16.000000000 +0900
> +++ b/cgclassify.c    2009-03-30 13:47:54.000000000 +0900
> @@ -57,9 +57,11 @@ int euid_of_pid(pid_t pid)
>                               (int *)&euid, (int *)&suid, (int *)&fsuid);
>                       cgroup_dbg("Scanned proc values are %d %d %d %d\n",
>                               ruid, euid, suid, fsuid);
> +                     fclose(fp);
>                       return euid;
>               }
>       }
> +     fclose(fp);
>  
>       /* If we are here, we could not find euid. Return error. */
>       return -1;
> diff -rpuN a/config.c b/config.c
> --- a/config.c        2009-03-30 13:48:16.000000000 +0900
> +++ b/config.c        2009-03-30 13:47:54.000000000 +0900
> @@ -469,6 +469,7 @@ int cgroup_config_load_config(const char
>  
>       if (yyparse() != 0) {
>               cgroup_dbg("Failed to parse file %s\n", pathname);
> +             fclose(yyin);
>               return ECGROUPPARSEFAIL;
>       }
>  
> 

The above patch was based on old libcgroup code, and I created a new
patch based on the latest libcgroup which is taken from git tree.


Thanks
Ken'ichi Ohmichi

Signed-off-by: Ken'ichi Ohmichi <[email protected]>
---
diff -rpuN a/src/api.c b/src/api.c
--- a/src/api.c 2009-03-31 11:36:24.000000000 +0900
+++ b/src/api.c 2009-03-31 11:38:23.000000000 +0900
@@ -696,14 +696,17 @@ static int cg_test_mounted_fs()
        temp_ent = (struct mntent *) malloc(sizeof(struct mntent));
        if (!temp_ent) {
                /* We just fail at the moment. */
+               fclose(proc_mount);
                return 0;
        }
 
        ent = getmntent_r(proc_mount, temp_ent, mntent_buff,
                                                sizeof(mntent_buff));
 
-       if (!ent)
-               return 0;
+       if (!ent) {
+               ret = 0;
+               goto done;
+       }
 
        while (strcmp(ent->mnt_type, "cgroup") !=0) {
                ent = getmntent_r(proc_mount, temp_ent, mntent_buff,
diff -rpuN a/src/config.c b/src/config.c
--- a/src/config.c      2009-03-31 11:36:24.000000000 +0900
+++ b/src/config.c      2009-03-31 11:39:45.000000000 +0900
@@ -469,6 +469,7 @@ int cgroup_config_load_config(const char
 
        if (yyparse() != 0) {
                cgroup_dbg("Failed to parse file %s\n", pathname);
+               fclose(yyin);
                return ECGROUPPARSEFAIL;
        }
 
diff -rpuN a/src/tools/cgclassify.c b/src/tools/cgclassify.c
--- a/src/tools/cgclassify.c    2009-03-31 11:36:24.000000000 +0900
+++ b/src/tools/cgclassify.c    2009-03-31 11:39:17.000000000 +0900
@@ -57,9 +57,11 @@ int euid_of_pid(pid_t pid)
                                (int *)&euid, (int *)&suid, (int *)&fsuid);
                        cgroup_dbg("Scanned proc values are %d %d %d %d\n",
                                ruid, euid, suid, fsuid);
+                       fclose(fp);
                        return euid;
                }
        }
+       fclose(fp);
 
        /* If we are here, we could not find euid. Return error. */
        return -1;

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

Reply via email to