bug-findutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 04/19] Fix some compilation unused-result and missing-declaration


From: James Youngman
Subject: [PATCH 04/19] Fix some compilation unused-result and missing-declaration warnings.
Date: Fri, 2 Apr 2010 23:47:04 +0100

Eliminate some unused function result warnings.
* lib/unused-result.h: New file, defines function attribute macro
__attribute_warn_unused_result__.
* lib/Makefile.am (EXTRA_DIST): Add unused-result.h.
* find/ftsfind.c (find): Issue a diagnostic if fts_close fails,
change return type to bool and return false for this case.  Add
__warn_unused_result__ attribute.
(process_all_startpoints): Likewise (except no need for second
diagnostic).
(main): If process_all_startpoints failed, don't do the cleanup
operations because we don't know what subdirectory we're in.

* find/util.c (fd_leak_check_is_enabled): Avoid implicit
pointer-to-int conversion.
* lib/buildcmd.c: #include <stdlib.h> for declaration of free.

Signed-off-by: James Youngman <address@hidden>
---
 ChangeLog           |   16 ++++++++++++++++
 find/ftsfind.c      |   51 +++++++++++++++++++++++++++++++++++++++------------
 find/util.c         |    8 ++++++--
 lib/Makefile.am     |    2 +-
 lib/unused-result.h |   39 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 101 insertions(+), 15 deletions(-)
 create mode 100644 lib/unused-result.h

diff --git a/ChangeLog b/ChangeLog
index 5f87420..9c656f4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,21 @@
 2010-04-02  James Youngman  <address@hidden>
 
+       Eliminate some unused function result warnings.
+       * lib/unused-result.h: New file, defines function attribute macro
+       __attribute_warn_unused_result__.
+       * lib/Makefile.am (EXTRA_DIST): Add unused-result.h.
+       * find/ftsfind.c (find): Issue a diagnostic if fts_close fails,
+       change return type to bool and return false for this case.  Add
+       __warn_unused_result__ attribute.
+       (process_all_startpoints): Likewise (except no need for second
+       diagnostic).
+       (main): If process_all_startpoints failed, don't do the cleanup
+       operations because we don't know what subdirectory we're in.
+
+       * find/util.c (fd_leak_check_is_enabled): Avoid implicit
+       pointer-to-int conversion.
+       * lib/buildcmd.c: #include <stdlib.h> for declaration of free.
+
        * NEWS: Change space-tab sequences to just spaces.
        * build-aux/check-testfiles.sh (main): Likewise
        * build-aux/src-sniff.py (checkers): Likewise.
diff --git a/find/ftsfind.c b/find/ftsfind.c
index 492728b..5566805 100644
--- a/find/ftsfind.c
+++ b/find/ftsfind.c
@@ -55,6 +55,7 @@
 #include "dircallback.h"
 #include "cloexec.h"
 #include "fdleak.h"
+#include "unused-result.h"
 
 #ifdef HAVE_LOCALE_H
 #include <locale.h>
@@ -84,6 +85,11 @@ static int ftsoptions = 
FTS_NOSTAT|FTS_TIGHT_CYCLE_CHECK|FTS_CWDFD;
 static int prev_depth = INT_MIN; /* fts_level can be < 0 */
 static int curr_fd = -1;
 
+
+static bool find (char *arg) __attribute_warn_unused_result__;
+static bool process_all_startpoints (int argc, char *argv[]) 
__attribute_warn_unused_result__;
+
+
 int
 get_current_dirfd (void)
 {
@@ -574,7 +580,7 @@ consider_visiting (FTS *p, FTSENT *ent)
 
 
 
-static void
+static bool
 find (char *arg)
 {
   char * arglist[2];
@@ -620,13 +626,24 @@ find (char *arg)
          state.type = state.have_type ? ent->fts_statp->st_mode : 0;
          consider_visiting (p, ent);
        }
-      fts_close (p);
+      if (0 != fts_close (p))
+       {
+         /* Here we break the abstraction of fts_close a bit, because we
+          * are going to skip the rest of the start points, and return with
+          * nonzero exit status.  Hence we need to issue a diagnostic on
+          * stderr. */
+         error (0, errno,
+                _("failed to restore working directory after searching %s"),
+                arg);
+         return false;
+       }
       p = NULL;
     }
+  return true;
 }
 
 
-static void
+static bool
 process_all_startpoints (int argc, char *argv[])
 {
   int i;
@@ -635,7 +652,8 @@ process_all_startpoints (int argc, char *argv[])
   for (i = 0; i < argc && !looks_like_expression (argv[i], true); i++)
     {
       state.starting_path_length = strlen (argv[i]); /* TODO: is this 
redundant? */
-      find (argv[i]);
+      if (!find (argv[i]))
+       return false;
     }
 
   if (i == 0)
@@ -647,8 +665,9 @@ process_all_startpoints (int argc, char *argv[])
        * "find -printf %H" (note, not "find . -printf %H").
        */
       char defaultpath[2] = ".";
-      find (defaultpath);
+      return find (defaultpath);
     }
+  return true;
 }
 
 
@@ -743,14 +762,22 @@ main (int argc, char **argv)
        error (EXIT_FAILURE, errno, _("cannot get current directory"));
     }
 
-  process_all_startpoints (argc-end_of_leading_options, 
argv+end_of_leading_options);
-
-  /* If "-exec ... {} +" has been used, there may be some
-   * partially-full command lines which have been built,
-   * but which are not yet complete.   Execute those now.
+  /* process_all_startpoints processes the starting points named on
+   * the command line.  A false return value from it means that we
+   * failed to restore the original context.  That means it would not
+   * be safe to call cleanup() since we might complete an execdir in
+   * the wrong directory for example.
    */
-  show_success_rates (eval_tree);
-  cleanup ();
+  if (process_all_startpoints (argc-end_of_leading_options,
+                              argv+end_of_leading_options))
+    {
+      /* If "-exec ... {} +" has been used, there may be some
+       * partially-full command lines which have been built,
+       * but which are not yet complete.   Execute those now.
+       */
+      show_success_rates (eval_tree);
+      cleanup ();
+    }
   return state.exit_status;
 }
 
diff --git a/find/util.c b/find/util.c
index 31193d1..2538793 100644
--- a/find/util.c
+++ b/find/util.c
@@ -31,11 +31,11 @@
 #include <errno.h>
 #include <assert.h>
 
-#include "xalloc.h"
 #include "quotearg.h"
 #include "timespec.h"
 #include "error.h"
 #include "verify.h"
+#include "fdleak.h"
 
 
 #if ENABLE_NLS
@@ -451,7 +451,11 @@ undangle_file_pointers (struct predicate *p)
 int
 fd_leak_check_is_enabled (void)
 {
-  return getenv ("GNU_FINDUTILS_FD_LEAK_CHECK");
+  if (getenv ("GNU_FINDUTILS_FD_LEAK_CHECK"))
+    return 1;
+  else
+    return 0;
+
 }
 
 /* Complete any outstanding commands.
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 4a3d7f7..57dec25 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -21,7 +21,7 @@ endif
 libfind_a_SOURCES = gnulib-version.c findutils-version.c
 EXTRA_DIST = extendbuf.h savedirinfo.h buildcmd.h \
        gnulib-version.h gnulib-version.c findutils-version.h \
-       fdleak.h check-regexprops.sh
+       fdleak.h unused-result.h check-regexprops.sh
 BUILT_SOURCES = gnulib-version.c
 SUFFIXES =
 MOSTLYCLEANFILES =
diff --git a/lib/unused-result.h b/lib/unused-result.h
new file mode 100644
index 0000000..0aa05af
--- /dev/null
+++ b/lib/unused-result.h
@@ -0,0 +1,39 @@
+/* unused-result.h -- macros for ensuring callers don't ignore return values
+   Copyright (C) 2010, 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/>.
+*/
+
+/* Taken from coreutils' fts_.h */
+#ifndef _UNUSED_RESULT_H
+# define _UNUSED_RESULT_H 1
+
+# ifndef __GNUC_PREREQ
+#  if defined __GNUC__ && defined __GNUC_MINOR__
+#   define __GNUC_PREREQ(maj, min) \
+          ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
+#  else
+#   define __GNUC_PREREQ(maj, min) 0
+#  endif
+# endif
+
+# if __GNUC_PREREQ (3,4)
+#  undef __attribute_warn_unused_result__
+#  define __attribute_warn_unused_result__ \
+    __attribute__ ((__warn_unused_result__))
+# else
+#  define __attribute_warn_unused_result__ /* empty */
+# endif
+
+#endif
-- 
1.7.0





reply via email to

[Prev in Thread] Current Thread [Next in Thread]