[tor-commits] [tor/master] Add raw_assert() variants for cases where we cannot log.

nickm at torproject.org nickm at torproject.org
Wed Jun 20 20:17:15 UTC 2018


commit ae01864b5d3bc8a5e3b069025d9d7972ff1a21c3
Author: Nick Mathewson <nickm at torproject.org>
Date:   Wed Jun 20 10:35:40 2018 -0400

    Add raw_assert() variants for cases where we cannot log.
    
    Remove a different raw_assert() macro declared in log.c
---
 src/common/log.c      |  9 +++------
 src/common/util_bug.h | 16 +++++++++++++++-
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/src/common/log.c b/src/common/log.c
index 7ba669818..2f5fdbc17 100644
--- a/src/common/log.c
+++ b/src/common/log.c
@@ -50,8 +50,6 @@
 #define TRUNCATED_STR_LEN 14
 /** @} */
 
-#define raw_assert(x) assert(x) // assert OK
-
 /** Defining compile-time constants for Tor log levels (used by the Rust
  * log wrapper at src/rust/tor_log) */
 const int LOG_WARN_ = LOG_WARN;
@@ -89,9 +87,9 @@ sev_to_string(int severity)
     case LOG_NOTICE:  return "notice";
     case LOG_WARN:    return "warn";
     case LOG_ERR:     return "err";
-    default:          /* Call assert, not tor_assert, since tor_assert
-                       * calls log on failure. */
-                      raw_assert(0); return "UNKNOWN"; // LCOV_EXCL_LINE
+    default:     /* Call assert, not tor_assert, since tor_assert
+                  * calls log on failure. */
+                 raw_assert_unreached(); return "UNKNOWN"; // LCOV_EXCL_LINE
   }
 }
 
@@ -1536,4 +1534,3 @@ truncate_logs(void)
     }
   }
 }
-
diff --git a/src/common/util_bug.h b/src/common/util_bug.h
index 70a28eb15..0e1af2da1 100644
--- a/src/common/util_bug.h
+++ b/src/common/util_bug.h
@@ -55,6 +55,21 @@
 #error "Sorry; we don't support building with NDEBUG."
 #endif /* defined(NDEBUG) */
 
+/* The raw_assert...() variants are for use within code that can't call
+ * tor_assertion_failed_() because of call circularity issues. */
+#define raw_assert(expr) STMT_BEGIN                             \
+  if (!(expr)) {                                                \
+    fprintf(stderr, "RAW ASSERTION FAILURE AT %s:%d: %s\n",     \
+          __FILE__, __LINE__, #expr);                           \
+    abort();                                                    \
+  }                                                             \
+  STMT_END
+#define raw_assert_unreached(expr) raw_assert(0)
+#define raw_assert_unreached_msg(msg) STMT_BEGIN \
+  fprintf(stderr, "ERROR: %s\n", (msg));         \
+  raw_assert_unreached();                        \
+  STMT_END
+
 /* Sometimes we don't want to use assertions during branch coverage tests; it
  * leads to tons of unreached branches which in reality are only assertions we
  * didn't hit. */
@@ -199,4 +214,3 @@ void tor_set_failed_assertion_callback(void (*fn)(void));
 #endif /* defined(TOR_UNIT_TESTS) */
 
 #endif /* !defined(TOR_UTIL_BUG_H) */
-





More information about the tor-commits mailing list