Skip to content

Commit a95096b

Browse files
committed
assert: optionally fall-back to assert(3)
Fall back to the system assert(3) in debug builds, which may aide in debugging. "Safe" assertions can be enabled in debug builds by setting GIT_ASSERT_HARD=0. Similarly, hard assertions can be enabled in release builds by setting GIT_ASSERT_HARD to nonzero.
1 parent abe2efe commit a95096b

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

src/assert_safe.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
#ifndef INCLUDE_assert_safe_h__
8+
#define INCLUDE_assert_safe_h__
9+
10+
/*
11+
* In a debug build, we'll assert(3) for aide in debugging. In release
12+
* builds, we will provide macros that will set an error message that
13+
* indicate a failure and return. Note that memory leaks can occur in
14+
* a release-mode assertion failure -- it is impractical to provide
15+
* safe clean up routines in these very extreme failures, but care
16+
* should be taken to not leak very large objects.
17+
*/
18+
19+
#if (defined(_DEBUG) || defined(GIT_ASSERT_HARD)) && GIT_ASSERT_HARD != 0
20+
# include <assert.h>
21+
22+
# define GIT_ASSERT(expr) assert(expr)
23+
# define GIT_ASSERT_ARG(expr) assert(expr)
24+
#else
25+
26+
/**
27+
* Assert that a consumer-provided argument is valid, setting an
28+
* actionable error message and returning -1 if it is not.
29+
*/
30+
# define GIT_ASSERT_ARG(expr) do { \
31+
if (!(expr)) { \
32+
git_error_set(GIT_ERROR_INVALID, \
33+
"invalid argument: '%s'", \
34+
#expr); \
35+
return -1; \
36+
} \
37+
} while(0)
38+
39+
/* Internal consistency check to stop the function. */
40+
# define GIT_ASSERT(expr) do { \
41+
if (!(expr)) { \
42+
git_error_set(GIT_ERROR_INTERNAL, \
43+
"unrecoverable internal error: '%s'", \
44+
#expr); \
45+
return -1; \
46+
} \
47+
} while(0)
48+
49+
#endif /* GIT_ASSERT_HARD */
50+
51+
#endif

src/common.h

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#include "errors.h"
8181
#include "thread-utils.h"
8282
#include "integer.h"
83+
#include "assert_safe.h"
8384

8485
/*
8586
* Include the declarations for deprecated functions; this ensures
@@ -94,33 +95,6 @@
9495
#define FILTERIO_BUFSIZE DEFAULT_BUFSIZE
9596
#define NETIO_BUFSIZE DEFAULT_BUFSIZE
9697

97-
/**
98-
* Assert that a consumer-provided argument is valid, setting an
99-
* actionable error message and returning -1 if it is not.
100-
*
101-
* Note that memory leaks can occur in a release-mode assertion
102-
* failure -- it is impractical to provide safe clean up routines in these very
103-
* extreme failures, but care should be taken to not leak very large objects.
104-
*/
105-
#define GIT_ASSERT_ARG(expr) do { \
106-
if (!(expr)) { \
107-
git_error_set(GIT_ERROR_INVALID, \
108-
"invalid argument: '%s'", \
109-
#expr); \
110-
return -1; \
111-
} \
112-
} while(0)
113-
114-
/** Internal consistency check to stop the function. */
115-
#define GIT_ASSERT(expr) do { \
116-
if (!(expr)) { \
117-
git_error_set(GIT_ERROR_INTERNAL, \
118-
"unrecoverable internal error: '%s'", \
119-
#expr); \
120-
return -1; \
121-
} \
122-
} while(0)
123-
12498
/**
12599
* Check a pointer allocation result, returning -1 if it failed.
126100
*/

tests/core/assert.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#define GIT_ASSERT_HARD 0
2+
13
#include "clar_libgit2.h"
24

35
static const char *hello_world = "hello, world";

0 commit comments

Comments
 (0)