Deploy GITERR_CHECK_VERSION
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
diff --git a/src/checkout.c b/src/checkout.c
index 06407bb..640b048 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -603,19 +603,6 @@ static int checkout_create_submodules(
return 0;
}
-static bool opts_is_valid_version(git_checkout_opts *opts)
-{
- if (!opts)
- return true;
-
- if (opts->version > 0 && opts->version <= GIT_CHECKOUT_OPTS_VERSION)
- return true;
-
- giterr_set(GITERR_INVALID, "Invalid version %d on git_checkout_opts structure",
- opts->version);
- return false;
-}
-
int git_checkout_index(
git_repository *repo,
git_index *index,
@@ -632,6 +619,8 @@ int git_checkout_index(
assert(repo);
+ GITERR_CHECK_VERSION(opts, GIT_CHECKOUT_OPTS_VERSION, "git_checkout_opts");
+
if ((error = git_repository__ensure_not_bare(repo, "checkout")) < 0)
return error;
@@ -639,11 +628,6 @@ int git_checkout_index(
GIT_DIFF_INCLUDE_UNMODIFIED | GIT_DIFF_INCLUDE_UNTRACKED |
GIT_DIFF_INCLUDE_TYPECHANGE | GIT_DIFF_SKIP_BINARY_CHECK;
- if (!opts_is_valid_version(opts)) {
- error = -1;
- goto cleanup;
- }
-
if (opts && opts->paths.count > 0)
diff_opts.pathspec = opts->paths;
diff --git a/src/commit.c b/src/commit.c
index 5197fdb..f9606dd 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -93,9 +93,8 @@ int git_commit_create(
git_odb *odb;
assert(git_object_owner((const git_object *)tree) == repo);
- if (!git_signature__has_valid_version(author) ||
- !git_signature__has_valid_version(committer))
- return -1;
+ GITERR_CHECK_VERSION(author, GIT_SIGNATURE_VERSION, "git_signature");
+ GITERR_CHECK_VERSION(committer, GIT_SIGNATURE_VERSION, "git_signature");
git_oid__writebuf(&commit, "tree ", git_object_id((const git_object *)tree));
diff --git a/src/common.h b/src/common.h
index a35239e..b779d28 100644
--- a/src/common.h
+++ b/src/common.h
@@ -65,6 +65,23 @@ void giterr_set(int error_class, const char *string, ...);
*/
void giterr_set_regex(const regex_t *regex, int error_code);
+/**
+ * Check a versioned structure for validity
+ */
+GIT_INLINE(bool) giterr__check_version(const void *structure, unsigned int expected_max, const char *name)
+{
+ if (!structure)
+ return true;
+
+ unsigned int actual = *(unsigned int*)structure;
+ if (actual > 0 && actual <= expected_max)
+ return true;
+
+ giterr_set(GITERR_INVALID, "Invalid version %d on %s", actual, name);
+ return false;
+}
+#define GITERR_CHECK_VERSION(S,V,N) if (!giterr__check_version(S,V,N)) return -1
+
/* NOTE: other giterr functions are in the public errors.h header file */
#include "util.h"
diff --git a/src/config.c b/src/config.c
index 913108a..d422447 100644
--- a/src/config.c
+++ b/src/config.c
@@ -248,18 +248,6 @@ int git_config_open_level(
return 0;
}
-static bool config_backend_has_valid_version(git_config_backend *backend)
-{
- if (!backend)
- return true;
-
- if (backend->version > 0 && backend->version <= GIT_CONFIG_BACKEND_VERSION)
- return true;
-
- giterr_set(GITERR_INVALID, "Invalid version %d for git_config_backend", backend->version);
- return false;
-}
-
int git_config_add_backend(
git_config *cfg,
git_config_backend *file,
@@ -271,8 +259,7 @@ int git_config_add_backend(
assert(cfg && file);
- if (!config_backend_has_valid_version(file))
- return -1;
+ GITERR_CHECK_VERSION(file, GIT_CONFIG_BACKEND_VERSION, "git_config_backend");
if ((result = file->open(file, level)) < 0)
return result;
diff --git a/src/diff.c b/src/diff.c
index 722acbe..46d96bb 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -758,9 +758,8 @@ fail:
#define DIFF_FROM_ITERATORS(MAKE_FIRST, MAKE_SECOND) do { \
git_iterator *a = NULL, *b = NULL; \
char *pfx = opts ? git_pathspec_prefix(&opts->pathspec) : NULL; \
- if (!git_diff__opts_has_valid_version(opts)) \
- error = -1; \
- else if (!(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \
+ GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \
+ if (!(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \
error = diff_from_iterators(diff, repo, a, b, opts); \
git__free(pfx); git_iterator_free(a); git_iterator_free(b); \
} while (0)
diff --git a/src/diff.h b/src/diff.h
index 1f45af1..f93bab1 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -62,18 +62,5 @@ extern int git_diff__oid_for_file(
git_repository *, const char *, uint16_t, git_off_t, git_oid *);
-GIT_INLINE(bool) git_diff__opts_has_valid_version(const git_diff_options *opts)
-{
- if (!opts)
- return true;
-
- if (opts->version > 0 && opts->version <= GIT_DIFF_OPTIONS_VERSION)
- return true;
-
- giterr_set(GITERR_INVALID, "Invalid version %d in git_diff_options", opts->version);
- return false;
-}
-
-
#endif
diff --git a/src/diff_output.c b/src/diff_output.c
index 6316bfa..b18255d 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -1266,8 +1266,7 @@ int git_diff_blobs(
git_diff_delta delta;
git_diff_patch patch;
- if (!git_diff__opts_has_valid_version(options))
- return -1;
+ GITERR_CHECK_VERSION(options, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
if (options && (options->flags & GIT_DIFF_REVERSE)) {
git_blob *swap = old_blob;
diff --git a/src/diff_tform.c b/src/diff_tform.c
index 1df0410..0c58859 100644
--- a/src/diff_tform.c
+++ b/src/diff_tform.c
@@ -168,18 +168,6 @@ int git_diff_merge(
return error;
}
-static bool find_opts_has_valid_version(const git_diff_find_options *opts)
-{
- if (!opts)
- return true;
-
- if (opts->version > 0 && opts->version <= GIT_DIFF_FIND_OPTIONS_VERSION)
- return true;
-
- giterr_set(GITERR_INVALID, "Invalid version %d on git_diff_find_options", opts->version);
- return false;
-}
-
#define DEFAULT_THRESHOLD 50
#define DEFAULT_BREAK_REWRITE_THRESHOLD 60
#define DEFAULT_TARGET_LIMIT 200
@@ -211,8 +199,7 @@ static int normalize_find_opts(
opts->flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
}
- if (!find_opts_has_valid_version(opts))
- return -1;
+ GITERR_CHECK_VERSION(opts, GIT_DIFF_FIND_OPTIONS_VERSION, "git_diff_find_options");
/* some flags imply others */
diff --git a/src/notes.c b/src/notes.c
index 87ee1e7..71a9e33 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -456,9 +456,8 @@ int git_note_create(
git_commit *commit = NULL;
git_tree *tree = NULL;
- if (!git_signature__has_valid_version(author) ||
- !git_signature__has_valid_version(committer))
- return -1;
+ GITERR_CHECK_VERSION(author, GIT_SIGNATURE_VERSION, "git_signature");
+ GITERR_CHECK_VERSION(committer, GIT_SIGNATURE_VERSION, "git_signature");
target = git_oid_allocfmt(oid);
GITERR_CHECK_ALLOC(target);
@@ -487,9 +486,8 @@ int git_note_remove(git_repository *repo, const char *notes_ref,
git_commit *commit = NULL;
git_tree *tree = NULL;
- if (!git_signature__has_valid_version(author) ||
- !git_signature__has_valid_version(committer))
- return -1;
+ GITERR_CHECK_VERSION(author, GIT_SIGNATURE_VERSION, "git_signature");
+ GITERR_CHECK_VERSION(committer, GIT_SIGNATURE_VERSION, "git_signature");
target = git_oid_allocfmt(oid);
GITERR_CHECK_ALLOC(target);
diff --git a/src/odb.c b/src/odb.c
index e35e4ca..b6d1f79 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -363,26 +363,13 @@ int git_odb_new(git_odb **out)
return 0;
}
-static bool backend_has_valid_version(git_odb_backend *backend)
-{
- if (!backend)
- return true;
-
- if (backend->version > 0 && backend->version <= GIT_ODB_BACKEND_VERSION)
- return true;
-
- giterr_set(GITERR_INVALID, "Invalid version %d on git_odb_backend", backend->version);
- return false;
-}
-
static int add_backend_internal(git_odb *odb, git_odb_backend *backend, int priority, int is_alternate)
{
backend_internal *internal;
assert(odb && backend);
- if (!backend_has_valid_version(backend))
- return -1;
+ GITERR_CHECK_VERSION(backend, GIT_ODB_BACKEND_VERSION, "git_odb_backend");
/* Check if the backend is already owned by another ODB */
assert(!backend->odb || backend->odb == odb);
diff --git a/src/reflog.c b/src/reflog.c
index 4d119ba..c0af60f 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -298,8 +298,7 @@ int git_reflog_append(git_reflog *reflog, const git_oid *new_oid,
assert(reflog && new_oid && committer);
- if (!git_signature__has_valid_version(committer))
- return -1;
+ GITERR_CHECK_VERSION(committer, GIT_SIGNATURE_VERSION, "git_signature");
if (reflog_entry_new(&entry) < 0)
return -1;
diff --git a/src/remote.c b/src/remote.c
index 5fc6b4f..4f5aa9d 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -985,24 +985,11 @@ void git_remote_check_cert(git_remote *remote, int check)
remote->check_cert = check;
}
-static bool callbacks_have_valid_version(git_remote_callbacks *callbacks)
-{
- if (!callbacks)
- return true;
-
- if (callbacks->version > 0 && callbacks->version <= GIT_REMOTE_CALLBACKS_VERSION)
- return true;
-
- giterr_set(GITERR_INVALID, "Invalid version %d for git_remote_callbacks", callbacks->version);
- return false;
-}
-
int git_remote_set_callbacks(git_remote *remote, git_remote_callbacks *callbacks)
{
assert(remote && callbacks);
- if (!callbacks_have_valid_version(callbacks))
- return -1;
+ GITERR_CHECK_VERSION(callbacks, GIT_REMOTE_CALLBACKS_VERSION, "git_remote_callbacks");
memcpy(&remote->callbacks, callbacks, sizeof(git_remote_callbacks));
@@ -1024,24 +1011,11 @@ void git_remote_set_cred_acquire_cb(
remote->cred_acquire_cb = cred_acquire_cb;
}
-static bool transport_has_valid_version(const git_transport *transport)
-{
- if (!transport)
- return true;
-
- if (transport->version > 0 && transport->version <= GIT_TRANSPORT_VERSION)
- return true;
-
- giterr_set(GITERR_INVALID, "Invalid version %d on git_transport", transport->version);
- return false;
-}
-
int git_remote_set_transport(git_remote *remote, git_transport *transport)
{
assert(remote && transport);
- if (!transport_has_valid_version(transport))
- return -1;
+ GITERR_CHECK_VERSION(transport, GIT_TRANSPORT_VERSION, "git_transport");
if (remote->transport) {
giterr_set(GITERR_NET, "A transport is already bound to this remote");
diff --git a/src/repository.c b/src/repository.c
index 073efa4..10ed12b 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -1160,18 +1160,6 @@ int git_repository_init(
return git_repository_init_ext(repo_out, path, &opts);
}
-static bool options_have_valid_version(git_repository_init_options *opts)
-{
- if (!opts)
- return true;
-
- if (opts->version > 0 && opts->version <= GIT_REMOTE_CALLBACKS_VERSION)
- return true;
-
- giterr_set(GITERR_INVALID, "Invalid version %d for git_repository_init_options", opts->version);
- return false;
-}
-
int git_repository_init_ext(
git_repository **out,
const char *given_repo,
@@ -1182,8 +1170,7 @@ int git_repository_init_ext(
assert(out && given_repo && opts);
- if (!options_have_valid_version(opts))
- return -1;
+ GITERR_CHECK_VERSION(opts, GIT_REPOSITORY_INIT_OPTIONS_VERSION, "git_repository_init_options");
error = repo_init_directories(&repo_path, &wd_path, given_repo, opts);
if (error < 0)
diff --git a/src/signature.h b/src/signature.h
index 599e199..97b3a05 100644
--- a/src/signature.h
+++ b/src/signature.h
@@ -15,16 +15,4 @@
int git_signature__parse(git_signature *sig, const char **buffer_out, const char *buffer_end, const char *header, char ender);
void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig);
-GIT_INLINE(bool) git_signature__has_valid_version(const git_signature *sig)
-{
- if (!sig)
- return true;
-
- if (sig->version > 0 && sig->version <= GIT_SIGNATURE_VERSION)
- return true;
-
- giterr_set(GITERR_INVALID, "Invalid version %d on git_signature", sig->version);
- return false;
-}
-
#endif
diff --git a/src/stash.c b/src/stash.c
index 67fa498..14b48a5 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -523,8 +523,7 @@ int git_stash_save(
assert(out && repo && stasher);
- if (!git_signature__has_valid_version(stasher))
- return -1;
+ GITERR_CHECK_VERSION(stasher, GIT_SIGNATURE_VERSION, "git_signature");
if ((error = ensure_non_bare_repository(repo)) < 0)
return error;
diff --git a/src/status.c b/src/status.c
index f23f40e..1ad835a 100644
--- a/src/status.c
+++ b/src/status.c
@@ -101,18 +101,6 @@ static int status_invoke_cb(
return usercb->cb(path, status, usercb->payload);
}
-static bool options_have_valid_version(const git_status_options *opts)
-{
- if (!opts)
- return true;
-
- if (opts->version > 0 && opts->version <= GIT_REMOTE_CALLBACKS_VERSION)
- return true;
-
- giterr_set(GITERR_INVALID, "Invalid version %d for git_repository_init_options", opts->version);
- return false;
-}
-
int git_status_foreach_ext(
git_repository *repo,
const git_status_options *opts,
@@ -129,8 +117,7 @@ int git_status_foreach_ext(
assert(show <= GIT_STATUS_SHOW_INDEX_THEN_WORKDIR);
- if (!options_have_valid_version(opts))
- return -1;
+ GITERR_CHECK_VERSION(opts, GIT_STATUS_OPTIONS_VERSION, "git_status_options");
if (show != GIT_STATUS_SHOW_INDEX_ONLY &&
(err = git_repository__ensure_not_bare(repo, "status")) < 0)
diff --git a/src/tag.c b/src/tag.c
index fb70837..c3b3319 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -244,8 +244,7 @@ static int git_tag_create__internal(
assert(repo && tag_name && target);
assert(!create_tag_annotation || (tagger && message));
- if (!git_signature__has_valid_version(tagger))
- return -1;
+ GITERR_CHECK_VERSION(tagger, GIT_SIGNATURE_VERSION, "git_signature");
if (git_object_owner(target) != repo) {
giterr_set(GITERR_INVALID, "The given target does not belong to this repository");