coreutils/coreutils-fix_tac.patch

138 lines
4.4 KiB
Diff

>From cdd328f232a93fb40aec25d0681ef191eaeba2da Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering@redhat.com>
Date: Sun, 16 Oct 2011 10:35:56 +0200
Subject: [PATCH 1/3] maint: tac: remove sole use of sprintf in favor of
stpcpy
* src/tac.c (copy_to_temp): Use stpcpy rather than sprintf.
Move some declarations "down" to point of initialization.
---
src/tac.c | 17 +++++++----------
1 files changed, 7 insertions(+), 10 deletions(-)
Index: src/tac.c
===================================================================
--- src/tac.c.orig 2011-02-19 18:17:03.000000000 +0100
+++ src/tac.c 2011-10-17 15:46:27.879485098 +0200
@@ -426,20 +426,17 @@ copy_to_temp (FILE **g_tmp, char **g_tem
{
static char *template = NULL;
static char const *tempdir;
- char *tempfile;
- FILE *tmp;
- int fd;
if (template == NULL)
{
- char const * const Template = "%s/tacXXXXXX";
+ char const * const Template = "tacXXXXXX";
tempdir = getenv ("TMPDIR");
if (tempdir == NULL)
tempdir = DEFAULT_TMPDIR;
- /* Subtract 2 for `%s' and add 1 for the trailing NUL byte. */
- template = xmalloc (strlen (tempdir) + strlen (Template) - 2 + 1);
- sprintf (template, Template, tempdir);
+ /* Add 1 for the slash and one for the trailing NUL byte. */
+ template = xmalloc (strlen (tempdir) + strlen (Template) + 1 + 1);
+ stpcpy (stpcpy (stpcpy (template, tempdir), "/"), Template);
}
/* FIXME: there's a small window between a successful mkstemp call
@@ -451,21 +448,23 @@ copy_to_temp (FILE **g_tmp, char **g_tem
FIXME: clean up upon fatal signal. Don't block them, in case
$TMPFILE is a remote file system. */
- tempfile = template;
- fd = mkstemp (template);
+ char *tempfile = xstrdup (template);
+ int fd = mkstemp (tempfile);
if (fd < 0)
{
error (0, errno, _("cannot create temporary file in %s"),
quote (tempdir));
+ free (tempfile);
return false;
}
- tmp = fdopen (fd, (O_BINARY ? "w+b" : "w+"));
+ FILE *tmp = fdopen (fd, (O_BINARY ? "w+b" : "w+"));
if (! tmp)
{
error (0, errno, _("cannot open %s for writing"), quote (tempfile));
close (fd);
unlink (tempfile);
+ free (tempfile);
return false;
}
@@ -501,6 +500,7 @@ copy_to_temp (FILE **g_tmp, char **g_tem
Fail:
fclose (tmp);
+ free (tempfile);
return false;
}
@@ -512,8 +512,14 @@ tac_nonseekable (int input_fd, const cha
{
FILE *tmp_stream;
char *tmp_file;
- return (copy_to_temp (&tmp_stream, &tmp_file, input_fd, file)
- && tac_seekable (fileno (tmp_stream), tmp_file));
+
+ if (!copy_to_temp (&tmp_stream, &tmp_file, input_fd, file))
+ return false;
+
+ bool ok = tac_seekable (fileno (tmp_stream), tmp_file);
+ fclose (tmp_stream);
+ free (tmp_file);
+ return ok;
}
/* Print FILE in reverse, copying it to a temporary
Index: tests/Makefile.am
===================================================================
--- tests/Makefile.am.orig 2011-10-17 15:40:44.533154336 +0200
+++ tests/Makefile.am 2011-10-17 15:40:44.882149592 +0200
@@ -270,6 +270,7 @@ TESTS = \
misc/sum-sysv \
misc/tac \
misc/tac-continue \
+ misc/tac-2-nonseekable \
misc/tail \
misc/tee \
misc/tee-dash \
Index: tests/misc/tac-2-nonseekable
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ tests/misc/tac-2-nonseekable 2011-10-17 15:40:44.883149578 +0200
@@ -0,0 +1,27 @@
+#!/bin/sh
+# ensure that tac works with two or more non-seekable inputs
+
+# Copyright (C) 2011 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
+print_ver_ tac
+
+echo x | tac - - > out 2> err || fail=1
+echo x > exp || fail=1
+compare out exp || fail=1
+compare err /dev/null || fail=1
+
+Exit $fail