Introduce and use gdb::unlinker

This introduces a new class, gdb::unlinker, that unlinks a file in the
destructor.  The user of this class has the option to preserve the
file instead, by calling the "keep" method.

This patch then changes the spots in gdb that use unlink in a cleanup
to use this class instead.  In one spot I went ahead and removed all
the cleanups from the function.

This fixes one latent bug -- do_bfd_delete_cleanup could refer to
freed memory, by decref'ing the BFD before using its filename.

2017-01-10  Tom Tromey  <tom@tromey.com>

	* record-full.c (record_full_save_cleanups): Remove.
	(record_full_save): Use gdb::unlinker.
	* gcore.c (do_bfd_delete_cleanup): Remove.
	(gcore_command): Use gdb::unlinker, unique_xmalloc_ptr.  Remove
	cleanups.
	* dwarf2read.c (unlink_if_set): Remove.
	(write_psymtabs_to_index): Use gdb::unlinker.
	* common/gdb_unlinker.h: New file.
This commit is contained in:
Tom Tromey
2016-11-21 16:26:20 -07:00
parent 192b62ce0b
commit bef155c3e8
5 changed files with 110 additions and 74 deletions

View File

@@ -69,6 +69,7 @@
#include "filestuff.h"
#include "build-id.h"
#include "namespace.h"
#include "common/gdb_unlinker.h"
#include <fcntl.h>
#include <sys/types.h>
@@ -23165,16 +23166,6 @@ write_obstack (FILE *file, struct obstack *obstack)
error (_("couldn't data write to file"));
}
/* Unlink a file if the argument is not NULL. */
static void
unlink_if_set (void *p)
{
char **filename = (char **) p;
if (*filename)
unlink (*filename);
}
/* A helper struct used when iterating over debug_types. */
struct signatured_type_index_data
{
@@ -23259,7 +23250,7 @@ static void
write_psymtabs_to_index (struct objfile *objfile, const char *dir)
{
struct cleanup *cleanup;
char *filename, *cleanup_filename;
char *filename;
struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
struct obstack cu_list, types_cu_list;
int i;
@@ -23289,8 +23280,7 @@ write_psymtabs_to_index (struct objfile *objfile, const char *dir)
if (!out_file)
error (_("Can't open `%s' for writing"), filename);
cleanup_filename = filename;
make_cleanup (unlink_if_set, &cleanup_filename);
gdb::unlinker unlink_file (filename);
symtab = create_mapped_symtab ();
make_cleanup (cleanup_mapped_symtab, symtab);
@@ -23429,9 +23419,8 @@ write_psymtabs_to_index (struct objfile *objfile, const char *dir)
fclose (out_file);
/* We want to keep the file, so we set cleanup_filename to NULL
here. See unlink_if_set. */
cleanup_filename = NULL;
/* We want to keep the file. */
unlink_file.keep ();
do_cleanups (cleanup);
}