From 757f6ffdc986c811a75e759271e288d387f36881 Mon Sep 17 00:00:00 2001 From: jay Date: Sat, 2 Jul 2005 09:44:03 +0000 Subject: [PATCH] Applied patch from Jim Meyering; if we open an output file but stderr is closed, ensure that we don't send diagnostics to that file --- ChangeLog | 9 +++++++++ find/find.c | 42 ------------------------------------------ find/parser.c | 3 ++- import-gnulib.sh | 13 ++----------- lib/savedirinfo.c | 4 ++++ 5 files changed, 17 insertions(+), 54 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2795765..74fa932 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2005-07-01 Jim Meyering + + With `-fprint F' or `-fprintf F', don't write diagnostics + to F when stderr is closed. + + * find/parser.c: Include "stdio-safer.h". + (open_output_file): Use fopen_safer, not fopen. + * import-gnulib.sh (findutils_modules): Add stdio-safer. + 2005-06-24 Bas van Gompel * locate/locate.c (visit_count, visit_limit): New functions. diff --git a/find/find.c b/find/find.c index 16001bb..fe2c1f4 100644 --- a/find/find.c +++ b/find/find.c @@ -383,46 +383,6 @@ check_nofollow(void) } #endif -static void -ensure_std_fds_are_open(void) -{ - const char *safe_file = "/dev/null"; - int fd, saved_errno - - do - { - fd = open(safe_file, O_RDONLY); - if (fd < 0) - { - /* /dev/null missing or cannot be opened. Perhaps we are in - * a very restrictive chroot() environment. Try using /, - * which must be present. - */ - saved_errno = errno; - fd = open("/", O_RDONLY); - if (fd < 0) - { - /* report the original error, which is probably - * more sensible. - */ - errno = saved_errno; - } - } - - } while (fd > 0 && fd <= 2); - - if (fd < 0) - { - /* If stderr is closed here, we lose. Oh, well. */ - error(1, errno, _("Cannot open %s"), quote(safe_file)); - } - else - { - close(fd); - } -} - - int main (int argc, char **argv) { @@ -435,8 +395,6 @@ main (int argc, char **argv) const struct parser_table *entry_close, *entry_print, *entry_open; - ensure_std_fds_are_open(); - /* We call check_nofollow() before setlocale() because the numbers * for which we check (in the results of uname) definitiely have "." * as the decimal point indicator even under locales for which that diff --git a/find/parser.c b/find/parser.c index 35d4b75..f13daf4 100644 --- a/find/parser.c +++ b/find/parser.c @@ -32,6 +32,7 @@ #include "quotearg.h" #include "buildcmd.h" #include "nextelem.h" +#include "stdio-safer.h" #ifdef HAVE_FCNTL_H #include @@ -2452,7 +2453,7 @@ open_output_file (char *path) return stderr; else if (!strcmp (path, "/dev/stdout")) return stdout; - f = fopen (path, "w"); + f = fopen_safer (path, "w"); if (f == NULL) error (1, errno, "%s", path); return f; diff --git a/import-gnulib.sh b/import-gnulib.sh index b151e75..3154606 100644 --- a/import-gnulib.sh +++ b/import-gnulib.sh @@ -41,8 +41,8 @@ destdir="gnulib" # Modules needed for findutils itself. findutils_modules="\ alloca argmatch dirname error fileblocks fnmatch-gnu \ -getline getopt human idcache lstat malloc memcmp memset mktime \ -modechange pathmax quotearg realloc regex rpmatch savedir stat \ +getline getopt human idcache lstat malloc memcmp memset mktime \ +modechange pathmax quotearg realloc regex rpmatch savedir stat stdio-safer \ stpcpy strdup strftime strstr strtol strtoul strtoull strtoumax \ xalloc xalloc-die xgetcwd xstrtol xstrtoumax yesno human filemode \ getline stpcpy canonicalize mountlist closeout gettext stat-macros" @@ -137,12 +137,3 @@ EOF ## ls *.m4 | sed -e 's/$/ \\/' | sed -e '$ s/\\$//' ## echo ## ) > gnulib/m4/Makefile.am - - - -# We also need config.rpath, because configure calls it, but for -# some reason, "autoreconf -i" doesn't fetch it. -for f in config.rpath -do - (set -x; cp "$1"/build-aux/"$f" . ) -done diff --git a/lib/savedirinfo.c b/lib/savedirinfo.c index 8e800d1..7b01770 100644 --- a/lib/savedirinfo.c +++ b/lib/savedirinfo.c @@ -292,6 +292,10 @@ new_savedirinfo (const char *dir, struct savedir_extrainfo **extra) free (pex); return buf; } + else + { + return NULL; + } } -- 2.11.4.GIT