Refactor the assertion-failure code into a function

This commit is contained in:
Nick Mathewson 2013-07-19 13:40:20 -04:00
parent 5343ee1a06
commit f6d8bc9389
2 changed files with 21 additions and 8 deletions

View File

@ -93,6 +93,20 @@
#include <sys/wait.h> #include <sys/wait.h>
#endif #endif
/* =====
* Assertion helper.
* ===== */
/** Helper for tor_assert: report the assertion failure. */
void
tor_assertion_failed_(const char *fname, unsigned int line,
const char *func, const char *expr)
{
log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.",
fname, line, func, expr);
fprintf(stderr,"%s:%u: %s: Assertion %s failed; aborting.\n",
fname, line, func, expr);
}
/* ===== /* =====
* Memory management * Memory management
* ===== */ * ===== */
@ -5057,4 +5071,3 @@ tor_weak_random_range(tor_weak_rng_t *rng, int32_t top)
} while (result >= top); } while (result >= top);
return result; return result;
} }

View File

@ -48,13 +48,13 @@
/** Like assert(3), but send assertion failures to the log as well as to /** Like assert(3), but send assertion failures to the log as well as to
* stderr. */ * stderr. */
#define tor_assert(expr) STMT_BEGIN \ #define tor_assert(expr) STMT_BEGIN \
if (PREDICT_UNLIKELY(!(expr))) { \ if (PREDICT_UNLIKELY(!(expr))) { \
log_err(LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \ tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, #expr); \
SHORT_FILE__, __LINE__, __func__, #expr); \ abort(); \
fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \ } STMT_END
SHORT_FILE__, __LINE__, __func__, #expr); \
abort(); \ void tor_assertion_failed_(const char *fname, unsigned int line,
} STMT_END const char *func, const char *expr);
/* If we're building with dmalloc, we want all of our memory allocation /* If we're building with dmalloc, we want all of our memory allocation
* functions to take an extra file/line pair of arguments. If not, not. * functions to take an extra file/line pair of arguments. If not, not.