From f999e1350d0f0f18f6a7b297cb57db398f963878 Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Sat, 20 Mar 1999 03:13:24 +0000 Subject: [PATCH] Provide global assert solution. (new) is now included by portable.h with NDEBUG undefined. This makes assert() is always available and automatically disables itself when LDAP_DEBUG is undefined. I've included a basic assert() for pre-STDC compilers. It relies on abort() which may not actually be available. (well replace abort() with whatever is appropriate if and when we're faced with a pre-STDC compiler that doesn't have assert()). --- include/ac/assert.h | 41 +++++++++++++++++++++++++++++++++++ include/portable.h.in | 13 +++++++++++ libraries/liblber/Makefile.in | 4 ++-- libraries/liblber/assert.c | 34 +++++++++++++++++++++++++++++ libraries/liblber/io.c | 6 ----- libraries/liblber/sockbuf.c | 5 +---- servers/slapd/daemon.c | 9 +------- servers/slapd/operation.c | 2 -- servers/slapd/slap.h | 3 --- 9 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 include/ac/assert.h create mode 100644 libraries/liblber/assert.c diff --git a/include/ac/assert.h b/include/ac/assert.h new file mode 100644 index 0000000000..7a9044dab0 --- /dev/null +++ b/include/ac/assert.h @@ -0,0 +1,41 @@ +/* Generic assert.h */ +/* + * Copyright 1999 The OpenLDAP Foundation, Redwood City, California, USA + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted only + * as authorized by the OpenLDAP Public License. A copy of this + * license is available at http://www.OpenLDAP.org/license.html or + * in file LICENSE in the top-level directory of the distribution. + */ + +#ifndef _AC_ASSERT_H +#define _AC_ASSERT_H + +#ifdef LDAP_DEBUG + +#if defined( HAVE_ASSERT_H ) || defined( STDC_HEADERS ) +#undef NDEBUG +#include +#else +#define LDAP_NEED_ASSERT 1 + +/* + * no assert()... must be a very old compiler. + * create a replacement and hope it works + */ + +void lber_pvt_assert(char* file, int line, char* test); +#define assert(test) \ + ((test) \ + ? (void)0 \ + : lber_pvt_assert( __FILE__, __LINE__, LDAP_STRING(test)) ) + +#endif + +#else +/* no asserts */ +#define assert(test) ((void)0) +#endif + +#endif /* _AC_ASSERT_H */ diff --git a/include/portable.h.in b/include/portable.h.in index 8e07edabc8..f478631394 100644 --- a/include/portable.h.in +++ b/include/portable.h.in @@ -680,6 +680,17 @@ /* begin of postamble */ +/* + * DEVEL implies TEST implies DEBUG + */ +#if !defined( LDAP_TEST) && defined( LDAP_DEVEL ) +#define LDAP_TEST +#endif + +#if !defined( LDAP_DEBUG) && defined( LDAP_TEST ) +#define LDAP_DEBUG +#endif + #ifdef HAVE_STDDEF_H # include #endif @@ -687,4 +698,6 @@ #include "ldap_cdefs.h" #include "ldap_features.h" +#include "ac/assert.h" + #endif /* _LDAP_PORTABLE_H */ diff --git a/libraries/liblber/Makefile.in b/libraries/liblber/Makefile.in index 9a17b3335b..1b5523d2f6 100644 --- a/libraries/liblber/Makefile.in +++ b/libraries/liblber/Makefile.in @@ -7,8 +7,8 @@ LIBRARY = liblber.la XLIBRARY = ../liblber.a -SRCS= decode.c encode.c io.c bprint.c options.c sockbuf.c -OBJS= decode.lo encode.lo io.lo bprint.lo options.lo sockbuf.lo +SRCS= assert.c decode.c encode.c io.c bprint.c options.c sockbuf.c +OBJS= assert.lo decode.lo encode.lo io.lo bprint.lo options.lo sockbuf.lo XSRCS= version.c PROGRAMS= dtest etest idtest diff --git a/libraries/liblber/assert.c b/libraries/liblber/assert.c new file mode 100644 index 0000000000..1ee56cc3e8 --- /dev/null +++ b/libraries/liblber/assert.c @@ -0,0 +1,34 @@ +/* + * Copyright 1999 The OpenLDAP Foundation, Redwood City, California, USA + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted only + * as authorized by the OpenLDAP Public License. A copy of this + * license is available at http://www.OpenLDAP.org/license.html or + * in file LICENSE in the top-level directory of the distribution. + */ + +#include "portable.h" + +#ifdef LDAP_NEED_ASSERT + +#include + +/* + * helper for our private assert() macro + * + * note: if assert() doesn't exist, like abort() or raise() won't either. + * could use kill() but that might be problematic. I'll just ignore this + * issue for now. + */ + +void lber_pvt_assert(char* file, int line, char* test) +{ + fprintf(stderr, + "Assertion failed: %s, file %s, line %d\n", + test, file, line); + + abort(); +} + +#endif diff --git a/libraries/liblber/io.c b/libraries/liblber/io.c index 7d529500d7..92d4242418 100644 --- a/libraries/liblber/io.c +++ b/libraries/liblber/io.c @@ -32,12 +32,6 @@ #include "lber-int.h" -#ifdef LDAP_DEBUG -#include -#else -#define assert(cond) -#endif - static long BerRead LDAP_P(( Sockbuf *sb, char *buf, long len )); static int ber_realloc LDAP_P(( BerElement *ber, unsigned long len )); diff --git a/libraries/liblber/sockbuf.c b/libraries/liblber/sockbuf.c index c693fdc2de..d07926e08e 100644 --- a/libraries/liblber/sockbuf.c +++ b/libraries/liblber/sockbuf.c @@ -27,12 +27,9 @@ #include "lber-int.h" -#ifdef LDAP_DEBUG -#include +#ifdef LDAP_TEST #undef TEST_PARTIAL_READ #undef TEST_PARTIAL_WRITE -#else -#define assert( cond ) #endif #define MAX_BUF_SIZE 65535 diff --git a/servers/slapd/daemon.c b/servers/slapd/daemon.c index e55eaf1d08..4596840b00 100644 --- a/servers/slapd/daemon.c +++ b/servers/slapd/daemon.c @@ -26,12 +26,6 @@ #include #endif -#ifdef LDAP_DEBUG -#include -#else -#define assert( cond ) -#endif - #ifdef HAVE_TCPD #include @@ -165,9 +159,8 @@ slapd_daemon( if ( (c[i].c_state != SLAP_C_INACTIVE) && (c[i].c_state != SLAP_C_CLOSING) ) { -#ifdef LDAP_DEBUG assert(lber_pvt_sb_in_use( &c[i].c_sb )); -#endif + FD_SET( lber_pvt_sb_get_desc(&c[i].c_sb), &readfds ); if (lber_pvt_sb_data_ready(&c[i].c_sb)) diff --git a/servers/slapd/operation.c b/servers/slapd/operation.c index 404e20d888..7848357a93 100644 --- a/servers/slapd/operation.c +++ b/servers/slapd/operation.c @@ -13,9 +13,7 @@ void slap_op_free( Operation *op ) { -#ifdef LDAP_DEBUG assert( op->o_next == NULL ); -#endif ldap_pvt_thread_mutex_lock( &op->o_abandonmutex ); diff --git a/servers/slapd/slap.h b/servers/slapd/slap.h index c9e9ad7338..837de9025b 100644 --- a/servers/slapd/slap.h +++ b/servers/slapd/slap.h @@ -13,9 +13,6 @@ #include #include -#undef NDEBUG -#include - #include "avl.h" #ifndef ldap_debug