summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2020-04-01 22:12:07 +0100
committerEdward Thomson <ethomson@edwardthomson.com>2020-05-11 20:13:54 +0100
commitcbae1c219d97dbc3cb0b88abe3bf6c21d019ebf8 (patch)
treec696665694c911b8e7ce113d4f237de103ca6c9d /src
parenta95096ba5f72db71422ffd9ea6630160387b97c0 (diff)
downloadlibgit2-ethomson/assert_macros.tar.gz
assert: allow non-int returning functions to assertethomson/assert_macros
Include GIT_ASSERT_WITH_RETVAL and GIT_ASSERT_ARG_WITH_RETVAL so that functions that do not return int (or more precisely, where `-1` would not be an error code) can assert. This allows functions that return, eg, NULL on an error code to do that by passing the return value (in this example, `NULL`) as a second parameter to the GIT_ASSERT_WITH_RETVAL functions.
Diffstat (limited to 'src')
-rw-r--r--src/assert_safe.h35
1 files changed, 21 insertions, 14 deletions
diff --git a/src/assert_safe.h b/src/assert_safe.h
index 9f2e16493..8c261100f 100644
--- a/src/assert_safe.h
+++ b/src/assert_safe.h
@@ -21,28 +21,35 @@
# define GIT_ASSERT(expr) assert(expr)
# define GIT_ASSERT_ARG(expr) assert(expr)
+
+# define GIT_ASSERT_WITH_RETVAL(expr, fail) assert(expr)
+# define GIT_ASSERT_ARG_WITH_RETVAL(expr, fail) assert(expr)
#else
+/** Internal consistency check to stop the function. */
+# define GIT_ASSERT(expr) GIT_ASSERT_WITH_RETVAL(expr, -1)
+
/**
* Assert that a consumer-provided argument is valid, setting an
* actionable error message and returning -1 if it is not.
*/
-# define GIT_ASSERT_ARG(expr) do { \
- if (!(expr)) { \
- git_error_set(GIT_ERROR_INVALID, \
- "invalid argument: '%s'", \
- #expr); \
- return -1; \
- } \
- } while(0)
+# define GIT_ASSERT_ARG(expr) GIT_ASSERT_ARG_WITH_RETVAL(expr, -1)
+
+/** Internal consistency check to return the `fail` param on failure. */
+# define GIT_ASSERT_WITH_RETVAL(expr, fail) \
+ GIT_ASSERT__WITH_RETVAL(expr, GIT_ERROR_INTERNAL, "unrecoverable internal error", fail)
+
+/**
+ * Assert that a consumer-provided argument is valid, setting an
+ * actionable error message and returning the `fail` param if not.
+ */
+# define GIT_ASSERT_ARG_WITH_RETVAL(expr, fail) \
+ GIT_ASSERT__WITH_RETVAL(expr, GIT_ERROR_INVALID, "invalid argument", fail)
-/* Internal consistency check to stop the function. */
-# define GIT_ASSERT(expr) do { \
+# define GIT_ASSERT__WITH_RETVAL(expr, code, msg, fail) do { \
if (!(expr)) { \
- git_error_set(GIT_ERROR_INTERNAL, \
- "unrecoverable internal error: '%s'", \
- #expr); \
- return -1; \
+ git_error_set(code, "%s: '%s'", msg, #expr); \
+ return fail; \
} \
} while(0)