From 22999697b81acf3e8683c95c310a36926e7af96f Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Wed, 13 Aug 2008 20:53:12 +0200 Subject: mktemp, sort, tac: don't use undefined after mkstemp failure When mkstemp fails, the template buffer may have undefined contents, so we must not print it. * src/sort.c (create_temp_file): Use temp_dir, not "file" when diagnosing failed mkstemp, because "file" may be undefined. * tests/misc/sort-merge: Adjust for new expected output. Jeph Cowan and Ralf Wildenhues reported the test failure: http://thread.gmane.org/gmane.comp.gnu.coreutils.bugs/14235/focus=14257 * src/tac.c (copy_to_temp): Don't use template buffer after failed mkstemp call, since its contents may be undefined. * tests/misc/tac (pipe-bad-tmpdir): New test for the above. * src/mktemp.c (main): Save a copy of the template string, solely for use in case mkstemp fails. * tests/misc/mktemp (pipe-bad-tmpdir): New test for the above. --- src/mktemp.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/mktemp.c') diff --git a/src/mktemp.c b/src/mktemp.c index 8a09231f9..d745e6069 100644 --- a/src/mktemp.c +++ b/src/mktemp.c @@ -242,20 +242,24 @@ main (int argc, char **argv) quote (template)); } - dest_name = file_name_concat (dest_dir, template, NULL); + template = file_name_concat (dest_dir, template, NULL); } else { - dest_name = xstrdup (template); + template = xstrdup (template); } + /* Make a copy to be used in case of diagnostic, since failing + mkstemp may leave the buffer in an undefined state. */ + dest_name = xstrdup (template); + if (create_directory) { int err = mkdtemp_len (dest_name, x_count, dry_run); if (err != 0) { error (0, errno, _("failed to create directory via template %s"), - quote (dest_name)); + quote (template)); status = EXIT_FAILURE; } } @@ -265,7 +269,7 @@ main (int argc, char **argv) if (fd < 0 || (!dry_run && close (fd) != 0)) { error (0, errno, _("failed to create file via template %s"), - quote (dest_name)); + quote (template)); status = EXIT_FAILURE; } } -- cgit v1.2.3-54-g00ecf