diff options
author | Jeff King <peff@peff.net> | 2017-09-05 12:15:21 (GMT) |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-09-06 08:19:54 (GMT) |
commit | bfffb48c5d520a5f181716b0330774a03cd3fa39 (patch) | |
tree | 78488ee47fe26a6f3c1592c69402ba36f7661792 /builtin/update-index.c | |
parent | ee4d8e455c0981269213eccfd788a9bb9adf77d9 (diff) | |
download | git-bfffb48c5d520a5f181716b0330774a03cd3fa39.zip git-bfffb48c5d520a5f181716b0330774a03cd3fa39.tar.gz git-bfffb48c5d520a5f181716b0330774a03cd3fa39.tar.bz2 |
stop leaking lock structs in some simple cases
Now that it's safe to declare a "struct lock_file" on the
stack, we can do so (and avoid an intentional leak). These
leaks were found by running t0000 and t0001 under valgrind
(though certainly other similar leaks exist and just don't
happen to be exercised by those tests).
Initializing the lock_file's inner tempfile with NULL is not
strictly necessary in these cases, but it's a good practice
to model. It means that if we were to call a function like
rollback_lock_file() on a lock that was never taken in the
first place, it becomes a quiet noop (rather than undefined
behavior).
Likewise, it's always safe to rollback_lock_file() on a file
that has already been committed or deleted, since that
operation is a noop on an inactive lockfile (and that's why
the case in config.c can drop the "if (lock)" check as we
move away from using a pointer).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/update-index.c')
-rw-r--r-- | builtin/update-index.c | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/builtin/update-index.c b/builtin/update-index.c index d562f2e..655e6d6 100644 --- a/builtin/update-index.c +++ b/builtin/update-index.c @@ -915,7 +915,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) struct refresh_params refresh_args = {0, &has_errors}; int lock_error = 0; int split_index = -1; - struct lock_file *lock_file; + struct lock_file lock_file = LOCK_INIT; struct parse_opt_ctx_t ctx; strbuf_getline_fn getline_fn; int parseopt_state = PARSE_OPT_UNKNOWN; @@ -1014,11 +1014,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) git_config(git_default_config, NULL); - /* We can't free this memory, it becomes part of a linked list parsed atexit() */ - lock_file = xcalloc(1, sizeof(struct lock_file)); - /* we will diagnose later if it turns out that we need to update it */ - newfd = hold_locked_index(lock_file, 0); + newfd = hold_locked_index(&lock_file, 0); if (newfd < 0) lock_error = errno; @@ -1153,11 +1150,11 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) exit(128); unable_to_lock_die(get_index_file(), lock_error); } - if (write_locked_index(&the_index, lock_file, COMMIT_LOCK)) + if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) die("Unable to write new index file"); } - rollback_lock_file(lock_file); + rollback_lock_file(&lock_file); return has_errors ? 1 : 0; } |