>From 972fd2a46979d17458eb9002354520163f410399 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 20 Dec 2014 13:00:21 -0800 Subject: [PATCH] assure: new module This works better than 'assert' when compiling with -DNDEBUG, as it avoids some compiler diagnostics in that case. Reported by Norihiro Tanaka in: http://lists.gnu.org/archive/html/bug-gnulib/2014-12/msg00215.html * MODULES.html.sh (func_all_modules): Add 'assure'. * lib/assure.h, modules/assure: New files. * lib/chdir-long.c, lib/cycle-check.c, lib/fchdir.c, lib/fts.c: * lib/poll.c, lib/savewd.c, lib/utimens.c, lib/xstrtol.c: Prefer 'assure' to 'assert'. * modules/chdir-long, modules/cycle-check, modules/fchdir: * modules/poll, modules/savewd, modules/utimens, modules/xstrtol: Depend on 'assure'. --- ChangeLog | 16 ++++++++++++++++ MODULES.html.sh | 1 + lib/assure.h | 37 +++++++++++++++++++++++++++++++++++++ lib/chdir-long.c | 15 ++++++++------- lib/cycle-check.c | 8 ++++---- lib/fchdir.c | 6 +++--- lib/fts.c | 2 +- lib/poll.c | 5 +++-- lib/savewd.c | 16 ++++++++-------- lib/utimens.c | 2 -- lib/xstrtol.c | 4 ++-- modules/assure | 20 ++++++++++++++++++++ modules/chdir-long | 1 + modules/cycle-check | 1 + modules/fchdir | 1 + modules/poll | 1 + modules/savewd | 1 + modules/utimens | 1 + modules/xstrtol | 1 + 19 files changed, 110 insertions(+), 29 deletions(-) create mode 100644 lib/assure.h create mode 100644 modules/assure diff --git a/ChangeLog b/ChangeLog index 877a721..3cf10e5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2014-12-20 Paul Eggert + + assure: new module + This works better than 'assert' when compiling with -DNDEBUG, + as it avoids some compiler diagnostics in that case. + Reported by Norihiro Tanaka in: + http://lists.gnu.org/archive/html/bug-gnulib/2014-12/msg00215.html + * MODULES.html.sh (func_all_modules): Add 'assure'. + * lib/assure.h, modules/assure: New files. + * lib/chdir-long.c, lib/cycle-check.c, lib/fchdir.c, lib/fts.c: + * lib/poll.c, lib/savewd.c, lib/utimens.c, lib/xstrtol.c: + Prefer 'assure' to 'assert'. + * modules/chdir-long, modules/cycle-check, modules/fchdir: + * modules/poll, modules/savewd, modules/utimens, modules/xstrtol: + Depend on 'assure'. + 2014-12-16 Paul Eggert stdalign: port better to HP compilers diff --git a/MODULES.html.sh b/MODULES.html.sh index db66253..ce76223 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -1663,6 +1663,7 @@ func_all_modules () func_begin_table func_module assert + func_module assure func_module verify func_end_table diff --git a/lib/assure.h b/lib/assure.h new file mode 100644 index 0000000..a53e55f --- /dev/null +++ b/lib/assure.h @@ -0,0 +1,37 @@ +/* Run-time assert-like macros. + + Copyright (C) 2014 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 . */ + +/* Written by Paul Eggert. */ + +#ifndef _GL_ASSURE_H +#define _GL_ASSURE_H + +#include + +/* Check E's value at runtime, and report an error and abort if not. + However, do nothng if NDEBUG is defined. + + Unlike standard 'assert', this macro always compiles E even when NDEBUG + is defined, so as to catch typos and avoid some GCC warnings. */ + +#ifdef NDEBUG +# define assure(E) ((void) (0 && (E))) +#else +# define assure(E) assert (E) +#endif + +#endif diff --git a/lib/chdir-long.c b/lib/chdir-long.c index 5b1b18f..14c6733 100644 --- a/lib/chdir-long.c +++ b/lib/chdir-long.c @@ -20,7 +20,6 @@ #include "chdir-long.h" -#include #include #include #include @@ -28,6 +27,8 @@ #include #include +#include "assure.h" + #ifndef PATH_MAX # error "compile this file only if your system defines PATH_MAX" #endif @@ -60,7 +61,7 @@ cdb_free (struct cd_buf const *cdb) if (0 <= cdb->fd) { bool close_fail = close (cdb->fd); - assert (! close_fail); + assure (! close_fail); } } @@ -122,8 +123,8 @@ chdir_long (char *dir) /* If DIR is the empty string, then the chdir above must have failed and set errno to ENOENT. */ - assert (0 < len); - assert (PATH_MAX <= len); + assure (0 < len); + assure (PATH_MAX <= len); /* Count leading slashes. */ n_leading_slash = strspn (dir, "/"); @@ -158,8 +159,8 @@ chdir_long (char *dir) dir += n_leading_slash; } - assert (*dir != '/'); - assert (dir <= dir_end); + assure (*dir != '/'); + assure (dir <= dir_end); while (PATH_MAX <= dir_end - dir) { @@ -175,7 +176,7 @@ chdir_long (char *dir) } *slash = '\0'; - assert (slash - dir < PATH_MAX); + assure (slash - dir < PATH_MAX); err = cdb_advance_fd (&cdb, dir); *slash = '/'; if (err != 0) diff --git a/lib/cycle-check.c b/lib/cycle-check.c index f7b3d07..95a9bed 100644 --- a/lib/cycle-check.c +++ b/lib/cycle-check.c @@ -19,15 +19,15 @@ #include +#include "cycle-check.h" + #include #include #include -#include #include - #include -#include "cycle-check.h" +#include "assure.h" #define CC_MAGIC 9827862 @@ -57,7 +57,7 @@ cycle_check_init (struct cycle_check_state *state) bool cycle_check (struct cycle_check_state *state, struct stat const *sb) { - assert (state->magic == CC_MAGIC); + assure (state->magic == CC_MAGIC); /* If the current directory ever happens to be the same as the one we last recorded for the cycle detection, diff --git a/lib/fchdir.c b/lib/fchdir.c index 5d71377..ab0be7a 100644 --- a/lib/fchdir.c +++ b/lib/fchdir.c @@ -19,7 +19,6 @@ /* Specification. */ #include -#include #include #include #include @@ -29,6 +28,7 @@ #include #include +#include "assure.h" #include "dosname.h" #include "filenamecat.h" @@ -132,7 +132,7 @@ _gl_register_fd (int fd, const char *filename) { struct stat statbuf; - assert (0 <= fd); + assure (0 <= fd); if (REPLACE_OPEN_DIRECTORY || (fstat (fd, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))) { @@ -156,7 +156,7 @@ _gl_register_fd (int fd, const char *filename) int _gl_register_dup (int oldfd, int newfd) { - assert (0 <= oldfd && 0 <= newfd && oldfd != newfd); + assure (0 <= oldfd && 0 <= newfd && oldfd != newfd); if (oldfd < dirs_allocated && dirs[oldfd].name) { /* Duplicated a directory; must ensure newfd is allocated. */ diff --git a/lib/fts.c b/lib/fts.c index 9851c53..4e136c9 100644 --- a/lib/fts.c +++ b/lib/fts.c @@ -189,7 +189,7 @@ enum Fts_stat #endif #ifdef NDEBUG -# define fts_assert(expr) ((void) 0) +# define fts_assert(expr) ((void) (0 && (expr))) #else # define fts_assert(expr) \ do \ diff --git a/lib/poll.c b/lib/poll.c index a3e0ab7..c2e5b42 100644 --- a/lib/poll.c +++ b/lib/poll.c @@ -33,7 +33,6 @@ #include #include -#include #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ # define WINDOWS_NATIVE @@ -59,6 +58,8 @@ #include +#include "assure.h" + #ifndef INFTIM # define INFTIM (-1) #endif @@ -480,7 +481,7 @@ restart: continue; h = (HANDLE) _get_osfhandle (pfd[i].fd); - assert (h != NULL); + assure (h != NULL); if (IsSocketHandle (h)) { int requested = FD_CLOSE; diff --git a/lib/savewd.c b/lib/savewd.c index 88c5fef..e8c1c2d 100644 --- a/lib/savewd.c +++ b/lib/savewd.c @@ -23,7 +23,6 @@ #include "savewd.h" -#include #include #include #include @@ -33,6 +32,7 @@ #include #include +#include "assure.h" #include "dosname.h" #include "fcntl-safer.h" @@ -88,7 +88,7 @@ savewd_save (struct savewd *wd) break; default: - assert (false); + assure (false); } return false; @@ -144,11 +144,11 @@ savewd_chdir (struct savewd *wd, char const *dir, int options, break; case FORKING_STATE: - assert (wd->val.child == 0); + assure (wd->val.child == 0); break; default: - assert (false); + assure (false); } } } @@ -205,7 +205,7 @@ savewd_restore (struct savewd *wd, int status) { int child_status; while (waitpid (child, &child_status, 0) < 0) - assert (errno == EINTR); + assure (errno == EINTR); wd->val.child = -1; if (! WIFEXITED (child_status)) raise (WTERMSIG (child_status)); @@ -215,7 +215,7 @@ savewd_restore (struct savewd *wd, int status) break; default: - assert (false); + assure (false); } return 0; @@ -236,11 +236,11 @@ savewd_finish (struct savewd *wd) break; case FORKING_STATE: - assert (wd->val.child < 0); + assure (wd->val.child < 0); break; default: - assert (false); + assure (false); } wd->state = FINAL_STATE; diff --git a/lib/utimens.c b/lib/utimens.c index dd3ec66..0444103 100644 --- a/lib/utimens.c +++ b/lib/utimens.c @@ -24,7 +24,6 @@ #define _GL_UTIMENS_INLINE _GL_EXTERN_INLINE #include "utimens.h" -#include #include #include #include @@ -87,7 +86,6 @@ validate_timespec (struct timespec timespec[2]) { int result = 0; int utime_omit_count = 0; - assert (timespec); if ((timespec[0].tv_nsec != UTIME_NOW && timespec[0].tv_nsec != UTIME_OMIT && ! (0 <= timespec[0].tv_nsec diff --git a/lib/xstrtol.c b/lib/xstrtol.c index f6d535a..544a74a 100644 --- a/lib/xstrtol.c +++ b/lib/xstrtol.c @@ -34,13 +34,13 @@ need stderr defined if assertion checking is enabled. */ #include -#include #include #include #include #include #include +#include "assure.h" #include "intprops.h" /* xstrtoll.c and xstrtoull.c, which include this file, require that @@ -93,7 +93,7 @@ __xstrtol (const char *s, char **ptr, int strtol_base, __strtol_t tmp; strtol_error err = LONGINT_OK; - assert (0 <= strtol_base && strtol_base <= 36); + assure (0 <= strtol_base && strtol_base <= 36); p = (ptr ? ptr : &t_ptr); diff --git a/modules/assure b/modules/assure new file mode 100644 index 0000000..3cfe1f8 --- /dev/null +++ b/modules/assure @@ -0,0 +1,20 @@ +Description: +Run-time assert-like macros. + +Files: +lib/assure.h + +Depends-on: + +configure.ac: + +Makefile.am: + +Include: +"assure.h" + +License: +LGPLv2+ + +Maintainer: +Paul Eggert, Jim Meyering diff --git a/modules/chdir-long b/modules/chdir-long index 74d9c33..82d4e8b 100644 --- a/modules/chdir-long +++ b/modules/chdir-long @@ -11,6 +11,7 @@ Depends-on: unistd pathmax chdir +assure [test $gl_cv_have_arbitrary_file_name_length_limit = yes] atexit [test $gl_cv_have_arbitrary_file_name_length_limit = yes] fchdir [test $gl_cv_have_arbitrary_file_name_length_limit = yes] fcntl-h [test $gl_cv_have_arbitrary_file_name_length_limit = yes] diff --git a/modules/cycle-check b/modules/cycle-check index 6a8618a..e8ef2ca 100644 --- a/modules/cycle-check +++ b/modules/cycle-check @@ -7,6 +7,7 @@ lib/cycle-check.h m4/cycle-check.m4 Depends-on: +assure dev-ino same-inode stdbool diff --git a/modules/fchdir b/modules/fchdir index af6cfa5..6e95e9a 100644 --- a/modules/fchdir +++ b/modules/fchdir @@ -7,6 +7,7 @@ m4/fchdir.m4 Depends-on: unistd +assure [test $HAVE_FCHDIR = 0] chdir [test $HAVE_FCHDIR = 0] close [test $HAVE_FCHDIR = 0] dirent [test $HAVE_FCHDIR = 0] diff --git a/modules/poll b/modules/poll index 8fa88fd..b1d928d 100644 --- a/modules/poll +++ b/modules/poll @@ -8,6 +8,7 @@ m4/poll.m4 Depends-on: poll-h alloca [test $HAVE_POLL = 0 || test $REPLACE_POLL = 1] +assure [test $HAVE_POLL = 0 || test $REPLACE_POLL = 1] select [test $HAVE_POLL = 0 || test $REPLACE_POLL = 1] sockets [test $HAVE_POLL = 0 || test $REPLACE_POLL = 1] sys_select [test $HAVE_POLL = 0 || test $REPLACE_POLL = 1] diff --git a/modules/savewd b/modules/savewd index b7a9fee..a6c8267 100644 --- a/modules/savewd +++ b/modules/savewd @@ -7,6 +7,7 @@ lib/savewd.c m4/savewd.m4 Depends-on: +assure chdir dosname errno diff --git a/modules/utimens b/modules/utimens index 8797774..4aed50a 100644 --- a/modules/utimens +++ b/modules/utimens @@ -9,6 +9,7 @@ m4/utimens.m4 m4/utimes.m4 Depends-on: +assure errno extern-inline fcntl-h diff --git a/modules/xstrtol b/modules/xstrtol index 66e5342..9c89aef 100644 --- a/modules/xstrtol +++ b/modules/xstrtol @@ -9,6 +9,7 @@ lib/xstrtol-error.c m4/xstrtol.m4 Depends-on: +assure exitfail error getopt-gnu -- 1.9.3