Implement our own assert() and, more importantly, assertf().

This commit is contained in:
Dag-Erling Smørgrav 2016-11-14 12:55:33 +01:00
parent 856571a06d
commit 2d507aaee4
8 changed files with 253 additions and 3 deletions

View file

@ -13,6 +13,8 @@ endif CRYB_CIPHER
if CRYB_CORE
cryb_HEADERS += \
algorithm.h \
assert.h \
attributes.h \
bitwise.h \
core.h \
ctype.h \

69
include/cryb/assert.h Normal file
View file

@ -0,0 +1,69 @@
/*-
* Copyright (c) 2016 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef CRYB_ASSERT_H_INCLUDED
#define CRYB_ASSERT_H_INCLUDED
#ifndef CRYB_TO
#include <cryb/to.h>
#endif
#ifndef CRYB_COVERAGE
#include <cryb/coverage.h>
#endif
#define assertion_failed cryb_assertion_failed
void assertion_failed(const char *, const char *, unsigned int,
const char *, ...) CRYB_NORETURN;
#ifndef CRYB_NDEBUG
#define assertf(exp, ...) \
do { \
CRYB_DISABLE_COVERAGE \
if (!(exp)) \
assertion_failed(__func__, __FILE__, \
__LINE__, __VA_ARGS__); \
CRYB_RESTORE_COVERAGE \
} while (0)
#define assert(exp) \
assertf(exp, "%s", #exp)
#else
#define assertf(exp, ...) \
((void)0)
#define assert(exp) \
((void)0)
#endif
#endif

View file

@ -3,7 +3,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include
lib_LTLIBRARIES = libcryb-core.la
libcryb_core_la_SOURCES = \
cryb_core.c \
cryb_assert.c \
cryb_memset_s.c \
cryb_string.c \
cryb_strlcat.c \
@ -12,7 +12,9 @@ libcryb_core_la_SOURCES = \
cryb_wcslcat.c \
cryb_wcslcmp.c \
cryb_wcslcpy.c \
cryb_wstring.c
cryb_wstring.c \
\
cryb_core.c
EXTRA_DIST = cryb_string_impl.c

53
lib/core/cryb_assert.c Normal file
View file

@ -0,0 +1,53 @@
/*-
* Copyright (c) 2016 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "cryb/impl.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <cryb/assert.h>
void
assertion_failed(const char *func, const char *file, unsigned int line,
const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "assertion failed in %s() on %s:%u\n",
func, file, line);
if (fmt != NULL) {
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
}
abort();
}

View file

@ -49,13 +49,13 @@
#endif
#endif
#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cryb/assert.h>
#include <cryb/test.h>
/*

1
t/.gitignore vendored
View file

@ -2,6 +2,7 @@
/*.trs
/t_adler
/t_aes
/t_assert
/t_cipher
/t_core
/t_ctype

View file

@ -46,6 +46,8 @@ endif CRYB_CIPHER
if CRYB_CORE
TESTS += t_core
t_core_LDADD = $(libt) $(libcore)
TESTS += t_assert
t_assert_LDADD = $(libt) $(libcore)
TESTS += t_ctype t_endian t_memset_s t_strlcat t_strlcmp t_strlcpy
t_ctype_LDADD = $(libt) $(libcore)
t_endian_LDADD = $(libt) $(libcore)

121
t/t_assert.c Normal file
View file

@ -0,0 +1,121 @@
/*-
* Copyright (c) 2016 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "cryb/impl.h"
#include <sys/types.h>
#include <sys/wait.h>
#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <cryb/assert.h>
#include <cryb/test.h>
/*
* Verify that an assertion failure causes a SIGABRT. Note that we can't
* just catch the signal, because abort(3) has other side effects (such as
* flushing and closing streams) from which we couldn't recover.
*/
static void
t_assert_child(void)
{
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE)
struct rlimit crl = { 0, 0 };
#endif
/* reset SIGABRT handler in case of shenanigans */
signal(SIGABRT, SIG_DFL);
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE)
/* prevent core dump */
if (setrlimit(RLIMIT_CORE, &crl) != 0)
t_verbose("failed to disable core dump\n");
#endif
/* suppress assertion message */
fclose(stderr);
assert(0);
}
int
t_assert_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
pid_t pid;
int signo, status;
t_assert((pid = fork()) != -1);
if (pid == 0) {
t_assert_child();
_exit(1);
}
t_assert(waitpid(pid, &status, 0) == pid);
if (!WIFSIGNALED(status)) {
t_verbose("expected child to raise a signal\n");
return (0);
}
#ifdef WCOREDUMP
if (WCOREDUMP(status))
t_verbose("warning: child dumped core\n");
#endif
if ((signo = WTERMSIG(status)) != SIGABRT) {
t_verbose("expected child to raise signal %d (SIGABRT), "
"got signal %d\n", SIGABRT, signo);
return (0);
}
return (1);
}
/***************************************************************************
* Boilerplate
*/
static int
t_prepare(int argc, char *argv[])
{
(void)argc;
(void)argv;
t_add_test(t_assert_fail, NULL, "assertion failed");
return (0);
}
int
main(int argc, char *argv[])
{
t_main(t_prepare, NULL, argc, argv);
}