summaryrefslogtreecommitdiff
path: root/src/fundamental
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2022-02-01 14:43:04 +0100
committerLennart Poettering <lennart@poettering.net>2022-02-01 15:31:05 +0100
commit8890ec82f5f8a04c44dc527afd5ad868505e74d0 (patch)
tree91cb42c999a9738735e4a703924e60b78f731749 /src/fundamental
parent3b23a6c40ac42c5673c6817ddf174983497519da (diff)
downloadsystemd-8890ec82f5f8a04c44dc527afd5ad868505e74d0.tar.gz
macro: add ASSERT_SE_PTR() macro
ASSERT_SE_PTR() is like ASSERT_PTR() but uses assert_se() instead of assert() internally. Code should use ASSERT_SE_PTR() where the check should never be optimized away, even if NDEBUG is set. Rationale: assert() is the right choice for validating assumptions about our own code, i.e. checking conditions that are "impossible" to not hold, because we ourselves hacked things up the "right" way of course. assert_se() is the right choice for tests that come with a weaker guarantee, they encode assumptions over other's API behaviour, i.e. whether something can fail there or not. When developing tools that are not oom-safe assert_se() is the right choice: we know that on Linux OOM doesn't really happen, even though theoretically the API allows it to happen. Usecase for ASSERT_SE_PTR() is mostly the fatal memory allocation logic for EFI memory allocations. So far it used regular assert() i.e. OOM failurs would be totally ignored if NDEBUG is set. We'd rather have our EFI program to print an assert message and freeze instead though.
Diffstat (limited to 'src/fundamental')
-rw-r--r--src/fundamental/macro-fundamental.h7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h
index a1cbc3a5b3..15b93165dc 100644
--- a/src/fundamental/macro-fundamental.h
+++ b/src/fundamental/macro-fundamental.h
@@ -76,6 +76,13 @@
_expr_; \
})
+#define ASSERT_SE_PTR(expr) \
+ ({ \
+ typeof(expr) _expr_ = (expr); \
+ assert_se(_expr_); \
+ _expr_; \
+ })
+
#if defined(static_assert)
#define assert_cc(expr) \
static_assert(expr, #expr)