aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil
diff options
context:
space:
mode:
authorJoão Valverde <j@v6e.pt>2022-01-04 12:20:21 +0000
committerA Wireshark GitLab Utility <gerald+gitlab-utility@wireshark.org>2022-01-05 13:31:53 +0000
commit7afb52c4793208728d82e4bf13f48304e3e9fbea (patch)
tree524d2b9fc18924ea84c99332f58342a2c90e882b /wsutil
parent1dfba751e5c4df442641278c3cd7e039b3593ba9 (diff)
ws_assert: refactor assertions
Diffstat (limited to 'wsutil')
-rw-r--r--wsutil/CMakeLists.txt1
-rw-r--r--wsutil/ws_assert.c30
-rw-r--r--wsutil/ws_assert.h24
-rw-r--r--wsutil/wslog.c13
-rw-r--r--wsutil/wslog.h10
5 files changed, 27 insertions, 51 deletions
diff --git a/wsutil/CMakeLists.txt b/wsutil/CMakeLists.txt
index 1ecdc66e75..3ad9950620 100644
--- a/wsutil/CMakeLists.txt
+++ b/wsutil/CMakeLists.txt
@@ -131,7 +131,6 @@ set(WSUTIL_COMMON_FILES
to_str.c
type_util.c
unicode-utils.c
- ws_assert.c
ws_getopt.c
ws_mempbrk.c
ws_pipe.c
diff --git a/wsutil/ws_assert.c b/wsutil/ws_assert.c
deleted file mode 100644
index 08afb4b21e..0000000000
--- a/wsutil/ws_assert.c
+++ /dev/null
@@ -1,30 +0,0 @@
-/* ws_assert.c
- *
- * Wireshark - Network traffic analyzer
- * By Gerald Combs <gerald@wireshark.org>
- * Copyright 1998 Gerald Combs
- *
- * SPDX-License-Identifier: GPL-2.0-or-later
- */
-
-#include "config.h"
-
-#include "ws_assert.h"
-#include <stdlib.h>
-
-#include "wslog.h"
-
-
-void ws_assert_failed(const char *file, long line, const char *function,
- const char *domain, const char *assertion,
- bool unreachable)
-{
- if (unreachable)
- ws_log_full(domain, LOG_LEVEL_ERROR, file, line, function,
- "assertion \"not reached\" failed");
- else
- ws_log_full(domain, LOG_LEVEL_ERROR, file, line, function,
- "assertion failed: %s", assertion);
-
- abort();
-}
diff --git a/wsutil/ws_assert.h b/wsutil/ws_assert.h
index ce75c1e8cc..f65293cdd2 100644
--- a/wsutil/ws_assert.h
+++ b/wsutil/ws_assert.h
@@ -14,12 +14,7 @@
#include <ws_attributes.h>
#include <stdbool.h>
#include <string.h>
-
-#ifdef WS_LOG_DOMAIN
-#define _ASSERT_DOMAIN WS_LOG_DOMAIN
-#else
-#define _ASSERT_DOMAIN ""
-#endif
+#include <wsutil/wslog.h>
#ifdef WS_DISABLE_ASSERT
#define _ASSERT_ENABLED false
@@ -31,12 +26,6 @@
extern "C" {
#endif /* __cplusplus */
-WS_DLL_PUBLIC
-WS_NORETURN
-void ws_assert_failed(const char *file, long line, const char *function,
- const char *domain, const char *assertion,
- bool unreachable);
-
/*
* We don't want to execute the expression with WS_DISABLE_ASSERT because
* it might be time and space costly and the goal here is to optimize for
@@ -45,11 +34,9 @@ void ws_assert_failed(const char *file, long line, const char *function,
* if (false) and let the compiler optimize away the dead execution branch.
*/
#define _ASSERT_IF_ACTIVE(active, expr) \
- do { \
- if ((active) && !(expr)) { \
- ws_assert_failed(__FILE__, __LINE__, __func__, \
- _ASSERT_DOMAIN, #expr, false); \
- } \
+ do { \
+ if ((active) && !(expr)) \
+ ws_error("assertion failed: %s", #expr); \
} while (0)
/*
@@ -86,8 +73,7 @@ void ws_assert_failed(const char *file, long line, const char *function,
* ws_assert_not_reached(). There is no reason to ever use a no-op here.
*/
#define ws_assert_not_reached() \
- ws_assert_failed(__FILE__, __LINE__, __func__, \
- _ASSERT_DOMAIN, NULL, true)
+ ws_error("assertion \"not reached\" failed")
#ifdef __cplusplus
}
diff --git a/wsutil/wslog.c b/wsutil/wslog.c
index 5f52374403..1f12e443bc 100644
--- a/wsutil/wslog.c
+++ b/wsutil/wslog.c
@@ -1039,6 +1039,19 @@ void ws_log_full(const char *domain, enum ws_log_level level,
}
+void ws_log_fatal_full(const char *domain, enum ws_log_level level,
+ const char *file, long line, const char *func,
+ const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ log_write_dispatch(domain, level, file, line, func, format, ap);
+ va_end(ap);
+ abort();
+}
+
+
void ws_log_write_always_full(const char *domain, enum ws_log_level level,
const char *file, long line, const char *func,
const char *format, ...)
diff --git a/wsutil/wslog.h b/wsutil/wslog.h
index 2f68a04f67..36810a5060 100644
--- a/wsutil/wslog.h
+++ b/wsutil/wslog.h
@@ -275,6 +275,13 @@ void ws_logv_full(const char *domain, enum ws_log_level level,
const char *format, va_list ap);
+WS_DLL_PUBLIC
+WS_NORETURN
+void ws_log_fatal_full(const char *domain, enum ws_log_level level,
+ const char *file, long line, const char *func,
+ const char *format, ...) G_GNUC_PRINTF(6,7);
+
+
/*
* The if condition avoids -Wunused warnings for variables used only with
* !WS_DISABLE_DEBUG, typically inside a ws_debug() call. The compiler will
@@ -302,7 +309,8 @@ void ws_logv_full(const char *domain, enum ws_log_level level,
* "error" is always fatal and terminates the program with a coredump.
*/
#define ws_error(...) \
- _LOG_FULL(true, LOG_LEVEL_ERROR, __VA_ARGS__)
+ ws_log_fatal_full(_LOG_DOMAIN, LOG_LEVEL_ERROR, \
+ __FILE__, __LINE__, __func__, __VA_ARGS__)
/** Logs with "critical" level.
*