diff options
author | Nick Mathewson <nickm@torproject.org> | 2016-04-12 10:10:44 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2016-04-14 16:25:06 -0400 |
commit | 532820b11c9d0566b45b3dd19f01cf3e16d984ef (patch) | |
tree | 538a0286a23c25ed22fcd77b64750c471d3a7b05 /src/common | |
parent | a885271c08d2337b35c203c0b27509d0aa32dbf6 (diff) | |
download | tor-532820b11c9d0566b45b3dd19f01cf3e16d984ef.tar.gz tor-532820b11c9d0566b45b3dd19f01cf3e16d984ef.zip |
Add a BUG macro for usage in if checks.
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/util_bug.c | 3 | ||||
-rw-r--r-- | src/common/util_bug.h | 14 |
2 files changed, 16 insertions, 1 deletions
diff --git a/src/common/util_bug.c b/src/common/util_bug.c index 606c665163..e3e1d6df90 100644 --- a/src/common/util_bug.c +++ b/src/common/util_bug.c @@ -26,7 +26,7 @@ tor_assertion_failed_(const char *fname, unsigned int line, log_backtrace(LOG_ERR, LD_BUG, buf); } - +/** Helper for tor_assert_nonfatal: report the assertion failure. */ void tor_bug_occurred_(const char *fname, unsigned int line, const char *func, const char *expr, @@ -50,3 +50,4 @@ tor_bug_occurred_(const char *fname, unsigned int line, } log_backtrace(LOG_WARN, LD_BUG, buf); } + 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, |