git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/3] git-compat-util: introduce BUG_IF_NOT() macro

From
Elijah Newren via GitGitGadget <gitgitgadget@gmail.com>
Date
Mar 16, 2025, 06:42 UTC
Message-ID
<109060ccb8665c73aa0c4f73e3cbbddcd135bde4.1742107322.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1881.v2.git.1742107322.gitgitgadget@gmail.com>
From: Elijah Newren <newren@gmail.com>

Create a BUG_IF_NOT() macro which is similar to assert(), but will not be compiled out when NDEBUG is defined, and is thus safe to use even if its argument has side-effects.

We will use this new macro in a subsequent commit to convert a few existing assert() invocations to BUG_IF_NOT(). In particular, we'll convert the handful of invocations which cannot be proven to be free of side effects with a simple compiler/linker hack.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 git-compat-util.h | 1 +
 1 file changed, 1 insertion(+)
diff --git a/git-compat-util.h b/git-compat-util.h
index e123288e8f1..c3415ad7e0a 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -1460,6 +1460,7 @@ extern int bug_called_must_BUG;
 __attribute__((format (printf, 3, 4))) NORETURN
 void BUG_fl(const char *file, int line, const char *fmt, ...);
 #define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__)
+#define BUG_IF_NOT(a) if (!(a)) BUG("Assertion `" #a "' failed.")
 __attribute__((format (printf, 3, 4)))
 void bug_fl(const char *file, int line, const char *fmt, ...);
 #define bug(...) bug_fl(__FILE__, __LINE__, __VA_ARGS__)
-- 
gitgitgadget
Previous: Elijah Newren via GitGitGadgetNext: Junio C Hamano
Message 14 of 30 in “Add a static analysis job to prevent assertions with side effects”
  1. 0/3 Add a static analysis job to prevent assertions with side effectsElijah Newren via GitGitGadget, Mar 14, 2025
  2. 1/3 git-compat-util: introduce BUG_IF_NOT() macroElijah Newren via GitGitGadget, Mar 14, 2025
  3. 2/3 ci: add build checking for side-effects in assert() callsElijah Newren via GitGitGadget, Mar 14, 2025
  4. Junio C HamanoMar 14, 2025
  5. brian m. carlsonMar 14, 2025
  6. Junio C HamanoMar 14, 2025
  7. Elijah NewrenMar 14, 2025
  8. Junio C HamanoMar 14, 2025
  9. Elijah NewrenMar 16, 2025
  10. Elijah NewrenMar 17, 2025
  11. Junio C HamanoMar 17, 2025
  12. 3/3 treewide: replace assert() with BUG_IF_NOT() in special casesElijah Newren via GitGitGadget, Mar 14, 2025
  13. 0/3 Add a static analysis job to prevent assertions with side effectsElijah Newren via GitGitGadget, Mar 16, 2025
  14. 1/3 git-compat-util: introduce BUG_IF_NOT() macroElijah Newren via GitGitGadget, Mar 16, 2025
  15. Junio C HamanoMar 17, 2025
  16. Taylor BlauMar 17, 2025
  17. 2/3 ci: add build checking for side-effects in assert() callsElijah Newren via GitGitGadget, Mar 16, 2025
  18. Taylor BlauMar 17, 2025
  19. Elijah NewrenMar 19, 2025
  20. Taylor BlauMar 19, 2025
  21. Junio C HamanoMar 17, 2025
  22. 3/3 treewide: replace assert() with BUG_IF_NOT() in special casesElijah Newren via GitGitGadget, Mar 16, 2025
  23. Taylor BlauMar 17, 2025
  24. Taylor BlauMar 17, 2025
  25. 0/3 Add a static analysis job to prevent assertions with side effectsElijah Newren via GitGitGadget, Mar 19, 2025
  26. 1/3 git-compat-util: introduce ASSERT() macroElijah Newren via GitGitGadget, Mar 19, 2025
  27. 2/3 ci: add build checking for side-effects in assert() callsElijah Newren via GitGitGadget, Mar 19, 2025
  28. 3/3 treewide: replace assert() with ASSERT() in special casesElijah Newren via GitGitGadget, Mar 19, 2025
  29. Taylor BlauMar 19, 2025
  30. Taylor BlauMar 19, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.