git/builtin
Martin Ågren 7199203937 object_array: add and use object_array_pop()
In a couple of places, we pop objects off an object array `foo` by
decreasing `foo.nr`. We access `foo.nr` in many places, but most if not
all other times we do so read-only, e.g., as we iterate over the array.
But when we change `foo.nr` behind the array's back, it feels a bit
nasty and looks like it might leak memory.

Leaks happen if the popped element has an allocated `name` or `path`.
At the moment, that is not the case. Still, 1) the object array might
gain more fields that want to be freed, 2) a code path where we pop
might start using names or paths, 3) one of these code paths might be
copied to somewhere where we do, and 4) using a dedicated function for
popping is conceptually cleaner.

Introduce and use `object_array_pop()` instead. Release memory in the
new function. Document that popping an object leaves the associated
elements in limbo.

The converted places were identified by grepping for "\.nr\>" and
looking for "--".

Make the new function return NULL on an empty array. This is consistent
with `pop_commit()` and allows the following:

	while ((o = object_array_pop(&foo)) != NULL) {
		// do something
	}

But as noted above, we don't need to go out of our way to avoid reading
`foo.nr`. This is probably more readable:

	while (foo.nr) {
		... o = object_array_pop(&foo);
		// do something
	}

The name of `object_array_pop()` does not quite align with
`add_object_array()`. That is unfortunate. On the other hand, it matches
`object_array_clear()`. Arguably it's `add_...` that is the odd one out,
since it reads like it's used to "add" an "object array". For that
reason, side with `object_array_clear()`.

Signed-off-by: Martin Ågren <martin.agren@gmail.com>
Reviewed-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-09-24 10:06:04 +09:00
..
add.c Merge branch 'rj/add-chmod-error-message' into maint 2017-09-10 17:03:00 +09:00
am.c Merge branch 'pw/am-signoff' into maint 2017-09-10 17:02:51 +09:00
annotate.c
apply.c Convert read_mmblob to take struct object_id. 2016-09-07 12:59:42 -07:00
archive.c archive: read local configuration 2016-11-22 13:55:20 -08:00
bisect--helper.c
blame.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
branch.c ref-filter: consult want_color() before emitting colors 2017-07-13 12:42:51 -07:00
bundle.c bundle: use prefix_filename with bundle path 2017-03-21 11:18:41 -07:00
cat-file.c Merge branch 'jt/unify-object-info' 2017-07-05 13:32:57 -07:00
check-attr.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
check-ignore.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
check-mailmap.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
check-ref-format.c use xmallocz to avoid size arithmetic 2016-02-22 14:51:09 -08:00
checkout-index.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
checkout.c leak_pending: use object_array_clear(), not free() 2017-09-24 10:05:57 +09:00
clean.c color: check color.ui in git_default_config() 2017-07-13 12:42:51 -07:00
clone.c Merge branch 'bw/clone-recursive-quiet' into maint 2017-09-10 17:02:49 +09:00
column.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
commit-tree.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
commit.c builtin/commit: fix memory leak in prepare_index() 2017-09-24 10:05:45 +09:00
config.c Merge branch 'ab/free-and-null' 2017-06-24 14:28:41 -07:00
count-objects.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
credential.c credential: handle invalid arguments earlier 2017-05-30 14:45:03 +09:00
describe.c Merge branch 'sb/hashmap-customize-comparison' 2017-07-13 16:14:54 -07:00
diff-files.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
diff-index.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
diff-tree.c Merge branch 'jc/diff-tree-stale-comment' into maint 2017-06-24 15:29:31 -07:00
diff.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
difftool.c hashmap.h: compare function has access to a data field 2017-06-30 12:49:28 -07:00
fast-export.c object_array: add and use object_array_pop() 2017-09-24 10:06:04 +09:00
fetch-pack.c Rename sha1_array to oid_array 2017-03-31 08:33:56 -07:00
fetch.c Merge branch 'sb/pull-rebase-submodule' 2017-07-13 16:14:54 -07:00
fmt-merge-msg.c Merge branch 'dc/fmt-merge-msg-microcleanup' into maint 2017-08-23 14:33:52 -07:00
for-each-ref.c for-each-ref: load config earlier 2017-07-13 12:42:51 -07:00
fsck.c object_array: add and use object_array_pop() 2017-09-24 10:06:04 +09:00
gc.c Merge branch 'jk/gc-pre-detach-under-hook' 2017-07-18 12:48:10 -07:00
get-tar-commit-id.c usage: do not insist that standard input must come from a file 2015-10-16 15:27:52 -07:00
grep.c Merge branch 'jk/ref-filter-colors' into maint 2017-08-23 14:33:42 -07:00
hash-object.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
help.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
index-pack.c Merge branch 'jt/unify-object-info' 2017-07-05 13:32:57 -07:00
init-db.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
interpret-trailers.c Merge branch 'jk/parseopt-string-list' into jk/string-list-static-init 2016-06-13 10:37:48 -07:00
log.c Merge branch 'jk/reflog-walk' into maint 2017-08-23 14:33:44 -07:00
ls-files.c ls-files: don't try to prune an empty index 2017-07-17 14:55:21 -07:00
ls-remote.c wildmatch: remove unused wildopts parameter 2017-06-23 18:27:07 -07:00
ls-tree.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
mailinfo.c prefix_filename: return newly allocated string 2017-03-21 11:18:41 -07:00
mailsplit.c mailinfo & mailsplit: check for EOF while parsing 2017-05-08 12:18:19 +09:00
merge-base.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
merge-file.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
merge-index.c Convert GIT_SHA1_HEXSZ used for allocation to GIT_MAX_HEXSZ 2017-03-26 22:08:21 -07:00
merge-ours.c
merge-recursive.c i18n: merge-recursive: mark verbose message for translation 2016-09-15 13:17:32 -07:00
merge-tree.c Convert lookup_blob to struct object_id 2017-05-08 15:12:57 +09:00
merge.c Merge branch 'rs/merge-microcleanup' into maint 2017-09-10 17:03:02 +09:00
mktag.c usage: do not insist that standard input must come from a file 2015-10-16 15:27:52 -07:00
mktree.c mktree: plug memory leaks reported by Coverity 2017-05-08 12:18:19 +09:00
mv.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
name-rev.c Merge branch 'ab/wildmatch' 2017-07-10 13:42:51 -07:00
notes.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
pack-objects.c Merge branch 'rs/pack-objects-pbase-cleanup' into maint 2017-08-23 14:33:48 -07:00
pack-redundant.c pack-redundant: plug memory leak 2017-05-08 12:18:19 +09:00
pack-refs.c refs: delete pack_refs() in favor of refs_pack_refs() 2017-04-14 03:53:25 -07:00
patch-id.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
prune-packed.c sha1_file: guard against invalid loose subdirectory numbers 2017-06-24 11:09:52 -07:00
prune.c Merge branch 'rs/sha1-name-readdir-optim' 2017-07-05 13:32:56 -07:00
pull.c Merge branch 'sb/pull-rebase-submodule' 2017-07-13 16:14:54 -07:00
push.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
read-tree.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
rebase--helper.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
receive-pack.c Merge branch 'rs/use-div-round-up' 2017-07-12 15:18:23 -07:00
reflog.c object_array: add and use object_array_pop() 2017-09-24 10:06:04 +09:00
remote-ext.c remote-{ext,fd}: print usage message on invalid arguments 2017-05-30 14:45:04 +09:00
remote-fd.c remote-{ext,fd}: print usage message on invalid arguments 2017-05-30 14:45:04 +09:00
remote.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
repack.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
replace.c Merge branch 'ab/wildmatch' 2017-07-10 13:42:51 -07:00
rerere.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
reset.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
rev-list.c Merge branch 'jk/rev-list-empty-input' into maint 2017-09-10 17:02:48 +09:00
rev-parse.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
revert.c cherry-pick/revert: reject --rerere-autoupdate when continuing 2017-08-02 15:16:09 -07:00
rm.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
send-pack.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
shortlog.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
show-branch.c color: check color.ui in git_default_config() 2017-07-13 12:42:51 -07:00
show-ref.c show-ref: remove a stale comment 2017-01-23 18:51:56 -08:00
stripspace.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
submodule--helper.c Merge branch 'bw/push-options-recursively-to-submodules' into maint 2017-09-10 17:02:49 +09:00
symbolic-ref.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
tag.c Merge branch 'ma/pager-per-subcommand-action' into maint 2017-09-10 17:02:48 +09:00
unpack-file.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
unpack-objects.c Merge branch 'ab/free-and-null' 2017-06-24 14:28:41 -07:00
update-index.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
update-ref.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
update-server-info.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
upload-archive.c upload-archive: handle "-h" option early 2017-05-30 14:45:04 +09:00
var.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
verify-commit.c Merge branch 'bw/config-h' 2017-06-24 14:28:41 -07:00
verify-pack.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00
verify-tag.c ref-filter: abstract ref format into its own struct 2017-07-13 12:42:50 -07:00
worktree.c Merge branch 'ab/free-and-null' 2017-06-24 14:28:41 -07:00
write-tree.c config: don't include config.h by default 2017-06-15 12:56:22 -07:00