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
This commit is contained in:
Pádraig Brady committed 2021-05-08 14:57:33 +01:00
1 parent 65e4d0f71e
commit 09bbb2b703
8 files changed
+51 -1

No files matched your search

+20
View 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.
+2
View 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;
+2
View 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
+5
View File
@@ -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 */
{
+3
View 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
}
}
+6
View File
@@ -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);
+4
View File
@@ -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
{
+9 -1
View 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;
}