summaryrefslogtreecommitdiff
path: root/src/common/util_bug.h
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2016-04-12 10:10:44 -0400
committerNick Mathewson <nickm@torproject.org>2016-04-14 16:25:06 -0400
commit532820b11c9d0566b45b3dd19f01cf3e16d984ef (patch)
tree538a0286a23c25ed22fcd77b64750c471d3a7b05 /src/common/util_bug.h
parenta885271c08d2337b35c203c0b27509d0aa32dbf6 (diff)
downloadtor-532820b11c9d0566b45b3dd19f01cf3e16d984ef.tar.gz
tor-532820b11c9d0566b45b3dd19f01cf3e16d984ef.zip
Add a BUG macro for usage in if checks.
Diffstat (limited to 'src/common/util_bug.h')
-rw-r--r--src/common/util_bug.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/common/util_bug.h b/src/common/util_bug.h
index ce54266b20..a5f78f2cd8 100644
--- a/src/common/util_bug.h
+++ b/src/common/util_bug.h
@@ -51,6 +51,11 @@
/* Non-fatal bug assertions. The "unreached" variants mean "this line should
* never be reached." The "once" variants mean "Don't log a warning more than
* once".
+ *
+ * The 'BUG' macro checks a boolean condition and logs an error message if it
+ * is true. Example usage:
+ * if (BUG(x == NULL))
+ * return -1;
*/
#ifdef ALL_BUGS_ARE_FATAL
@@ -58,11 +63,16 @@
#define tor_assert_nonfatal(cond) tor_assert((cond))
#define tor_assert_nonfatal_unreached_once() tor_assert(0)
#define tor_assert_nonfatal_once(cond) tor_assert((cond))
+#define BUG(cond) \
+ ((cond) ? \
+ (tor_assertion_failed_(SHORT_FILE__,__LINE__,__func__,#cond), abort(), 1) \
+ : 0)
#elif defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS)
#define tor_assert_nonfatal_unreached() STMT_NIL
#define tor_assert_nonfatal(cond) ((void)(cond))
#define tor_assert_nonfatal_unreached_once() STMT_NIL
#define tor_assert_nonfatal_once(cond) ((void)(cond))
+#define BUG(cond) ((cond) ? 1 : 0)
#else /* Normal case, !ALL_BUGS_ARE_FATAL, !DISABLE_ASSERTS_IN_UNIT_TESTS */
#define tor_assert_nonfatal_unreached() STMT_BEGIN \
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 0); \
@@ -86,6 +96,10 @@
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1); \
} \
STMT_END
+#define BUG(cond) \
+ ((cond) ? \
+ (tor_bug_occurred_(SHORT_FILE__,__LINE__,__func__,#cond,0), 1) \
+ : 0)
#endif
/** Define this if you want Tor to crash when any problem comes up,