Skip to content

Commit fa618a5

Browse files
authored
Merge pull request libgit2#5327 from libgit2/ethomson/assert
Introduce GIT_ASSERT macros
2 parents b8cdc9c + c6ebdb2 commit fa618a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+1744
-1120
lines changed

include/git2/blob.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ GIT_EXTERN(git_repository *) git_blob_owner(const git_blob *blob);
8484
* time.
8585
*
8686
* @param blob pointer to the blob
87-
* @return the pointer
87+
* @return the pointer, or NULL on error
8888
*/
8989
GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
9090

src/annotated_commit.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ static int annotated_commit_init(
2626
git_annotated_commit *annotated_commit;
2727
int error = 0;
2828

29-
assert(out && commit);
29+
GIT_ASSERT_ARG(out);
30+
GIT_ASSERT_ARG(commit);
3031

3132
*out = NULL;
3233

@@ -63,7 +64,9 @@ static int annotated_commit_init_from_id(
6364
git_commit *commit = NULL;
6465
int error = 0;
6566

66-
assert(out && repo && id);
67+
GIT_ASSERT_ARG(out);
68+
GIT_ASSERT_ARG(repo);
69+
GIT_ASSERT_ARG(id);
6770

6871
*out = NULL;
6972

@@ -100,7 +103,9 @@ int git_annotated_commit_from_revspec(
100103
git_object *obj, *commit;
101104
int error;
102105

103-
assert(out && repo && revspec);
106+
GIT_ASSERT_ARG(out);
107+
GIT_ASSERT_ARG(repo);
108+
GIT_ASSERT_ARG(revspec);
104109

105110
if ((error = git_revparse_single(&obj, repo, revspec)) < 0)
106111
return error;
@@ -126,7 +131,9 @@ int git_annotated_commit_from_ref(
126131
git_object *peeled;
127132
int error = 0;
128133

129-
assert(out && repo && ref);
134+
GIT_ASSERT_ARG(out);
135+
GIT_ASSERT_ARG(repo);
136+
GIT_ASSERT_ARG(ref);
130137

131138
*out = NULL;
132139

@@ -154,11 +161,12 @@ int git_annotated_commit_from_head(
154161
git_reference *head;
155162
int error;
156163

157-
assert(out && repo);
164+
GIT_ASSERT_ARG(out);
165+
GIT_ASSERT_ARG(repo);
158166

159167
*out = NULL;
160168

161-
if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
169+
if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
162170
return -1;
163171

164172
error = git_annotated_commit_from_ref(out, repo, head);
@@ -174,7 +182,11 @@ int git_annotated_commit_from_fetchhead(
174182
const char *remote_url,
175183
const git_oid *id)
176184
{
177-
assert(repo && id && branch_name && remote_url);
185+
GIT_ASSERT_ARG(out);
186+
GIT_ASSERT_ARG(repo);
187+
GIT_ASSERT_ARG(branch_name);
188+
GIT_ASSERT_ARG(remote_url);
189+
GIT_ASSERT_ARG(id);
178190

179191
if (annotated_commit_init_from_id(out, repo, id, branch_name) < 0)
180192
return -1;
@@ -192,14 +204,14 @@ int git_annotated_commit_from_fetchhead(
192204
const git_oid *git_annotated_commit_id(
193205
const git_annotated_commit *annotated_commit)
194206
{
195-
assert(annotated_commit);
207+
GIT_ASSERT_ARG_WITH_RETVAL(annotated_commit, NULL);
196208
return git_commit_id(annotated_commit->commit);
197209
}
198210

199211
const char *git_annotated_commit_ref(
200212
const git_annotated_commit *annotated_commit)
201213
{
202-
assert(annotated_commit);
214+
GIT_ASSERT_ARG_WITH_RETVAL(annotated_commit, NULL);
203215
return annotated_commit->ref_name;
204216
}
205217

src/apply.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* a Linking Exception. For full terms see the included COPYING file.
66
*/
77

8-
#include "apply.h"
9-
108
#include "git2/apply.h"
119
#include "git2/patch.h"
1210
#include "git2/filter.h"
@@ -21,6 +19,7 @@
2119
#include "zstream.h"
2220
#include "reader.h"
2321
#include "index.h"
22+
#include "apply.h"
2423

2524
typedef struct {
2625
/* The lines that we allocate ourself are allocated out of the pool.
@@ -399,7 +398,11 @@ int git_apply__patch(
399398
unsigned int mode = 0;
400399
int error = 0;
401400

402-
assert(contents_out && filename_out && mode_out && (source || !source_len) && patch);
401+
GIT_ASSERT_ARG(contents_out);
402+
GIT_ASSERT_ARG(filename_out);
403+
GIT_ASSERT_ARG(mode_out);
404+
GIT_ASSERT_ARG(source || !source_len);
405+
GIT_ASSERT_ARG(patch);
403406

404407
if (given_opts)
405408
memcpy(&ctx.opts, given_opts, sizeof(git_apply_options));
@@ -624,7 +627,10 @@ int git_apply_to_tree(
624627
size_t i;
625628
int error = 0;
626629

627-
assert(out && repo && preimage && diff);
630+
GIT_ASSERT_ARG(out);
631+
GIT_ASSERT_ARG(repo);
632+
GIT_ASSERT_ARG(preimage);
633+
GIT_ASSERT_ARG(diff);
628634

629635
*out = NULL;
630636

@@ -772,6 +778,8 @@ static int git_apply__to_index(
772778

773779
int git_apply_options_init(git_apply_options *opts, unsigned int version)
774780
{
781+
GIT_ASSERT_ARG(opts);
782+
775783
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
776784
opts, version, git_apply_options, GIT_APPLY_OPTIONS_INIT);
777785
return 0;
@@ -805,7 +813,8 @@ int git_apply(
805813
git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
806814
int error = GIT_EINVALID;
807815

808-
assert(repo && diff);
816+
GIT_ASSERT_ARG(repo);
817+
GIT_ASSERT_ARG(diff);
809818

810819
GIT_ERROR_CHECK_VERSION(
811820
given_opts, GIT_APPLY_OPTIONS_VERSION, "git_apply_options");
@@ -829,7 +838,7 @@ int git_apply(
829838
error = git_reader_for_workdir(&pre_reader, repo, false);
830839
break;
831840
default:
832-
assert(false);
841+
GIT_ASSERT(false);
833842
}
834843

835844
if (error < 0)
@@ -869,7 +878,7 @@ int git_apply(
869878
error = git_apply__to_workdir(repo, diff, preimage, postimage, location, &opts);
870879
break;
871880
default:
872-
assert(false);
881+
GIT_ASSERT(false);
873882
}
874883

875884
if (error < 0)

src/attr.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ int git_attr_get(
5858
git_attr_rule *rule;
5959
git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN;
6060

61-
assert(value && repo && name);
61+
GIT_ASSERT_ARG(value);
62+
GIT_ASSERT_ARG(repo);
63+
GIT_ASSERT_ARG(name);
6264

6365
*value = NULL;
6466

@@ -123,7 +125,10 @@ int git_attr_get_many_with_session(
123125
if (!num_attr)
124126
return 0;
125127

126-
assert(values && repo && names);
128+
GIT_ASSERT_ARG(values);
129+
GIT_ASSERT_ARG(repo);
130+
GIT_ASSERT_ARG(pathname);
131+
GIT_ASSERT_ARG(names);
127132

128133
if (git_repository_is_bare(repo))
129134
dir_flag = GIT_DIR_FLAG_FALSE;
@@ -206,7 +211,8 @@ int git_attr_foreach(
206211
git_strmap *seen = NULL;
207212
git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN;
208213

209-
assert(repo && callback);
214+
GIT_ASSERT_ARG(repo);
215+
GIT_ASSERT_ARG(callback);
210216

211217
if (git_repository_is_bare(repo))
212218
dir_flag = GIT_DIR_FLAG_FALSE;
@@ -378,6 +384,9 @@ int git_attr_add_macro(
378384
git_attr_rule *macro = NULL;
379385
git_pool *pool;
380386

387+
GIT_ASSERT_ARG(repo);
388+
GIT_ASSERT_ARG(name);
389+
381390
if ((error = git_attr_cache__init(repo)) < 0)
382391
return error;
383392

src/attr_file.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ uint32_t git_attr_file__name_hash(const char *name)
351351
{
352352
uint32_t h = 5381;
353353
int c;
354-
assert(name);
354+
355+
GIT_ASSERT_ARG(name);
356+
355357
while ((c = (int)*name++) != 0)
356358
h = ((h << 5) + h) + c;
357359
return h;
@@ -660,7 +662,8 @@ int git_attr_fnmatch__parse(
660662
int slash_count, allow_space;
661663
bool escaped;
662664

663-
assert(spec && base && *base);
665+
GIT_ASSERT_ARG(spec);
666+
GIT_ASSERT_ARG(base && *base);
664667

665668
if (parse_optimized_patterns(spec, pool, *base))
666669
return 0;
@@ -828,7 +831,7 @@ int git_attr_assignment__parse(
828831
const char *scan = *base;
829832
git_attr_assignment *assign = NULL;
830833

831-
assert(assigns && !assigns->length);
834+
GIT_ASSERT_ARG(assigns && !assigns->length);
832835

833836
git_vector_set_cmp(assigns, sort_by_hash_and_name);
834837

@@ -954,7 +957,7 @@ void git_attr_rule__free(git_attr_rule *rule)
954957

955958
int git_attr_session__init(git_attr_session *session, git_repository *repo)
956959
{
957-
assert(repo);
960+
GIT_ASSERT_ARG(repo);
958961

959962
memset(session, 0, sizeof(*session));
960963
session->key = git_atomic_inc(&repo->attr_session_key);

src/blame.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,21 @@ void git_blame_free(git_blame *blame)
171171

172172
uint32_t git_blame_get_hunk_count(git_blame *blame)
173173
{
174-
assert(blame);
174+
GIT_ASSERT_ARG(blame);
175175
return (uint32_t)blame->hunks.length;
176176
}
177177

178178
const git_blame_hunk *git_blame_get_hunk_byindex(git_blame *blame, uint32_t index)
179179
{
180-
assert(blame);
180+
GIT_ASSERT_ARG_WITH_RETVAL(blame, NULL);
181181
return (git_blame_hunk*)git_vector_get(&blame->hunks, index);
182182
}
183183

184184
const git_blame_hunk *git_blame_get_hunk_byline(git_blame *blame, size_t lineno)
185185
{
186186
size_t i, new_lineno = lineno;
187-
assert(blame);
187+
188+
GIT_ASSERT_ARG_WITH_RETVAL(blame, NULL);
188189

189190
if (!git_vector_bsearch2(&i, &blame->hunks, hunk_byfinalline_search_cmp, &new_lineno)) {
190191
return git_blame_get_hunk_byindex(blame, (uint32_t)i);
@@ -388,7 +389,10 @@ int git_blame_file(
388389
git_blame_options normOptions = GIT_BLAME_OPTIONS_INIT;
389390
git_blame *blame = NULL;
390391

391-
assert(out && repo && path);
392+
GIT_ASSERT_ARG(out);
393+
GIT_ASSERT_ARG(repo);
394+
GIT_ASSERT_ARG(path);
395+
392396
if ((error = normalize_options(&normOptions, options, repo)) < 0)
393397
goto on_error;
394398

@@ -509,7 +513,9 @@ int git_blame_buffer(
509513

510514
diffopts.context_lines = 0;
511515

512-
assert(out && reference && buffer && buffer_len);
516+
GIT_ASSERT_ARG(out);
517+
GIT_ASSERT_ARG(reference);
518+
GIT_ASSERT_ARG(buffer && buffer_len);
513519

514520
blame = git_blame__alloc(reference->repository, reference->options, reference->path);
515521
GIT_ERROR_CHECK_ALLOC(blame);

0 commit comments

Comments
 (0)