+2014-12-20 Paul Eggert <eggert@cs.ucla.edu>
+
+ 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 <eggert@cs.ucla.edu>
stdalign: port better to HP compilers
func_begin_table
func_module assert
+ func_module assure
func_module verify
func_end_table
--- /dev/null
+/* 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 <http://www.gnu.org/licenses/>. */
+
+/* Written by Paul Eggert. */
+
+#ifndef _GL_ASSURE_H
+#define _GL_ASSURE_H
+
+#include <assert.h>
+
+/* 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
#include "chdir-long.h"
-#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
+#include "assure.h"
+
#ifndef PATH_MAX
# error "compile this file only if your system defines PATH_MAX"
#endif
if (0 <= cdb->fd)
{
bool close_fail = close (cdb->fd);
- assert (! close_fail);
+ assure (! close_fail);
}
}
/* 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, "/");
dir += n_leading_slash;
}
- assert (*dir != '/');
- assert (dir <= dir_end);
+ assure (*dir != '/');
+ assure (dir <= dir_end);
while (PATH_MAX <= dir_end - dir)
{
}
*slash = '\0';
- assert (slash - dir < PATH_MAX);
+ assure (slash - dir < PATH_MAX);
err = cdb_advance_fd (&cdb, dir);
*slash = '/';
if (err != 0)
#include <config.h>
+#include "cycle-check.h"
+
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
-#include <assert.h>
#include <stdlib.h>
-
#include <stdbool.h>
-#include "cycle-check.h"
+#include "assure.h"
#define CC_MAGIC 9827862
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,
/* Specification. */
#include <unistd.h>
-#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include "assure.h"
#include "dosname.h"
#include "filenamecat.h"
{
struct stat statbuf;
- assert (0 <= fd);
+ assure (0 <= fd);
if (REPLACE_OPEN_DIRECTORY
|| (fstat (fd, &statbuf) == 0 && S_ISDIR (statbuf.st_mode)))
{
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. */
#endif
#ifdef NDEBUG
-# define fts_assert(expr) ((void) 0)
+# define fts_assert(expr) ((void) (0 && (expr)))
#else
# define fts_assert(expr) \
do \
#include <errno.h>
#include <limits.h>
-#include <assert.h>
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
# define WINDOWS_NATIVE
#include <time.h>
+#include "assure.h"
+
#ifndef INFTIM
# define INFTIM (-1)
#endif
continue;
h = (HANDLE) _get_osfhandle (pfd[i].fd);
- assert (h != NULL);
+ assure (h != NULL);
if (IsSocketHandle (h))
{
int requested = FD_CLOSE;
#include "savewd.h"
-#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
+#include "assure.h"
#include "dosname.h"
#include "fcntl-safer.h"
break;
default:
- assert (false);
+ assure (false);
}
return false;
break;
case FORKING_STATE:
- assert (wd->val.child == 0);
+ assure (wd->val.child == 0);
break;
default:
- assert (false);
+ assure (false);
}
}
}
{
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));
break;
default:
- assert (false);
+ assure (false);
}
return 0;
break;
case FORKING_STATE:
- assert (wd->val.child < 0);
+ assure (wd->val.child < 0);
break;
default:
- assert (false);
+ assure (false);
}
wd->state = FINAL_STATE;
#define _GL_UTIMENS_INLINE _GL_EXTERN_INLINE
#include "utimens.h"
-#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
{
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
need stderr defined if assertion checking is enabled. */
#include <stdio.h>
-#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
+#include "assure.h"
#include "intprops.h"
/* xstrtoll.c and xstrtoull.c, which include this file, require that
__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);
--- /dev/null
+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
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]
m4/cycle-check.m4
Depends-on:
+assure
dev-ino
same-inode
stdbool
Depends-on:
unistd
+assure [test $HAVE_FCHDIR = 0]
chdir [test $HAVE_FCHDIR = 0]
close [test $HAVE_FCHDIR = 0]
dirent [test $HAVE_FCHDIR = 0]
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]
m4/savewd.m4
Depends-on:
+assure
chdir
dosname
errno
m4/utimes.m4
Depends-on:
+assure
errno
extern-inline
fcntl-h
m4/xstrtol.m4
Depends-on:
+assure
exitfail
error
getopt-gnu