From c019e11cc02e2d6328364e50b2ff70d035367513 Mon Sep 17 00:00:00 2001 From: Otavio Pontes Date: Thu, 9 Apr 2020 13:58:15 -0700 Subject: [PATCH] Create the UNEXPECTED() macro There are several points in swupd that we find conditions that should never happen, but we try to correct them anyway to be more reliable. Because of that we may hide programming mistakes that are corrected later. So, to make sure we are going to catch those problems in debug mode in our development machines and test environment, I am suggesting a UNEXPECTED() macro that aborts swupd if --enable-debug was used to build swupd. I am adding some examples on what we could do with that. Signed-off-by: Otavio Pontes --- configure.ac | 10 ++++++++++ scripts/github_actions/build_ci.bash | 2 +- src/lib/macros.h | 10 ++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index c0bf072d..4baea960 100644 --- a/configure.ac +++ b/configure.ac @@ -28,6 +28,16 @@ AC_CHECK_LIB([pthread], [pthread_create]) # Program checks AC_CHECK_PROGS(TAR, tar) +# Enable/Disable debug mode +AC_ARG_ENABLE( + [debug], + [AS_HELP_STRING([--enable-debug], [Enable debug mode (disabled by default)])] +) +AS_IF( + [test "x$enable_debug" == "xyes"], + [AC_DEFINE(DEBUG_MODE, 1, [Debug enabled])] +) + # Enable/disable options AC_ARG_ENABLE( diff --git a/scripts/github_actions/build_ci.bash b/scripts/github_actions/build_ci.bash index 5a688ecd..38f5162b 100755 --- a/scripts/github_actions/build_ci.bash +++ b/scripts/github_actions/build_ci.bash @@ -17,7 +17,7 @@ sudo ln -s /usr/share/docutils/scripts/python3/rst2man /usr/bin/rst2man.py # Build Swupd autoreconf --verbose --warnings=none --install --force -./configure CFLAGS="$CFLAGS -fsanitize=address -Werror" --prefix=/usr --with-fallback-capaths="$PWD"/swupd_test_certificates --with-systemdsystemunitdir=/usr/lib/systemd/system --with-config-file-path="$PWD"/testconfig +./configure CFLAGS="$CFLAGS -fsanitize=address -Werror" --prefix=/usr --with-fallback-capaths="$PWD"/swupd_test_certificates --with-systemdsystemunitdir=/usr/lib/systemd/system --with-config-file-path="$PWD"/testconfig --enable-debug make -j$CORES # Needed to initialize the host for auto-update. Without these steps auto-update diff --git a/src/lib/macros.h b/src/lib/macros.h index 6efbe719..71cd2f15 100644 --- a/src/lib/macros.h +++ b/src/lib/macros.h @@ -18,4 +18,14 @@ /** @brief Return the max between 2 values. */ #define MAX(_a, _b) ((_a) > (_b) ? (_a) : (_b)) +#ifdef DEBUG_MODE +#define UNEXPECTED() abort() +#else +/** + * @brief To be used in all checks where we assume swupd should never get in to. + * This macro aborts execution only when DEBUG_MODE is used. + */ +#define UNEXPECTED() +#endif + #endif