]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: consistently free hash structures in dev mode
authorPádraig Brady <P@draigBrady.com>
Mon, 3 May 2021 17:11:04 +0000 (18:11 +0100)
committerPádraig Brady <P@draigBrady.com>
Sat, 8 May 2021 13:57:33 +0000 (14:57 +0100)
Ensure we call hash_free() to avoid valgrind and leak_sanitizer
"definitely lost" warnings.  These were not real leaks as
we terminate immediately after, but we should avoid these
"definitely lost" warnings where possible.

* src/copy.c: Add dest_info_free() and src_info_free().
* src/copy.h: Declare the above.
* src/cp-hash.c: Don't define unless "lint" is defined.
* src/install.c: Call dest_info_free() in dev mode.
* src/mv.c: Likewise.
* src/cp.c: Likewise. Also call src_info_free().
* src/ln.c: Call hash_free() in dev mode.
* src/tail.c: Call hash_free() even if about to exit, in dev mode.

Fixes https://bugs.gnu.org/48189

src/copy.c
src/copy.h
src/cp-hash.c
src/cp.c
src/install.c
src/ln.c
src/mv.c
src/tail.c

index 9fb2420e1b98785dffd81186211e812d660021fd..26ab909fd283b731b5917e449b63ab7a5ecafa06 100644 (file)
@@ -1957,6 +1957,16 @@ dest_info_init (struct cp_options *x)
     xalloc_die ();
 }
 
+#ifdef lint
+extern void
+dest_info_free (struct cp_options *x)
+{
+  if (x->dest_info)
+    hash_free (x->dest_info);
+  x->dest_info = NULL;
+}
+#endif
+
 /* Initialize the hash table implementing a set of F_triple entries
    corresponding to source files listed on the command line.  */
 extern void
@@ -1981,6 +1991,16 @@ src_info_init (struct cp_options *x)
     xalloc_die ();
 }
 
+#ifdef lint
+extern void
+src_info_free (struct cp_options *x)
+{
+  if (x->src_info)
+    hash_free (x->src_info);
+  x->src_info = NULL;
+}
+#endif
+
 /* When effecting a move (e.g., for mv(1)), and given the name DST_NAME
    of the destination and a corresponding stat buffer, DST_SB, return
    true if the logical 'move' operation should _not_ proceed.
index 0265305183f430a493b0efa4106d69fb7f15a16a..a97089137461be0ecfeab6efb944db5737650755 100644 (file)
@@ -300,7 +300,9 @@ extern bool set_file_security_ctx (char const *dst_name,
                                    bool recurse, const struct cp_options *x);
 
 void dest_info_init (struct cp_options *);
+void dest_info_free (struct cp_options *);
 void src_info_init (struct cp_options *);
+void src_info_free (struct cp_options *);
 
 void cp_options_default (struct cp_options *);
 bool chown_failure_ok (struct cp_options const *) _GL_ATTRIBUTE_PURE;
index 423fcda2399fd6203bf1fe7f38fab9d03ef9910f..1223c8aed5a7a2e44935737cfc735ec0741ddf02 100644 (file)
@@ -157,8 +157,10 @@ hash_init (void)
 /* Reset the hash structure in the global variable 'htab' to
    contain no entries.  */
 
+#ifdef lint
 extern void
 forget_all (void)
 {
   hash_free (src_to_dest);
 }
+#endif
index a420c592b8058b5db8b111db5629e77b001b6c91..c97a675631a359e26eeb6076d685b74aa588e1dd 100644 (file)
--- a/src/cp.c
+++ b/src/cp.c
@@ -730,6 +730,11 @@ do_copy (int n_files, char **file, char const *target_directory,
 
           free (dst_name);
         }
+
+#ifdef lint
+      dest_info_free (x);
+      src_info_free (x);
+#endif
     }
   else /* !target_directory */
     {
index 8edf00f93626119f8bd35d7de9a1f10d09225ce2..c9456fe5d924288099251f663c2c20e8a9bb654b 100644 (file)
@@ -1017,6 +1017,9 @@ main (int argc, char **argv)
             if (! install_file_in_dir (file[i], target_directory, &x,
                                        i == 0 && mkdir_and_install))
               exit_status = EXIT_FAILURE;
+#ifdef lint
+          dest_info_free (&x);
+#endif
         }
     }
 
index 760bab25a66816effe96b8b618cd93015e24d71a..c7eb74026c48982977ffbe6a97d6bdf78b9f6652 100644 (file)
--- a/src/ln.c
+++ b/src/ln.c
@@ -680,6 +680,12 @@ main (int argc, char **argv)
           ok &= do_link (file[i], destdir_fd, dest_base, dest, -1);
           free (dest);
         }
+
+#ifdef lint
+      if (dest_set)
+        hash_free (dest_set);
+      dest_set = NULL;
+#endif
     }
   else
     ok = do_link (file[0], AT_FDCWD, file[1], file[1], link_errno);
index 3db5cc48dd91254d3646a50100770306a361a412..7d81b59a7dd764a5a555adbecc1486558782ee73 100644 (file)
--- a/src/mv.c
+++ b/src/mv.c
@@ -503,6 +503,10 @@ main (int argc, char **argv)
           x.last_file = i + 1 == n_files;
           ok &= movefile (file[i], target_directory, true, &x);
         }
+
+#ifdef lint
+      dest_info_free (&x);
+#endif
     }
   else
     {
index 9095a18d899fd77306e9a7d06aa75a9d258cc690..ff567560dc0637adc48a64bbebf04690eaee36af 100644 (file)
@@ -1568,7 +1568,12 @@ tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
       return true;
     }
   if (follow_mode == Follow_descriptor && !found_watchable_file)
-    return false;
+    {
+# ifdef lint
+      hash_free (wd_to_name);
+# endif
+      return false;
+    }
 
   prev_fspec = &(f[n_files - 1]);
 
@@ -1626,6 +1631,9 @@ tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
           && hash_get_n_entries (wd_to_name) == 0)
         {
           error (0, 0, _("no files remaining"));
+# ifdef lint
+          hash_free (wd_to_name);
+# endif
           return false;
         }