mirror of
https://passt.top/passt
synced 2025-06-19 21:45:34 +02:00
util: Add abort_with_msg() and ASSERT_WITH_MSG() helpers
We already have the ASSERT() macro which will abort() passt based on a condition. It always has a fixed error message based on its location and the asserted expression. We have some upcoming cases where we want to customise the message when hitting an assert. Add abort_with_msg() and ASSERT_WITH_MSG() helpers to allow this. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
38bcce9977
commit
9153aca15b
2 changed files with 29 additions and 15 deletions
19
util.c
19
util.c
|
@ -1017,3 +1017,22 @@ void encode_domain_name(char *buf, const char *domain_name)
|
||||||
}
|
}
|
||||||
p[i] = 0L;
|
p[i] = 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* abort_with_msg() - Print error message and abort
|
||||||
|
* @fmt: Format string
|
||||||
|
* @...: Format parameters
|
||||||
|
*/
|
||||||
|
void abort_with_msg(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
vlogmsg(true, false, LOG_CRIT, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
/* This may actually cause a SIGSYS instead of SIGABRT, due to seccomp,
|
||||||
|
* but that will still get the job done.
|
||||||
|
*/
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
25
util.h
25
util.h
|
@ -61,27 +61,22 @@
|
||||||
#define STRINGIFY(x) #x
|
#define STRINGIFY(x) #x
|
||||||
#define STR(x) STRINGIFY(x)
|
#define STR(x) STRINGIFY(x)
|
||||||
|
|
||||||
#ifdef CPPCHECK_6936
|
void abort_with_msg(const char *fmt, ...)
|
||||||
|
__attribute__((format(printf, 1, 2), noreturn));
|
||||||
|
|
||||||
/* Some cppcheck versions get confused by aborts inside a loop, causing
|
/* Some cppcheck versions get confused by aborts inside a loop, causing
|
||||||
* it to give false positive uninitialised variable warnings later in
|
* it to give false positive uninitialised variable warnings later in
|
||||||
* the function, because it doesn't realise the non-initialising path
|
* the function, because it doesn't realise the non-initialising path
|
||||||
* already exited. See https://trac.cppcheck.net/ticket/13227
|
* already exited. See https://trac.cppcheck.net/ticket/13227
|
||||||
|
*
|
||||||
|
* Therefore, avoid using the usual do while wrapper we use to force the macro
|
||||||
|
* to act like a single statement requiring a ';'.
|
||||||
*/
|
*/
|
||||||
#define ASSERT(expr) \
|
#define ASSERT_WITH_MSG(expr, ...) \
|
||||||
((expr) ? (void)0 : abort())
|
((expr) ? (void)0 : abort_with_msg(__VA_ARGS__))
|
||||||
#else
|
|
||||||
#define ASSERT(expr) \
|
#define ASSERT(expr) \
|
||||||
do { \
|
ASSERT_WITH_MSG((expr), "ASSSERTION FAILED in %s (%s:%d): %s", \
|
||||||
if (!(expr)) { \
|
__func__, __FILE__, __LINE__, STRINGIFY(expr))
|
||||||
err("ASSERTION FAILED in %s (%s:%d): %s", \
|
|
||||||
__func__, __FILE__, __LINE__, STRINGIFY(expr)); \
|
|
||||||
/* This may actually SIGSYS, due to seccomp, \
|
|
||||||
* but that will still get the job done \
|
|
||||||
*/ \
|
|
||||||
abort(); \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef P_tmpdir
|
#ifdef P_tmpdir
|
||||||
#define TMPDIR P_tmpdir
|
#define TMPDIR P_tmpdir
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue