From c0e9035f57e6f630df8092fccb3f95a21a161cf9 Mon Sep 17 00:00:00 2001
From: James Youngman <jay@gnu.org>
Date: Sun, 8 Dec 2013 15:04:44 +0000
Subject: [PATCH 1/4] Bug #35753: detect failure of the atexit library
 function.
To: findutils-patches@gnu.org

* find/find.c (main): Issue a fatal error message if atexit fails.
* xargs/xargs.c (main): Likewise.
* find/ftsfind.c (main): Likewise.
* locate/code.c (main): Likewise.
* locate/frcode.c (main): Likewise.
* locate/locate.c (dolocate): Likewise.
* locate/bigram.c (main): Likewise; also include <errno.h>.
---
 ChangeLog       |   11 +++++++++++
 find/find.c     |    5 ++++-
 find/ftsfind.c  |    5 ++++-
 locate/bigram.c |    7 ++++++-
 locate/code.c   |    5 ++++-
 locate/frcode.c |    5 ++++-
 locate/locate.c |    6 ++++--
 xargs/xargs.c   |    7 +++++--
 8 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index d136ce8..104eb3e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2013-12-08  James Youngman  <jay@gnu.org>
+
+	Bug #35753: detect failure of the atexit library function.
+	* find/find.c (main): Issue a fatal error message if atexit fails.
+	* xargs/xargs.c (main): Likewise.
+	* find/ftsfind.c (main): Likewise.
+	* locate/code.c (main): Likewise.
+	* locate/frcode.c (main): Likewise.
+	* locate/locate.c (dolocate): Likewise.
+	* locate/bigram.c (main): Likewise; also include <errno.h>.
+
 2013-10-27  James Youngman  <jay@gnu.org>
 
 	* THANKS: Add Eric S. Raymond.
diff --git a/find/find.c b/find/find.c
index 10973b5..0dd4db1 100644
--- a/find/find.c
+++ b/find/find.c
@@ -214,7 +214,10 @@ main (int argc, char **argv)
 #endif
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
-  atexit (close_stdin);
+  if (atexit (close_stdin))
+    {
+      error (EXIT_FAILURE, errno, _("The atexit library function failed"));
+    }
 
   /* Check for -P, -H or -L options. */
   end_of_leading_options = process_leading_options (argc, argv);
diff --git a/find/ftsfind.c b/find/ftsfind.c
index e97bd7d..6decdd9 100644
--- a/find/ftsfind.c
+++ b/find/ftsfind.c
@@ -688,7 +688,10 @@ main (int argc, char **argv)
 
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
-  atexit (close_stdout);
+  if (atexit (close_stdout))
+    {
+      error (EXIT_FAILURE, errno, _("The atexit library function failed"));
+    }
 
   /* Check for -P, -H or -L options.  Also -D and -O, which are
    * both GNU extensions.
diff --git a/locate/bigram.c b/locate/bigram.c
index f60903f..e83e9b1 100644
--- a/locate/bigram.c
+++ b/locate/bigram.c
@@ -34,6 +34,7 @@
 #include <config.h>
 
 /* system headers. */
+#include <errno.h>
 #include <stdio.h>
 #include <locale.h>
 #include <string.h>
@@ -45,6 +46,7 @@
 #include "gettext.h"
 #include "progname.h"
 #include "xalloc.h"
+#include "error.h"
 
 /* find headers would go here but we don't need any. */
 
@@ -98,7 +100,10 @@ main (int argc, char **argv)
   textdomain (PACKAGE);
 
   (void) argc;
-  atexit (close_stdout);
+  if (atexit (close_stdout))
+    {
+      error (EXIT_FAILURE, errno, _("The atexit library function failed"));
+    }
 
   pathsize = oldpathsize = 1026; /* Increased as necessary by getline.  */
   path = xmalloc (pathsize);
diff --git a/locate/code.c b/locate/code.c
index 6588d5e..eae02ce 100644
--- a/locate/code.c
+++ b/locate/code.c
@@ -169,7 +169,10 @@ main (int argc, char **argv)
   int line_len;			/* Length of input line.  */
 
   set_program_name (argv[0]);
-  atexit (close_stdout);
+  if (atexit (close_stdout))
+    {
+      error (EXIT_FAILURE, errno, _("The atexit library function failed"));
+    }
 
   bigram[2] = '\0';
 
diff --git a/locate/frcode.c b/locate/frcode.c
index 35075c7..475b69a 100644
--- a/locate/frcode.c
+++ b/locate/frcode.c
@@ -228,7 +228,10 @@ main (int argc, char **argv)
   else
     set_program_name ("frcode");
 
-  atexit (close_stdout);
+  if (atexit (close_stdout))
+    {
+      error (EXIT_FAILURE, errno, _("The atexit library function failed"));
+    }
 
   pathsize = oldpathsize = 1026; /* Increased as necessary by getline.  */
   path = xmalloc (pathsize);
diff --git a/locate/locate.c b/locate/locate.c
index dc31d6a..bf602bd 100644
--- a/locate/locate.c
+++ b/locate/locate.c
@@ -1561,10 +1561,12 @@ dolocate (int argc, char **argv, int secure_db_fd)
 #endif
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
-  atexit (close_stdout);
 
   quote_opts = clone_quoting_options (NULL);
-  atexit (cleanup_quote_opts);
+  if (atexit (close_stdout) || atexit (cleanup_quote_opts))
+    {
+      error (EXIT_FAILURE, errno, _("The atexit library function failed"));
+    }
 
   limits.limit = 0;
   limits.items_accepted = 0;
diff --git a/xargs/xargs.c b/xargs/xargs.c
index 70ea506..460c359 100644
--- a/xargs/xargs.c
+++ b/xargs/xargs.c
@@ -386,8 +386,11 @@ main (int argc, char **argv)
 #endif
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
-  atexit (close_stdin);
-  atexit (wait_for_proc_all);
+
+  if (atexit (close_stdin) || atexit (wait_for_proc_all))
+    {
+      error (EXIT_FAILURE, errno, _("The atexit library function failed"));
+    }
 
   /* xargs is required by POSIX to allow 2048 bytes of headroom
    * for extra environment variables (that perhaps the utliity might
-- 
1.7.10.4

