From 6ceaa78088ebdaea6cb887a7e8066fc29e0f984d Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 29 Dec 2025 12:04:22 +0100 Subject: macros: Introduce must_succeed() macro This is like assert() but never compiled out so can be used with function calls that have side effects. --- apt-pkg/contrib/macros.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/apt-pkg/contrib/macros.h b/apt-pkg/contrib/macros.h index 08e7aeb72..1a76ef60a 100644 --- a/apt-pkg/contrib/macros.h +++ b/apt-pkg/contrib/macros.h @@ -33,6 +33,14 @@ #define likely(x) (x) #define unlikely(x) (x) #endif + +// Asserts that the result of expr is true +#define must_succeed(expr) ({ \ + auto result = (expr); \ + if (unlikely(not result)) \ + fprintf(stderr, "%s:%d: %s: Assertion `%s` failed.\n", __FILE__, __LINE__, __FUNCTION__, #expr), abort(); \ + result; \ +}) #endif #if APT_GCC_VERSION >= 0x0300 -- cgit v1.2.3-70-g09d2