buffer: git_buf_sanitize should return a value `git_buf_sanitize` is called with user-input, and wants to sanity-check that input. Allow it to return a value if the input was malformed in a way that we cannot cope.
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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
diff --git a/src/blob.c b/src/blob.c
index 392a1fe..9706964 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -423,11 +423,12 @@ int git_blob_filter(
GIT_ASSERT_ARG(path);
GIT_ASSERT_ARG(out);
- git_buf_sanitize(out);
-
GIT_ERROR_CHECK_VERSION(
given_opts, GIT_BLOB_FILTER_OPTIONS_VERSION, "git_blob_filter_options");
+ if (git_buf_sanitize(out) < 0)
+ return -1;
+
if (given_opts != NULL)
memcpy(&opts, given_opts, sizeof(git_blob_filter_options));
diff --git a/src/branch.c b/src/branch.c
index 08fd95d..29ff0b9 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -417,7 +417,8 @@ int git_branch_upstream_name(
GIT_ASSERT_ARG(out);
GIT_ASSERT_ARG(refname);
- git_buf_sanitize(out);
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
if (!git_reference__is_branch(refname))
return not_a_local_branch(refname);
@@ -478,9 +479,8 @@ int git_branch_upstream_remote(git_buf *buf, git_repository *repo, const char *r
if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
return error;
- git_buf_sanitize(buf);
-
- if ((error = retrieve_upstream_configuration(buf, cfg, refname, "branch.%s.remote")) < 0)
+ if ((error = git_buf_sanitize(buf)) < 0 ||
+ (error = retrieve_upstream_configuration(buf, cfg, refname, "branch.%s.remote")) < 0)
return error;
if (git_buf_len(buf) == 0) {
@@ -505,7 +505,8 @@ int git_branch_remote_name(git_buf *buf, git_repository *repo, const char *refna
GIT_ASSERT_ARG(repo);
GIT_ASSERT_ARG(refname);
- git_buf_sanitize(buf);
+ if ((error = git_buf_sanitize(buf)) < 0)
+ return error;
/* Verify that this is a remote branch */
if (!git_reference__is_remote(refname)) {
diff --git a/src/buffer.c b/src/buffer.c
index f395a77..2928b17 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -140,13 +140,17 @@ void git_buf_free(git_buf *buf)
}
#endif
-void git_buf_sanitize(git_buf *buf)
+int git_buf_sanitize(git_buf *buf)
{
if (buf->ptr == NULL) {
- assert(buf->size == 0 && buf->asize == 0);
+ GIT_ASSERT_ARG(buf->size == 0 && buf->asize == 0);
+
buf->ptr = git_buf__initbuf;
- } else if (buf->asize > buf->size)
+ } else if (buf->asize > buf->size) {
buf->ptr[buf->size] = '\0';
+ }
+
+ return 0;
}
void git_buf_clear(git_buf *buf)
diff --git a/src/buffer.h b/src/buffer.h
index 6b717d2..6257b43 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -69,7 +69,7 @@ extern int git_buf_try_grow(
* git_buf__initbuf. If a buffer with a non-NULL ptr is passed in, this method
* assures that the buffer is '\0'-terminated.
*/
-extern void git_buf_sanitize(git_buf *buf);
+extern int git_buf_sanitize(git_buf *buf);
extern void git_buf_swap(git_buf *buf_a, git_buf *buf_b);
extern char *git_buf_detach(git_buf *buf);
diff --git a/src/config.c b/src/config.c
index 42f46e6..b3e7324 100644
--- a/src/config.c
+++ b/src/config.c
@@ -886,7 +886,8 @@ int git_config_get_string_buf(
int ret;
const char *str;
- git_buf_sanitize(out);
+ if ((ret = git_buf_sanitize(out)) < 0)
+ return ret;
ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS);
str = !ret ? (entry->value ? entry->value : "") : NULL;
@@ -1084,19 +1085,31 @@ void git_config_iterator_free(git_config_iterator *iter)
int git_config_find_global(git_buf *path)
{
- git_buf_sanitize(path);
+ int error;
+
+ if ((error = git_buf_sanitize(path)) < 0)
+ return error;
+
return git_sysdir_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL);
}
int git_config_find_xdg(git_buf *path)
{
- git_buf_sanitize(path);
+ int error;
+
+ if ((error = git_buf_sanitize(path)) < 0)
+ return error;
+
return git_sysdir_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG);
}
int git_config_find_system(git_buf *path)
{
- git_buf_sanitize(path);
+ int error;
+
+ if ((error = git_buf_sanitize(path)) < 0)
+ return error;
+
return git_sysdir_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM);
}
@@ -1104,7 +1117,9 @@ int git_config_find_programdata(git_buf *path)
{
int ret;
- git_buf_sanitize(path);
+ if ((ret = git_buf_sanitize(path)) < 0)
+ return ret;
+
ret = git_sysdir_find_programdata_file(path,
GIT_CONFIG_FILENAME_PROGRAMDATA);
if (ret != GIT_OK)
@@ -1360,9 +1375,12 @@ fail_parse:
int git_config_parse_path(git_buf *out, const char *value)
{
+ int error;
+
assert(out && value);
- git_buf_sanitize(out);
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
if (value[0] == '~') {
if (value[1] != '\0' && value[1] != '/') {
diff --git a/src/describe.c b/src/describe.c
index 8beffde..ebf70b0 100644
--- a/src/describe.c
+++ b/src/describe.c
@@ -780,7 +780,8 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g
GIT_ERROR_CHECK_VERSION(given, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION, "git_describe_format_options");
normalize_format_options(&opts, given);
- git_buf_sanitize(out);
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
if (opts.always_use_long_format && opts.abbreviated_size == 0) {
diff --git a/src/diff_print.c b/src/diff_print.c
index 9ce04d7..0e93fe8 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -764,8 +764,12 @@ int git_diff_print_callback__to_file_handle(
/* print a git_diff to a git_buf */
int git_diff_to_buf(git_buf *out, git_diff *diff, git_diff_format_t format)
{
+ int error;
+
assert(out && diff);
- git_buf_sanitize(out);
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
+
return git_diff_print(diff, format, git_diff_print_callback__to_buf, out);
}
@@ -799,7 +803,12 @@ out:
/* print a git_patch to a git_buf */
int git_patch_to_buf(git_buf *out, git_patch *patch)
{
+ int error;
+
assert(out && patch);
- git_buf_sanitize(out);
+
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
+
return git_patch_print(patch, git_diff_print_callback__to_buf, out);
}
diff --git a/src/filter.c b/src/filter.c
index 86db5a5..ae9220c 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -725,8 +725,9 @@ int git_filter_list_apply_to_data(
struct buf_stream writer;
int error;
- git_buf_sanitize(tgt);
- git_buf_sanitize(src);
+ if ((error = git_buf_sanitize(tgt)) < 0 ||
+ (error = git_buf_sanitize(src)) < 0)
+ return error;
if (!filters) {
git_buf_attach_notowned(tgt, src->ptr, src->size);
@@ -832,7 +833,9 @@ static int proxy_stream_close(git_writestream *s)
if (error == GIT_PASSTHROUGH) {
writebuf = &proxy_stream->input;
} else if (error == 0) {
- git_buf_sanitize(proxy_stream->output);
+ if ((error = git_buf_sanitize(proxy_stream->output)) < 0)
+ return error;
+
writebuf = proxy_stream->output;
} else {
/* close stream before erroring out taking care
@@ -1004,7 +1007,8 @@ int git_filter_list_stream_data(
git_writestream *stream_start;
int error, initialized = 0;
- git_buf_sanitize(data);
+ if ((error = git_buf_sanitize(data)) < 0)
+ return error;
if ((error = stream_list_init(&stream_start, &filter_streams, filters, target)) < 0)
goto out;
diff --git a/src/libgit2.c b/src/libgit2.c
index 07414be..9e5112d 100644
--- a/src/libgit2.c
+++ b/src/libgit2.c
@@ -122,29 +122,28 @@ int git_libgit2_features(void)
;
}
-static int config_level_to_sysdir(int config_level)
+static int config_level_to_sysdir(int *out, int config_level)
{
- int val = -1;
-
switch (config_level) {
case GIT_CONFIG_LEVEL_SYSTEM:
- val = GIT_SYSDIR_SYSTEM;
- break;
+ *out = GIT_SYSDIR_SYSTEM;
+ return 0;
case GIT_CONFIG_LEVEL_XDG:
- val = GIT_SYSDIR_XDG;
- break;
+ *out = GIT_SYSDIR_XDG;
+ return 0;
case GIT_CONFIG_LEVEL_GLOBAL:
- val = GIT_SYSDIR_GLOBAL;
- break;
+ *out = GIT_SYSDIR_GLOBAL;
+ return 0;
case GIT_CONFIG_LEVEL_PROGRAMDATA:
- val = GIT_SYSDIR_PROGRAMDATA;
- break;
+ *out = GIT_SYSDIR_PROGRAMDATA;
+ return 0;
default:
- git_error_set(
- GIT_ERROR_INVALID, "invalid config path selector %d", config_level);
+ break;
}
- return val;
+ git_error_set(
+ GIT_ERROR_INVALID, "invalid config path selector %d", config_level);
+ return -1;
}
const char *git_libgit2__user_agent(void)
@@ -190,12 +189,15 @@ int git_libgit2_opts(int key, ...)
break;
case GIT_OPT_GET_SEARCH_PATH:
- if ((error = config_level_to_sysdir(va_arg(ap, int))) >= 0) {
+ {
+ int sysdir = va_arg(ap, int);
git_buf *out = va_arg(ap, git_buf *);
const git_buf *tmp;
+ int level;
- git_buf_sanitize(out);
- if ((error = git_sysdir_get(&tmp, error)) < 0)
+ if ((error = config_level_to_sysdir(&level, sysdir)) < 0 ||
+ (error = git_buf_sanitize(out)) < 0 ||
+ (error = git_sysdir_get(&tmp, level)) < 0)
break;
error = git_buf_sets(out, tmp->ptr);
@@ -203,8 +205,12 @@ int git_libgit2_opts(int key, ...)
break;
case GIT_OPT_SET_SEARCH_PATH:
- if ((error = config_level_to_sysdir(va_arg(ap, int))) >= 0)
- error = git_sysdir_set(error, va_arg(ap, const char *));
+ {
+ int level;
+
+ if ((error = config_level_to_sysdir(&level, va_arg(ap, int))) >= 0)
+ error = git_sysdir_set(level, va_arg(ap, const char *));
+ }
break;
case GIT_OPT_SET_CACHE_OBJECT_LIMIT:
@@ -233,8 +239,8 @@ int git_libgit2_opts(int key, ...)
git_buf *out = va_arg(ap, git_buf *);
const git_buf *tmp;
- git_buf_sanitize(out);
- if ((error = git_sysdir_get(&tmp, GIT_SYSDIR_TEMPLATE)) < 0)
+ if ((error = git_buf_sanitize(out)) < 0 ||
+ (error = git_sysdir_get(&tmp, GIT_SYSDIR_TEMPLATE)) < 0)
break;
error = git_buf_sets(out, tmp->ptr);
@@ -303,7 +309,8 @@ int git_libgit2_opts(int key, ...)
case GIT_OPT_GET_USER_AGENT:
{
git_buf *out = va_arg(ap, git_buf *);
- git_buf_sanitize(out);
+ if ((error = git_buf_sanitize(out)) < 0)
+ break;
error = git_buf_sets(out, git__user_agent);
}
break;
diff --git a/src/message.c b/src/message.c
index 6c5a237..327b984 100644
--- a/src/message.c
+++ b/src/message.c
@@ -28,8 +28,10 @@ int git_message_prettify(git_buf *message_out, const char *message, int strip_co
int consecutive_empty_lines = 0;
size_t i, line_length, rtrimmed_line_length;
char *next_newline;
+ int error;
- git_buf_sanitize(message_out);
+ if ((error = git_buf_sanitize(message_out)) < 0)
+ return error;
for (i = 0; i < strlen(message); i += line_length) {
next_newline = memchr(message + i, '\n', message_len - i);
diff --git a/src/notes.c b/src/notes.c
index 68d2ae9..a3c4b7c 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -629,9 +629,8 @@ int git_note_default_ref(git_buf *out, git_repository *repo)
assert(out && repo);
- git_buf_sanitize(out);
-
- if ((error = note_get_default_ref(&default_ref, repo)) < 0)
+ if ((error = git_buf_sanitize(out)) < 0 ||
+ (error = note_get_default_ref(&default_ref, repo)) < 0)
return error;
git_buf_attach(out, default_ref, strlen(default_ref));
diff --git a/src/object.c b/src/object.c
index 749b0ca..fa97534 100644
--- a/src/object.c
+++ b/src/object.c
@@ -495,7 +495,9 @@ int git_object_short_id(git_buf *out, const git_object *obj)
assert(out && obj);
- git_buf_sanitize(out);
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
+
repo = git_object_owner(obj);
if ((error = git_repository__configmap_lookup(&len, repo, GIT_CONFIGMAP_ABBREV)) < 0)
diff --git a/src/pack-objects.c b/src/pack-objects.c
index cf10e6c..9679e8e 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -1363,8 +1363,13 @@ int git_packbuilder_foreach(git_packbuilder *pb, int (*cb)(void *buf, size_t siz
int git_packbuilder_write_buf(git_buf *buf, git_packbuilder *pb)
{
+ int error;
+
+ if ((error = git_buf_sanitize(buf)) < 0)
+ return error;
+
PREPARE_PACK;
- git_buf_sanitize(buf);
+
return write_pack(pb, &write_pack_buf, buf);
}
diff --git a/src/refspec.c b/src/refspec.c
index 4245cbb..a778f62 100644
--- a/src/refspec.c
+++ b/src/refspec.c
@@ -245,8 +245,11 @@ static int refspec_transform(
{
const char *from_star, *to_star;
size_t replacement_len, star_offset;
+ int error;
+
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
- git_buf_sanitize(out);
git_buf_clear(out);
/*
@@ -278,8 +281,12 @@ static int refspec_transform(
int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name)
{
+ int error;
+
assert(out && spec && name);
- git_buf_sanitize(out);
+
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
if (!git_refspec_src_matches(spec, name)) {
git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the source", name);
@@ -294,8 +301,12 @@ int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *nam
int git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name)
{
+ int error;
+
assert(out && spec && name);
- git_buf_sanitize(out);
+
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
if (!git_refspec_dst_matches(spec, name)) {
git_error_set(GIT_ERROR_INVALID, "ref '%s' doesn't match the destination", name);
diff --git a/src/remote.c b/src/remote.c
index f638248..a12fa00 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -648,14 +648,17 @@ int git_remote_set_pushurl(git_repository *repo, const char *remote, const char*
static int resolve_url(git_buf *resolved_url, const char *url, int direction, const git_remote_callbacks *callbacks)
{
- int status;
+ int status, error;
if (callbacks && callbacks->resolve_url) {
git_buf_clear(resolved_url);
status = callbacks->resolve_url(resolved_url, url, direction, callbacks->payload);
if (status != GIT_PASSTHROUGH) {
git_error_set_after_callback_function(status, "git_resolve_url_cb");
- git_buf_sanitize(resolved_url);
+
+ if ((error = git_buf_sanitize(resolved_url)) < 0)
+ return error;
+
return status;
}
}
@@ -2403,7 +2406,8 @@ int git_remote_default_branch(git_buf *out, git_remote *remote)
goto done;
}
- git_buf_sanitize(out);
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
/* the first one must be HEAD so if that has the symref info, we're done */
if (heads[0]->symref_target) {
diff --git a/src/repository.c b/src/repository.c
index f0d4b06..28cbf3c 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -945,10 +945,12 @@ int git_repository_discover(
const char *ceiling_dirs)
{
uint32_t flags = across_fs ? GIT_REPOSITORY_OPEN_CROSS_FS : 0;
+ int error;
assert(start_path);
- git_buf_sanitize(out);
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
return find_repo(out, NULL, NULL, NULL, start_path, flags, ceiling_dirs);
}
@@ -2609,7 +2611,8 @@ int git_repository_message(git_buf *out, git_repository *repo)
struct stat st;
int error;
- git_buf_sanitize(out);
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
if (git_buf_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0)
return -1;
diff --git a/src/submodule.c b/src/submodule.c
index c32b977..8fb3bdb 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -998,7 +998,8 @@ int git_submodule_resolve_url(git_buf *out, git_repository *repo, const char *ur
assert(out && repo && url);
- git_buf_sanitize(out);
+ if ((error = git_buf_sanitize(out)) < 0)
+ return error;
/* We do this in all platforms in case someone on Windows created the .gitmodules */
if (strchr(url, '\\')) {