3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
10 #ifndef __WS_ASSERT_H__
11 #define __WS_ASSERT_H__
13 #include <ws_symbol_export.h>
14 #include <ws_attributes.h>
17 #include <wsutil/wslog.h>
18 #include <wsutil/wmem/wmem.h>
21 * XXX - WS_ASSERT_ENABLED is tested in various if statements
22 * below, so that we don't test various assertions unless
23 * assertions are enabled. Compilers will often partially
24 * evaluate (CONSTANT && (expression)) at compile time, so
25 * that if CONSTANT is 0 the rest of the test isn't evaluated
26 * and assumed to result in a false result, with the code in
27 * the if branch being removed, and if CONSTANT is 1, the
28 * code is treated as an if that tests the expression.
30 * This could mean that, if "defined but not used" tests are
31 * being done, any variable tested in the expression may be warked
32 * as "defined but not used" if WS_ASSERT_ENABLED is 0, causing
33 * a pile of warnings if the variable isn't marked as unused
34 * (especially true of parametre variables).
36 * However, some compilers - Clang, in my tests, and probably GCC,
37 * due to tests in builds not failing - treate "if (0 && (expression))"
38 * specially, pretending hat all variables in the expression are used,
39 * even if they aren't used in the generated code. (At least in
40 * Apple clang version 15.0.0 (clang-1500.1.0.2.5), it must be
41 * exactly 0 - (0) doesn't have the same effect.)
43 * That's all very well, but, unfortunately Microsoft Visual Studio's
44 * C compiler doesn't do that, so the variables have to be marked as
45 * unused, which may cause warnings "used, but marked as unused"
46 * warnings if the code is compiled with assertions enabled.
48 #if defined(ENABLE_ASSERT)
49 #define WS_ASSERT_ENABLED 1
51 #define WS_ASSERT_ENABLED 0
53 #define WS_ASSERT_ENABLED 1
58 #endif /* __cplusplus */
61 * We don't want to execute the expression without assertions because
62 * it might be time and space costly and the goal here is to optimize for
63 * that case. However removing it completely is not good enough
64 * because it might generate many unused variable warnings. So we use
65 * if (false) and let the compiler optimize away the dead execution branch.
67 #define ws_assert_if_active(active, expr) \
69 if ((active) && !(expr)) \
70 ws_error("assertion failed: %s", #expr); \
74 * ws_abort_if_fail() is not conditional on having assertions enabled.
75 * Usually used to appease a static analyzer.
77 #define ws_abort_if_fail(expr) \
78 ws_assert_if_active(true, expr)
81 * ws_assert() cannot produce side effects, otherwise code will
82 * behave differently because of having assertions enabled/disabled, and
83 * probably introduce some difficult to track bugs.
85 #define ws_assert(expr) \
86 ws_assert_if_active(WS_ASSERT_ENABLED, expr)
89 #define ws_assert_streq(s1, s2) \
90 ws_assert((s1) && (s2) && strcmp((s1), (s2)) == 0)
92 #define ws_assert_utf8(str, len) \
94 const char *__assert_endptr; \
95 if (WS_ASSERT_ENABLED && \
96 !g_utf8_validate(str, len, &__assert_endptr)) { \
97 ws_log_utf8_full(LOG_DOMAIN_UTF_8, LOG_LEVEL_ERROR, \
98 __FILE__, __LINE__, __func__, \
99 str, len, __assert_endptr); \
104 * We don't want to disable ws_assert_not_reached() with (optional) assertions
106 * That would blast compiler warnings everywhere for no benefit, not
107 * even a miniscule performance gain. Reaching this function is always
108 * a programming error and will unconditionally abort execution.
110 * Note: With g_assert_not_reached() if the compiler supports unreachable
111 * built-ins (which recent versions of GCC and MSVC do) there is no warning
112 * blast with g_assert_not_reached() and G_DISABLE_ASSERT. However if that
113 * is not the case then g_assert_not_reached() is simply (void)0 and that
114 * causes the spurious warnings, because the compiler can't tell anymore
115 * that a certain code path is not used. We avoid that with
116 * ws_assert_not_reached(). There is no reason to ever use a no-op here.
118 #define ws_assert_not_reached() \
119 ws_error("assertion \"not reached\" failed")
122 * These macros can be used as an alternative to ws_assert() to
123 * assert some condition on function arguments. This must only be used
124 * to catch programming errors, in situations where an assertion is
125 * appropriate. And it should only be used if failing the condition
126 * doesn't necessarily lead to an inconsistent state for the program.
128 * It is possible to set the fatal log domain to "InvalidArg" to abort
129 * execution for debugging purposes, if one of these checks fail.
132 #define ws_warn_badarg(str) \
133 ws_log_full(LOG_DOMAIN_EINVAL, LOG_LEVEL_WARNING, \
134 __FILE__, __LINE__, __func__, \
135 "invalid argument: %s", str)
137 #define ws_return_str_if(expr, scope) \
139 if (WS_ASSERT_ENABLED && (expr)) { \
140 ws_warn_badarg(#expr); \
141 return wmem_strdup_printf(scope, "(invalid argument: %s)", #expr); \
145 #define ws_return_val_if(expr, val) \
147 if (WS_ASSERT_ENABLED && (expr)) { \
148 ws_warn_badarg(#expr); \
155 #endif /* __cplusplus */
157 #endif /* __WS_ASSERT_H__ */