diff options
| author | Ulf Hermann <[email protected]> | 2018-07-23 12:15:22 +0200 |
|---|---|---|
| committer | Ulf Hermann <[email protected]> | 2018-07-23 12:25:55 +0000 |
| commit | 4b0dd76e3aa9d27366fc17a47598ef7aa2c737fa (patch) | |
| tree | 4082a1e72f46380e4ef24803080feb26dc167ef7 | |
| parent | 470247828445d63104c5e32264471f3f31e9c9a5 (diff) | |
readlink: unlink temporary files after closing them, and avoid TMPDIR
Windows cannot unlink open files, and there is no predefined temporary
directory. Use the section name as base for the file name.
Change-Id: I7e050e1ca5e5e890256b2573ca0d0c64745932fa
Reviewed-by: Christian Kandeler <[email protected]>
| -rw-r--r-- | src/readelf.c | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/src/readelf.c b/src/readelf.c index cc084120..157ead61 100644 --- a/src/readelf.c +++ b/src/readelf.c @@ -579,7 +579,7 @@ parse_opt (int key, char *arg, /* Create a file descriptor to read the data from the elf_input_section given a file descriptor to an ELF file. */ static int -open_input_section (int fd) +open_input_section (int fd, char **tempname) { size_t shnums; size_t cnt; @@ -643,22 +643,20 @@ open_input_section (int fd) goto open_error; } - /* Create (and immediately unlink) a temporary file to store + /* Create a temporary file to store section data in to create a file descriptor for it. */ - const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir; - static const char suffix[] = "/readelfXXXXXX"; - int tmplen = strlen (tmpdir) + sizeof (suffix); - char *tempname = alloca (tmplen); - sprintf (tempname, "%s%s", tmpdir, suffix); + static const char suffix[] = "-readelfXXXXXX"; + int tmplen = strlen (sname) + sizeof (suffix); + *tempname = malloc (tmplen); + sprintf (*tempname, "%s%s", sname, suffix); - int sfd = mkstemp (tempname); + int sfd = mkstemp (*tempname); if (sfd == -1) { error (0, 0, gettext ("cannot create temp file '%s'"), - tempname); + *tempname); goto open_error; } - unlink (tempname); ssize_t size = data->d_size; if (write_retry (sfd, data->d_buf, size) != size) @@ -839,12 +837,13 @@ process_file (int fd, const char *fname, bool only_one) if (!any_control_option) return; + char *tempname = NULL; if (elf_input_section != NULL) { /* Replace fname and fd with section content. */ char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2); sprintf (fnname, "%s:%s", fname, elf_input_section); - fd = open_input_section (fd); + fd = open_input_section (fd, &tempname); if (fd == -1) { error (0, 0, gettext ("No such section '%s' in '%s'"), @@ -877,6 +876,12 @@ process_file (int fd, const char *fname, bool only_one) care of original. */ if (elf_input_section != NULL) close (fd); + + if (tempname) + { + unlink (tempname); + free (tempname); + } } /* Check whether there are any compressed sections in the ELF file. */ |
