>From f4ad182a14f31537238542fc01fce59c26220f53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1draig=20Brady?= Date: Mon, 3 May 2021 18:11:04 +0100 Subject: [PATCH] maint: consistently free hash structures in dev mode 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 | 20 ++++++++++++++++++++ src/copy.h | 2 ++ src/cp-hash.c | 2 ++ src/cp.c | 5 +++++ src/install.c | 3 +++ src/ln.c | 6 ++++++ src/mv.c | 4 ++++ src/tail.c | 10 +++++++++- 8 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/copy.c b/src/copy.c index 3e1abee28..afd7974b1 100644 --- a/src/copy.c +++ b/src/copy.c @@ -1955,6 +1955,16 @@ dest_info_init (struct cp_options *x) triple_free); } +#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 @@ -1977,6 +1987,16 @@ src_info_init (struct cp_options *x) triple_free); } +#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. diff --git a/src/copy.h b/src/copy.h index 026530518..a97089137 100644 --- a/src/copy.h +++ b/src/copy.h @@ -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; diff --git a/src/cp-hash.c b/src/cp-hash.c index 423fcda23..1223c8aed 100644 --- a/src/cp-hash.c +++ b/src/cp-hash.c @@ -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 diff --git a/src/cp.c b/src/cp.c index a420c592b..c97a67563 100644 --- 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 */ { diff --git a/src/install.c b/src/install.c index 8edf00f93..c9456fe5d 100644 --- a/src/install.c +++ b/src/install.c @@ -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 } } diff --git a/src/ln.c b/src/ln.c index 760bab25a..c7eb74026 100644 --- 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); diff --git a/src/mv.c b/src/mv.c index 3db5cc48d..7d81b59a7 100644 --- 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 { diff --git a/src/tail.c b/src/tail.c index 9095a18d8..ff567560d 100644 --- a/src/tail.c +++ b/src/tail.c @@ -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; } -- 2.26.2