Skip to content

Commit ccb259f

Browse files
authored
Use assert() for unreachable assertions (PCRE2Project#543)
1 parent 24f9d8d commit ccb259f

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/pcre2_util.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,20 @@ POSSIBILITY OF SUCH DAMAGE.
4545

4646
#ifdef PCRE2_DEBUG
4747

48+
#if defined(HAVE_ASSERT_H) && !defined(NDEBUG)
49+
#include <assert.h>
50+
#endif
51+
4852
/* PCRE2_ASSERT(x) can be used to inject an assert() for conditions
4953
that the code below doesn't support. It is a NOP for non debug builds
5054
but in debug builds will print information about the location of the
5155
code where it triggered and crash.
5256
53-
it is meant to work like assert(), and therefore the expressiom used
57+
It is meant to work like assert(), and therefore the expression used
5458
should indicate what the expected state is, and shouldn't have any
55-
sideeffects. */
59+
side-effects. */
5660

5761
#if defined(HAVE_ASSERT_H) && !defined(NDEBUG)
58-
#include <assert.h>
5962
#define PCRE2_ASSERT(x) assert(x)
6063
#else
6164
#define PCRE2_ASSERT(x) do \
@@ -73,21 +76,26 @@ shouldn't be reached. In non debug builds is defined as a hint for
7376
the compiler to eliminate any code after it, so it is useful also for
7477
performance reasons, but should be used with care because if it is
7578
ever reached will trigger Undefined Behaviour and if you are lucky a
76-
crash. In debug builds will report the location where it was trigered
79+
crash. In debug builds it will report the location where it was triggered
7780
and crash. One important point to consider when using this macro, is
7881
that it is only implemented for a few compilers, and therefore can't
7982
be relied on to always be active either, so if it is followed by some
80-
code it is imoprtant to make sure that the whole thing is safe to
83+
code it is important to make sure that the whole thing is safe to
8184
use even if the macro is not there (ex: make sure there is a `break`
8285
after it if used at the end of a `case`) and to test your code also
8386
with a configuration where the macro will be a NOP. */
8487

88+
#if defined(HAVE_ASSERT_H) && !defined(NDEBUG)
89+
#define PCRE2_UNREACHABLE() \
90+
assert(((void)"Execution reached unexpected point", 0))
91+
#else
8592
#define PCRE2_UNREACHABLE() do \
8693
{ \
8794
fprintf(stderr, "Execution reached unexpected point at " __FILE__ \
8895
":%d\n", __LINE__); \
8996
abort(); \
9097
} while(0)
98+
#endif
9199

92100
/* PCRE2_DEBUG_UNREACHABLE() is a debug only version of the previous
93101
macro. It is meant to be used in places where the code is handling

0 commit comments

Comments
 (0)