coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] maint: make chmod/chgrp/chown leak free under valgrind


From: Pádraig Brady
Subject: [PATCH] maint: make chmod/chgrp/chown leak free under valgrind
Date: Sun, 13 May 2018 19:24:12 -0700

* src/chmod.c: Deallocate the mode change array in dev mode.
* src/chown.c: Make chopt_free() actually deallocate, but
only call in dev mode.
* src/chgrp.c: Likewise.
---
 src/chgrp.c      | 4 ++--
 src/chmod.c      | 2 ++
 src/chown-core.c | 7 +++++++
 src/chown-core.h | 5 ++---
 src/chown.c      | 4 ++--
 5 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/chgrp.c b/src/chgrp.c
index 130fa73..ec3bb13 100644
--- a/src/chgrp.c
+++ b/src/chgrp.c
@@ -295,7 +295,7 @@ main (int argc, char **argv)
   else
     {
       char *group_name = argv[optind++];
-      chopt.group_name = (*group_name ? group_name : NULL);
+      chopt.group_name = (*group_name ? xstrdup (group_name) : NULL);
       gid = parse_group (group_name);
     }
 
@@ -313,7 +313,7 @@ main (int argc, char **argv)
                     (uid_t) -1, gid,
                     (uid_t) -1, (gid_t) -1, &chopt);
 
-  chopt_free (&chopt);
+  IF_LINT (chopt_free (&chopt));
 
   return ok ? EXIT_SUCCESS : EXIT_FAILURE;
 }
diff --git a/src/chmod.c b/src/chmod.c
index 0e9436c..520d5e1 100644
--- a/src/chmod.c
+++ b/src/chmod.c
@@ -566,5 +566,7 @@ main (int argc, char **argv)
   ok = process_files (argv + optind,
                       FTS_COMFOLLOW | FTS_PHYSICAL | FTS_DEFER_STAT);
 
+  IF_LINT (free (change));
+
   return ok ? EXIT_SUCCESS : EXIT_FAILURE;
 }
diff --git a/src/chown-core.c b/src/chown-core.c
index c235ef3..c99d263 100644
--- a/src/chown-core.c
+++ b/src/chown-core.c
@@ -66,6 +66,13 @@ chopt_init (struct Chown_option *chopt)
   chopt->group_name = NULL;
 }
 
+extern void
+chopt_free (struct Chown_option *chopt)
+{
+  free (chopt->user_name);
+  free (chopt->group_name);
+}
+
 /* Convert the numeric group-id, GID, to a string stored in xmalloc'd memory,
    and return it.  If there's no corresponding group name, use the decimal
    representation of the ID.  */
diff --git a/src/chown-core.h b/src/chown-core.h
index c410158..77a5feb 100644
--- a/src/chown-core.h
+++ b/src/chown-core.h
@@ -68,9 +68,8 @@ struct Chown_option
 void
 chopt_init (struct Chown_option *);
 
-/* Deliberately do not free chopt->user_name or ->group_name.
-   They're not always allocated.  */
-# define chopt_free(chopt)
+void
+chopt_free (struct Chown_option *);
 
 char *
 gid_to_name (gid_t) _GL_ATTRIBUTE_MALLOC;
diff --git a/src/chown.c b/src/chown.c
index da6ed4a..f44eab2 100644
--- a/src/chown.c
+++ b/src/chown.c
@@ -306,7 +306,7 @@ main (int argc, char **argv)
          empty string so that diagnostics say "ownership :GROUP"
          rather than "group GROUP".  */
       if (!chopt.user_name && chopt.group_name)
-        chopt.user_name = bad_cast ("");
+        chopt.user_name = xstrdup ("");
 
       optind++;
     }
@@ -325,7 +325,7 @@ main (int argc, char **argv)
                     uid, gid,
                     required_uid, required_gid, &chopt);
 
-  chopt_free (&chopt);
+  IF_LINT (chopt_free (&chopt));
 
   return ok ? EXIT_SUCCESS : EXIT_FAILURE;
 }
-- 
2.9.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]