From patchwork Mon Oct 4 00:46:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12533109 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B658C433EF for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 18110610CC for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231989AbhJDAsH (ORCPT ); Sun, 3 Oct 2021 20:48:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231967AbhJDAsG (ORCPT ); Sun, 3 Oct 2021 20:48:06 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5F22C061780 for ; Sun, 3 Oct 2021 17:46:17 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id t8so27584955wri.1 for ; Sun, 03 Oct 2021 17:46:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fHNMVX+tmtrz838dcrFxdPv9ckV28+cFUly6/2r1sp4=; b=WTNpWxKnuWWz7djf98AvUJId/Tyxo6yEVch2h/JK6IVU+qxC9iJxkooeUHcHMkrAqa uHJ5amNZVeuJfA88H7IeoceJgkNItaTLjF/LfvMA0Wwz6ibe325jAjTqJBG4DBYvR3rU ifO4a2JPjwDG1tDQ3ncQTxGJYNDHK9Ztxj9pfkR5Fu0EhdFlWvuQfmauLnLUokCdTG7n 8jRdUlQVa+Kk0pTYhPwh9+qfjkqiztjx0EKHEGFIPAkHin6tenUVTMW3fS/xnwh11Kp1 hA9jcMFRVT1T+zC3k6QFXQudVxHHhxfebzYh/3wWIkpO2ubr34hj5Ve+BcD8wq1ls+hD plwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fHNMVX+tmtrz838dcrFxdPv9ckV28+cFUly6/2r1sp4=; b=BLyguJiPLTz52b/SmBjHWBZAiw7DVs6CaB5gXPehkrZXIde5ug+gf7Dxcj6OISViGW 4oCRac6QUBQLUA1LMMieBrqu/hY6GnT7mO5vZbjmwDR5bZ3YGvZ1dcUqq3RWOobhoMYq TWCJY42lBBZTOxG9VSL7ML8EdDV0VpkHOkes4THbUvgs5Te0gEaHz+HND1JXrlmoHhQM yxwSW8m94jfEM5jH3MWCRR2d70Zc+iGw7g/hMa/3IIEXZsp+ywbfICN5xf81C4ow13/E e0Ckf1tU12Hfmk/D6Ehp5SdUYqyN7WuxkTOWYKiuKf8LS+8XYDCLIvWV1b1NR8jbRJoW iEtg== X-Gm-Message-State: AOAM532P322Edizk/4dFSqTabSfaPd6J0wBHLLvS9OCOZP584X5wgk9M /LolXcln0Fh8USTbJ2DlIIcm1zn8VYnakg== X-Google-Smtp-Source: ABdhPJwPU5jYO25mjhkzy/S6gnhz8q3PVYleMpTM/SO7ESoBMeVCi0+qdAE7ChAmGR8l7hxQVsJxcQ== X-Received: by 2002:a5d:6da9:: with SMTP id u9mr10885847wrs.58.1633308376014; Sun, 03 Oct 2021 17:46:16 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q7sm13526773wrc.55.2021.10.03.17.46.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 17:46:15 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , =?utf-8?q?Martin_=C3=85gren?= , Andrzej Hunt , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 01/10] unpack-trees.[ch]: define and use a UNPACK_TREES_OPTIONS_INIT Date: Mon, 4 Oct 2021 02:46:02 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1404.g83021034c5d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Almost all users of "struct unpack_trees_options" allocate it on the stack already, let's provide an *_INIT macro for them to use. This will make later changes that would like to initialize other members of the struct easier[1][2], but for now we're keeping it compatible with a memset() to "0" with an "{ 0 }" initialization. This covers all callers except a special-case in "merge-recursive.c", which will be dealt with in a subsequent commit. 1. https://lore.kernel.org/git/87k0ixrv23.fsf@evledraar.gmail.com/ 2. https://lore.kernel.org/git/87fstlrumj.fsf@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- archive.c | 3 +-- builtin/am.c | 6 ++---- builtin/checkout.c | 6 ++---- builtin/clone.c | 3 +-- builtin/commit.c | 3 +-- builtin/merge.c | 3 +-- builtin/read-tree.c | 3 +-- builtin/reset.c | 3 +-- builtin/sparse-checkout.c | 3 +-- builtin/stash.c | 6 ++---- diff-lib.c | 3 +-- merge-ort.c | 3 +-- merge.c | 3 +-- reset.c | 2 +- sequencer.c | 3 +-- unpack-trees.h | 2 ++ 16 files changed, 20 insertions(+), 35 deletions(-) diff --git a/archive.c b/archive.c index a3bbb091256..210d7235c5a 100644 --- a/archive.c +++ b/archive.c @@ -269,7 +269,7 @@ int write_archive_entries(struct archiver_args *args, write_archive_entry_fn_t write_entry) { struct archiver_context context; - struct unpack_trees_options opts; + struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree_desc t; int err; struct strbuf path_in_archive = STRBUF_INIT; @@ -300,7 +300,6 @@ int write_archive_entries(struct archiver_args *args, * Setup index and instruct attr to read index only */ if (!args->worktree_attributes) { - memset(&opts, 0, sizeof(opts)); opts.index_only = 1; opts.head_idx = -1; opts.src_index = args->repo->index; diff --git a/builtin/am.c b/builtin/am.c index e4a0ff9cd7c..82641ce68ec 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -1901,7 +1901,7 @@ static void am_resolve(struct am_state *state) static int fast_forward_to(struct tree *head, struct tree *remote, int reset) { struct lock_file lock_file = LOCK_INIT; - struct unpack_trees_options opts; + struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree_desc t[2]; if (parse_tree(head) || parse_tree(remote)) @@ -1911,7 +1911,6 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset) refresh_cache(REFRESH_QUIET); - memset(&opts, 0, sizeof(opts)); opts.head_idx = 1; opts.src_index = &the_index; opts.dst_index = &the_index; @@ -1940,7 +1939,7 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset) static int merge_tree(struct tree *tree) { struct lock_file lock_file = LOCK_INIT; - struct unpack_trees_options opts; + struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree_desc t[1]; if (parse_tree(tree)) @@ -1948,7 +1947,6 @@ static int merge_tree(struct tree *tree) hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); - memset(&opts, 0, sizeof(opts)); opts.head_idx = 1; opts.src_index = &the_index; opts.dst_index = &the_index; diff --git a/builtin/checkout.c b/builtin/checkout.c index 8c69dcdf72a..fea4533dbec 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -639,10 +639,9 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o, int worktree, int *writeout_error, struct branch_info *info) { - struct unpack_trees_options opts; + struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree_desc tree_desc; - memset(&opts, 0, sizeof(opts)); opts.head_idx = -1; opts.update = worktree; opts.skip_unmerged = !worktree; @@ -719,9 +718,8 @@ static int merge_working_tree(const struct checkout_opts *opts, } else { struct tree_desc trees[2]; struct tree *tree; - struct unpack_trees_options topts; + struct unpack_trees_options topts = UNPACK_TREES_OPTIONS_INIT; - memset(&topts, 0, sizeof(topts)); topts.head_idx = -1; topts.src_index = &the_index; topts.dst_index = &the_index; diff --git a/builtin/clone.c b/builtin/clone.c index ff1d3d447a3..0df820c5970 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -655,7 +655,7 @@ static int checkout(int submodule_progress) struct object_id oid; char *head; struct lock_file lock_file = LOCK_INIT; - struct unpack_trees_options opts; + struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree *tree; struct tree_desc t; int err = 0; @@ -683,7 +683,6 @@ static int checkout(int submodule_progress) hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); - memset(&opts, 0, sizeof opts); opts.update = 1; opts.merge = 1; opts.clone = 1; diff --git a/builtin/commit.c b/builtin/commit.c index e7320f66f95..6cc7313bad8 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -303,7 +303,7 @@ static void add_remove_files(struct string_list *list) static void create_base_index(const struct commit *current_head) { struct tree *tree; - struct unpack_trees_options opts; + struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree_desc t; if (!current_head) { @@ -311,7 +311,6 @@ static void create_base_index(const struct commit *current_head) return; } - memset(&opts, 0, sizeof(opts)); opts.head_idx = 1; opts.index_only = 1; opts.merge = 1; diff --git a/builtin/merge.c b/builtin/merge.c index 3fbdacc7db4..73290a07fcc 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -670,9 +670,8 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head, int i, nr_trees = 0; struct tree *trees[MAX_UNPACK_TREES]; struct tree_desc t[MAX_UNPACK_TREES]; - struct unpack_trees_options opts; + struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; - memset(&opts, 0, sizeof(opts)); opts.head_idx = 2; opts.src_index = &the_index; opts.dst_index = &the_index; diff --git a/builtin/read-tree.c b/builtin/read-tree.c index 485e7b04794..847182fdad6 100644 --- a/builtin/read-tree.c +++ b/builtin/read-tree.c @@ -116,7 +116,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix) int i, stage = 0; struct object_id oid; struct tree_desc t[MAX_UNPACK_TREES]; - struct unpack_trees_options opts; + struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; int prefix_set = 0; struct lock_file lock_file = LOCK_INIT; const struct option read_tree_options[] = { @@ -158,7 +158,6 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix) OPT_END() }; - memset(&opts, 0, sizeof(opts)); opts.head_idx = -1; opts.src_index = &the_index; opts.dst_index = &the_index; diff --git a/builtin/reset.c b/builtin/reset.c index 51c9e2f43ff..86c604b21e9 100644 --- a/builtin/reset.c +++ b/builtin/reset.c @@ -51,10 +51,9 @@ static int reset_index(const char *ref, const struct object_id *oid, int reset_t int i, nr = 0; struct tree_desc desc[2]; struct tree *tree; - struct unpack_trees_options opts; + struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; int ret = -1; - memset(&opts, 0, sizeof(opts)); opts.head_idx = 1; opts.src_index = &the_index; opts.dst_index = &the_index; diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c index d0f5c4702be..4c3c29fb580 100644 --- a/builtin/sparse-checkout.c +++ b/builtin/sparse-checkout.c @@ -195,7 +195,7 @@ static void clean_tracked_sparse_directories(struct repository *r) static int update_working_directory(struct pattern_list *pl) { enum update_sparsity_result result; - struct unpack_trees_options o; + struct unpack_trees_options o = UNPACK_TREES_OPTIONS_INIT; struct lock_file lock_file = LOCK_INIT; struct repository *r = the_repository; @@ -205,7 +205,6 @@ static int update_working_directory(struct pattern_list *pl) r->index->sparse_checkout_patterns = pl; - memset(&o, 0, sizeof(o)); o.verbose_update = isatty(2); o.update = 1; o.head_idx = -1; diff --git a/builtin/stash.c b/builtin/stash.c index 8f42360ca91..1137e5fcbe8 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -233,7 +233,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix) static int reset_tree(struct object_id *i_tree, int update, int reset) { int nr_trees = 1; - struct unpack_trees_options opts; + struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree_desc t[MAX_UNPACK_TREES]; struct tree *tree; struct lock_file lock_file = LOCK_INIT; @@ -244,8 +244,6 @@ static int reset_tree(struct object_id *i_tree, int update, int reset) hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); - memset(&opts, 0, sizeof(opts)); - tree = parse_tree_indirect(i_tree); if (parse_tree(tree)) return -1; @@ -799,7 +797,7 @@ static void diff_include_untracked(const struct stash_info *info, struct diff_op const struct object_id *oid[] = { &info->w_commit, &info->u_tree }; struct tree *tree[ARRAY_SIZE(oid)]; struct tree_desc tree_desc[ARRAY_SIZE(oid)]; - struct unpack_trees_options unpack_tree_opt = { 0 }; + struct unpack_trees_options unpack_tree_opt = UNPACK_TREES_OPTIONS_INIT; int i; for (i = 0; i < ARRAY_SIZE(oid); i++) { diff --git a/diff-lib.c b/diff-lib.c index ca085a03efc..8a08d9af4eb 100644 --- a/diff-lib.c +++ b/diff-lib.c @@ -526,13 +526,12 @@ static int diff_cache(struct rev_info *revs, { struct tree *tree; struct tree_desc t; - struct unpack_trees_options opts; + struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; tree = parse_tree_indirect(tree_oid); if (!tree) return error("bad tree object %s", tree_name ? tree_name : oid_to_hex(tree_oid)); - memset(&opts, 0, sizeof(opts)); opts.head_idx = 1; opts.index_only = cached; opts.diff_index_cached = (cached && diff --git a/merge-ort.c b/merge-ort.c index 35aa979c3a4..75d2b8e4b99 100644 --- a/merge-ort.c +++ b/merge-ort.c @@ -4021,9 +4021,8 @@ static int checkout(struct merge_options *opt, /* Switch the index/working copy from old to new */ int ret; struct tree_desc trees[2]; - struct unpack_trees_options unpack_opts; + struct unpack_trees_options unpack_opts = UNPACK_TREES_OPTIONS_INIT; - memset(&unpack_opts, 0, sizeof(unpack_opts)); unpack_opts.head_idx = -1; unpack_opts.src_index = opt->repo->index; unpack_opts.dst_index = opt->repo->index; diff --git a/merge.c b/merge.c index 6e736881d90..086f04b0f97 100644 --- a/merge.c +++ b/merge.c @@ -50,7 +50,7 @@ int checkout_fast_forward(struct repository *r, int overwrite_ignore) { struct tree *trees[MAX_UNPACK_TREES]; - struct unpack_trees_options opts; + struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree_desc t[MAX_UNPACK_TREES]; int i, nr_trees = 0; struct dir_struct dir = DIR_INIT; @@ -79,7 +79,6 @@ int checkout_fast_forward(struct repository *r, init_tree_desc(t+i, trees[i]->buffer, trees[i]->size); } - memset(&opts, 0, sizeof(opts)); if (overwrite_ignore) { dir.flags |= DIR_SHOW_IGNORED; setup_standard_excludes(&dir); diff --git a/reset.c b/reset.c index 79310ae071b..d13984ab781 100644 --- a/reset.c +++ b/reset.c @@ -21,7 +21,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, struct object_id head_oid; struct tree_desc desc[2] = { { NULL }, { NULL } }; struct lock_file lock = LOCK_INIT; - struct unpack_trees_options unpack_tree_opts = { 0 }; + struct unpack_trees_options unpack_tree_opts = UNPACK_TREES_OPTIONS_INIT; struct tree *tree; const char *reflog_action; struct strbuf msg = STRBUF_INIT; diff --git a/sequencer.c b/sequencer.c index 614d56f5e21..abd85b6c562 100644 --- a/sequencer.c +++ b/sequencer.c @@ -3652,7 +3652,7 @@ static int do_reset(struct repository *r, struct lock_file lock = LOCK_INIT; struct tree_desc desc; struct tree *tree; - struct unpack_trees_options unpack_tree_opts; + struct unpack_trees_options unpack_tree_opts = UNPACK_TREES_OPTIONS_INIT; int ret = 0; if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) @@ -3691,7 +3691,6 @@ static int do_reset(struct repository *r, } } - memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); setup_unpack_trees_porcelain(&unpack_tree_opts, "reset"); unpack_tree_opts.head_idx = 1; unpack_tree_opts.src_index = r->index; diff --git a/unpack-trees.h b/unpack-trees.h index 2d88b19dca7..ecf256cbcea 100644 --- a/unpack-trees.h +++ b/unpack-trees.h @@ -91,6 +91,8 @@ struct unpack_trees_options { struct checkout_metadata meta; }; +#define UNPACK_TREES_OPTIONS_INIT { 0 } + int unpack_trees(unsigned n, struct tree_desc *t, struct unpack_trees_options *options); From patchwork Mon Oct 4 00:46:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12533101 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 374E9C43219 for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 261EF6124F for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232000AbhJDAsI (ORCPT ); Sun, 3 Oct 2021 20:48:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230508AbhJDAsG (ORCPT ); Sun, 3 Oct 2021 20:48:06 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFFB4C0613EC for ; Sun, 3 Oct 2021 17:46:18 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id z184-20020a1c7ec1000000b003065f0bc631so17894995wmc.0 for ; Sun, 03 Oct 2021 17:46:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Dhk3sduOucoBBuG8GZdIrjhofCoKSIqDQ2p2Rot2YDE=; b=Mx05rYPHWHKwfDpuXY2zujcreKFywStH/PeP8aQ0NCRwQ9PmJNX1pie9KZfTxvN6HF 4MmXebzTRhjZJHQsg8FaDVFf9cP3Y27ZXx1FsAbVqwvlFFW2YBrkxFT6mhGHrYhPLlkc WXTRr3ytqNvqOjbNymfaZP/mbX93zCN4ia1M2aqwxi3PeaHTclQMTxx7xesQCSuhquxy WljPwQTYgG6dz7HKQqIJ7O4ee5L6S3EUc5YjrmmhxYM+Rxhw5U8wghKTbp4c0ZTzht/L Qt2BBw4GYKjW60zo99HUUf6LjM+pOPpKJPxIJZm9QGmhVSLJHgEctH4J9S5FItHNax0P K6aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Dhk3sduOucoBBuG8GZdIrjhofCoKSIqDQ2p2Rot2YDE=; b=AVCLb5JRiY86xJmbjDupH3KkG1UDoVRonC8b49GPhfG83LaG0dKOCydNb6hCEEWX4D qNAn89YlNj+0J/1yGY3GA1SDb/6lAhhnI3hQ+OL7Paqb7xdwnFpz8JABTyQqclZbNU7p UgqG4l9TIhh+ywK4NEFel2XCQ8cje24zBbMhqfjbVcpJtTS4PH5TBVqScP+nVKOj68ek Dy1dzVmc1eEfSLcqviE3+Hfkq5p3GDmxTMiSBWU6ZAF+iSxTnFhG/uISOAVAhdpvEEx2 jgF6Ge/Q6f6i4IACL58CfIXtzq1sfGstSkATpHsSZ+KTguVNmFdIQGhzVaSGXdGSUDW+ ddyw== X-Gm-Message-State: AOAM532a1YqsoutZOYFC4+ojfH95VBSxOFHDlITo6j+UAQchRZf+BoHA rcldHTGBztAeL8mzKXqAnpZgSk4f9liiVw== X-Google-Smtp-Source: ABdhPJwQUnA1iMa0HQmLqb2qbXzZIuFi+7PdOh382EYa2CoIXdEO0gcmtk2tr1dcmxEAKUl+JKlX9Q== X-Received: by 2002:a1c:ed0a:: with SMTP id l10mr16187239wmh.140.1633308376900; Sun, 03 Oct 2021 17:46:16 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q7sm13526773wrc.55.2021.10.03.17.46.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 17:46:16 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , =?utf-8?q?Martin_=C3=85gren?= , Andrzej Hunt , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 02/10] merge-recursive.c: call a new unpack_trees_options_init() function Date: Mon, 4 Oct 2021 02:46:03 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1404.g83021034c5d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In the preceding commit we introduced a new UNPACK_TREES_OPTIONS_INIT macro, but "merge-recursive.c" could not be converted to it since it (re-)initializes a "struct unpack_trees_options" on the heap. In order to convert use the macro as the source of truth for initialization we need to not only convert the initialization in unpack_trees_start(), but also call the new unpack_trees_options_init() just after the CALLOC_ARRAY() in merge_start(). When we call merge_trees() we'll call merge_start() followed by merge_trees_internal(), and it will call unpack_trees_start() before it does much of anything. So it's covered by an initialization in unpack_trees_start(). But when merge_recursive() is called it will call merge_start() followed by merge_recursive_internal(), which won't call unpack_trees_start() until its own call call to merge_trees_internal(), but in the meantime it might end up using that calloc'd "struct unpack_trees_options". This was OK before, as setup_unpack_trees_porcelain() would call strvec_init(), and our "struct dir_struct" in turn is OK with being NULL'd. Let's convert the former to macro initialization, we'll deal with the latter in a subsequent commit. Signed-off-by: Ævar Arnfjörð Bjarmason --- merge-recursive.c | 3 ++- unpack-trees.c | 8 ++++++-- unpack-trees.h | 5 ++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/merge-recursive.c b/merge-recursive.c index e594d4c3fa1..d24a4903f1d 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -405,7 +405,7 @@ static int unpack_trees_start(struct merge_options *opt, struct tree_desc t[3]; struct index_state tmp_index = { NULL }; - memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts)); + unpack_trees_options_init(&opt->priv->unpack_opts); if (opt->priv->call_depth) opt->priv->unpack_opts.index_only = 1; else @@ -3704,6 +3704,7 @@ static int merge_start(struct merge_options *opt, struct tree *head) CALLOC_ARRAY(opt->priv, 1); string_list_init_dup(&opt->priv->df_conflict_file_set); + unpack_trees_options_init(&opt->priv->unpack_opts); return 0; } diff --git a/unpack-trees.c b/unpack-trees.c index 8ea0a542da8..393c1f35a5d 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -108,8 +108,6 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts, const char **msgs = opts->msgs; const char *msg; - strvec_init(&opts->msgs_to_free); - if (!strcmp(cmd, "checkout")) msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) ? _("Your local changes to the following files would be overwritten by checkout:\n%%s" @@ -189,6 +187,12 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts, opts->unpack_rejects[i].strdup_strings = 1; } +void unpack_trees_options_init(struct unpack_trees_options *o) +{ + struct unpack_trees_options blank = UNPACK_TREES_OPTIONS_INIT; + memcpy(o, &blank, sizeof(*o)); +} + void clear_unpack_trees_porcelain(struct unpack_trees_options *opts) { strvec_clear(&opts->msgs_to_free); diff --git a/unpack-trees.h b/unpack-trees.h index ecf256cbcea..892b65ea623 100644 --- a/unpack-trees.h +++ b/unpack-trees.h @@ -91,7 +91,10 @@ struct unpack_trees_options { struct checkout_metadata meta; }; -#define UNPACK_TREES_OPTIONS_INIT { 0 } +#define UNPACK_TREES_OPTIONS_INIT { \ + .msgs_to_free = STRVEC_INIT, \ +} +void unpack_trees_options_init(struct unpack_trees_options *o); int unpack_trees(unsigned n, struct tree_desc *t, struct unpack_trees_options *options); From patchwork Mon Oct 4 00:46:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12533107 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5EC39C43217 for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 40C4A6103B for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232014AbhJDAsN (ORCPT ); Sun, 3 Oct 2021 20:48:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231967AbhJDAsH (ORCPT ); Sun, 3 Oct 2021 20:48:07 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81761C0613EC for ; Sun, 3 Oct 2021 17:46:19 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id v17so27482955wrv.9 for ; Sun, 03 Oct 2021 17:46:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mAwWdLQI94A6oAvvZU0Xn0wBJFWa2z7IYqmzNSG/X6o=; b=kYEqYsG9tG32HwQ6BK4PjBIH3dhsP1We2bNEi9Ktmyh5BoRhvONzbJDbSa8PjsnqVa Mc+aWugJD1DxAX/3TlFOvf7b29oP/7sjbLqUm9sEOdgUnxZuAC1JGkALzlg9IJ5Iujaz lm3cy/61oX+LIcyI7R9whjuaSl+vFmzI9tU1u9UkazlHkD84+TdIyWTKyBSDudwJqig8 FH2uYu3FIb4ZdtSU1S9ZHU3JD6wP0FomIuTlglTLxlpCvIYTuPaYePp6IBIOCSeYMhWL JOA4WXP7GnLSwowor0zzf8zt5nrQHOqUtTFHw7HiNI8pHGG1rBlfTssrmHO3kGwUEQqS FC2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mAwWdLQI94A6oAvvZU0Xn0wBJFWa2z7IYqmzNSG/X6o=; b=FNI8mx6Xqx9q77KjD+jvju1G8eumK7pT/5on6n762GebvN2U4bDt+Sqlfg5G00/xUz T2HXj3hfgM0PrQMOUI56y+5lmHxgWmBdqkHVgo0kMyPZRKLIEQnzj0dyeYlKdNvdQOJr qi99LMAeggmVklpfze8sCkjg9cZGul3lPJGKZABuf439pC6Ngpy0lvwM61/4vOg9nseI yCcN7vGq5k06RFXMsVRBhcOuLmSoC8UQkX+C2CCD6Wlkkf4sC5/khrVbxjs623sGgSv9 cKzMppn9REeY9BuF47RkvfN/ZKaNdhUsT7iP8SlHd+N9QuLX9FwpJf54mkDRVAh3QEZJ 3s1g== X-Gm-Message-State: AOAM531N7o7YDeZLn/dIH9Gvq5wvznx+LgEJUFDr+ISFRvn2XQ/NWvcw ETRpN4LXK0/Zw2PRs3sZJos4DWGyimFBcQ== X-Google-Smtp-Source: ABdhPJxefHDyJSui1WEtmpirLMy/UIWi+C8xX8f747HgsNiZPVy01kpevglmRpQ978NLjsU23A/3HQ== X-Received: by 2002:adf:a2c8:: with SMTP id t8mr11092429wra.215.1633308377812; Sun, 03 Oct 2021 17:46:17 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q7sm13526773wrc.55.2021.10.03.17.46.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 17:46:17 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , =?utf-8?q?Martin_=C3=85gren?= , Andrzej Hunt , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 03/10] unpack-trees.[ch]: embed "dir" in "struct unpack_trees_options" Date: Mon, 4 Oct 2021 02:46:04 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1404.g83021034c5d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the "struct dir" in "struct unpack_trees_options" away from a pointer to being embedded in the struct itself, this mean that we can initialize it with our new UNPACK_TREES_OPTIONS_INIT macro. As it turned out nothing actually needed to provide its own "struct dir" to this API, there's another patch to "hide" the struct instead, see the discussion at [1], but I think just allocating it on the stack along with the rest makes more sense. This fixes a memory leak in "builtin/checkout.c" that's been there since the clear_unpack_trees_porcelain() function was added in 1c41d2805e4 (unpack_trees_options: free messages when done, 2018-05-21), i.e. we still had a memory leak from allocating the "dir" member here. That "dir" member had in turn been with us ever since 782c2d65c24 (Build in checkout, 2008-02-07). This fixes that memory leak, and allows us to remove the boilerplate dir_clear() elsewhere in favor of just using clear_unpack_trees_porcelain(). 1. https://lore.kernel.org/git/87ilyjviiy.fsf@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/checkout.c | 5 ++--- builtin/read-tree.c | 11 ++++------- merge-ort.c | 7 ++----- merge.c | 7 ++----- unpack-trees.c | 10 ++++------ unpack-trees.h | 4 +++- 6 files changed, 17 insertions(+), 27 deletions(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index fea4533dbec..d4b88affba7 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -745,9 +745,8 @@ static int merge_working_tree(const struct checkout_opts *opts, &new_branch_info->commit->object.oid : &new_branch_info->oid, NULL); if (opts->overwrite_ignore) { - topts.dir = xcalloc(1, sizeof(*topts.dir)); - topts.dir->flags |= DIR_SHOW_IGNORED; - setup_standard_excludes(topts.dir); + topts.dir.flags |= DIR_SHOW_IGNORED; + setup_standard_excludes(&topts.dir); } tree = parse_tree_indirect(old_branch_info->commit ? &old_branch_info->commit->object.oid : diff --git a/builtin/read-tree.c b/builtin/read-tree.c index 847182fdad6..06f3b97ac05 100644 --- a/builtin/read-tree.c +++ b/builtin/read-tree.c @@ -53,20 +53,17 @@ static int index_output_cb(const struct option *opt, const char *arg, static int exclude_per_directory_cb(const struct option *opt, const char *arg, int unset) { - struct dir_struct *dir; struct unpack_trees_options *opts; BUG_ON_OPT_NEG(unset); opts = (struct unpack_trees_options *)opt->value; - if (opts->dir) + if (opts->dir.exclude_per_dir) die("more than one --exclude-per-directory given."); - dir = xcalloc(1, sizeof(*opts->dir)); - dir->flags |= DIR_SHOW_IGNORED; - dir->exclude_per_dir = arg; - opts->dir = dir; + opts->dir.flags |= DIR_SHOW_IGNORED; + opts->dir.exclude_per_dir = arg; /* We do not need to nor want to do read-directory * here; we are merely interested in reusing the * per directory ignore stack mechanism. @@ -208,7 +205,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix) if ((opts.update || opts.index_only) && !opts.merge) die("%s is meaningless without -m, --reset, or --prefix", opts.update ? "-u" : "-i"); - if ((opts.dir && !opts.update)) + if ((opts.dir.exclude_per_dir && !opts.update)) die("--exclude-per-directory is meaningless unless -u"); if (opts.merge && !opts.index_only) setup_work_tree(); diff --git a/merge-ort.c b/merge-ort.c index 75d2b8e4b99..e526b78b88d 100644 --- a/merge-ort.c +++ b/merge-ort.c @@ -4045,9 +4045,8 @@ static int checkout(struct merge_options *opt, unpack_opts.verbose_update = (opt->verbosity > 2); unpack_opts.fn = twoway_merge; if (1/* FIXME: opts->overwrite_ignore*/) { - CALLOC_ARRAY(unpack_opts.dir, 1); - unpack_opts.dir->flags |= DIR_SHOW_IGNORED; - setup_standard_excludes(unpack_opts.dir); + unpack_opts.dir.flags |= DIR_SHOW_IGNORED; + setup_standard_excludes(&unpack_opts.dir); } parse_tree(prev); init_tree_desc(&trees[0], prev->buffer, prev->size); @@ -4056,8 +4055,6 @@ static int checkout(struct merge_options *opt, ret = unpack_trees(2, trees, &unpack_opts); clear_unpack_trees_porcelain(&unpack_opts); - dir_clear(unpack_opts.dir); - FREE_AND_NULL(unpack_opts.dir); return ret; } diff --git a/merge.c b/merge.c index 086f04b0f97..9cb32990dd9 100644 --- a/merge.c +++ b/merge.c @@ -53,7 +53,6 @@ int checkout_fast_forward(struct repository *r, struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree_desc t[MAX_UNPACK_TREES]; int i, nr_trees = 0; - struct dir_struct dir = DIR_INIT; struct lock_file lock_file = LOCK_INIT; refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); @@ -80,9 +79,8 @@ int checkout_fast_forward(struct repository *r, } if (overwrite_ignore) { - dir.flags |= DIR_SHOW_IGNORED; - setup_standard_excludes(&dir); - opts.dir = &dir; + opts.dir.flags |= DIR_SHOW_IGNORED; + setup_standard_excludes(&opts.dir); } opts.head_idx = 1; @@ -100,7 +98,6 @@ int checkout_fast_forward(struct repository *r, clear_unpack_trees_porcelain(&opts); return -1; } - dir_clear(&dir); clear_unpack_trees_porcelain(&opts); if (write_locked_index(r->index, &lock_file, COMMIT_LOCK)) diff --git a/unpack-trees.c b/unpack-trees.c index 393c1f35a5d..94767d3f96f 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -196,6 +196,7 @@ void unpack_trees_options_init(struct unpack_trees_options *o) void clear_unpack_trees_porcelain(struct unpack_trees_options *opts) { strvec_clear(&opts->msgs_to_free); + dir_clear(&opts->dir); memset(opts->msgs, 0, sizeof(opts->msgs)); } @@ -2085,7 +2086,7 @@ static int verify_clean_subdirectory(const struct cache_entry *ce, */ int namelen; int i; - struct dir_struct d; + struct dir_struct d = DIR_INIT; char *pathbuf; int cnt = 0; @@ -2136,9 +2137,7 @@ static int verify_clean_subdirectory(const struct cache_entry *ce, */ pathbuf = xstrfmt("%.*s/", namelen, ce->name); - memset(&d, 0, sizeof(d)); - if (o->dir) - d.exclude_per_dir = o->dir->exclude_per_dir; + d.exclude_per_dir = o->dir.exclude_per_dir; i = read_directory(&d, o->src_index, pathbuf, namelen+1, NULL); if (i) return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name); @@ -2179,8 +2178,7 @@ static int check_ok_to_remove(const char *name, int len, int dtype, if (ignore_case && icase_exists(o, name, len, st)) return 0; - if (o->dir && - is_excluded(o->dir, o->src_index, name, &dtype)) + if (is_excluded(&o->dir, o->src_index, name, &dtype)) /* * ce->name is explicitly excluded, so it is Ok to * overwrite it. diff --git a/unpack-trees.h b/unpack-trees.h index 892b65ea623..40c4841748d 100644 --- a/unpack-trees.h +++ b/unpack-trees.h @@ -5,6 +5,7 @@ #include "strvec.h" #include "string-list.h" #include "tree-walk.h" +#include "dir.h" #define MAX_UNPACK_TREES MAX_TRAVERSE_TREES @@ -66,7 +67,7 @@ struct unpack_trees_options { dry_run; const char *prefix; int cache_bottom; - struct dir_struct *dir; + struct dir_struct dir; struct pathspec *pathspec; merge_fn_t fn; const char *msgs[NB_UNPACK_TREES_WARNING_TYPES]; @@ -93,6 +94,7 @@ struct unpack_trees_options { #define UNPACK_TREES_OPTIONS_INIT { \ .msgs_to_free = STRVEC_INIT, \ + .dir = DIR_INIT, \ } void unpack_trees_options_init(struct unpack_trees_options *o); From patchwork Mon Oct 4 00:46:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12533103 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69E84C4167B for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4F27E6124F for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232009AbhJDAsP (ORCPT ); Sun, 3 Oct 2021 20:48:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232003AbhJDAsL (ORCPT ); Sun, 3 Oct 2021 20:48:11 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83DDDC0613EC for ; Sun, 3 Oct 2021 17:46:20 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id e12so7261266wra.4 for ; Sun, 03 Oct 2021 17:46:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=91+q5KqikvkmtBnUielKrquxw8mEIZoCu4H1DVjwi6Y=; b=ZxcCYlv5qZHqsuyDvI+PvkyuJbhKmCS56YBshmBilaxCjxvUZvqQaMayFZRWHJZeHm YYU79zQI3P1Wd8/YZl3fuzO6vcmkLI0AGeIAhbDKvFajk/KXMAQpt5eiLMWRFwnZkeLZ 317e/A39RfZgGTFz2VQz1ZArd20EpNBksDKKP57l3MJyuOvhXL/d8pbfQDhfkEG2OXUW K4ZQ+32CmjrQbRNikFRD8M7DifjikZzxw4NasrqH/jsPwRzbgMLdhgVg0Q+kkaKz9l96 AdcdjgGJOF0y+7B8DVNGzJnoUIfP7AeVjyjnmbRR8nd5ZqNdtoc0m9ktv8WHorki+Sfu tSJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=91+q5KqikvkmtBnUielKrquxw8mEIZoCu4H1DVjwi6Y=; b=Tx6bX2ktI6eeqEZXHL8zNtG9Q0hnhugpkP1zT6HJy5Qmc2IHlScqRIC7i94K0CKnMA NqUJGjFyOR20d+ovhXw72tG9Gi4PIFcPxgsDFl8xLhxQDAeRGSymeK3bZe+QvlhFrfhH szdNLkHtMjqeia8Gi07fjKgdBFxWo6SsDVo/RyRBOa4ZJoM8DvPA5S2kLq0gXY7BwLVr yZC+0LxBrOV6naWzPZU0wwMWqjkDI5NPz3AknMYpBJolQYSKWncYj+1Y13WxVAou+P/K Nfnd01q+WaTBD+3ani52Pix5RA7ieOwfwh+9hWBm7DeIeYekd9ZqmLRkvl8MVOd73WPZ PbUA== X-Gm-Message-State: AOAM531VHldn+0eo914O26w3bDEzQ989YkA/6DCxEQvX7IZ63IV3R5tQ pj7yDVve6l36gYMtB7Y+VjP4mrRim/eEWg== X-Google-Smtp-Source: ABdhPJycneP38yavIivdj86cslib6KqUTWj0g6bOMzQjDhr/j6ZVBpJOnARwCq/oNJIRfJv2dGtcSA== X-Received: by 2002:a5d:59a6:: with SMTP id p6mr10782824wrr.142.1633308378907; Sun, 03 Oct 2021 17:46:18 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q7sm13526773wrc.55.2021.10.03.17.46.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 17:46:18 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , =?utf-8?q?Martin_=C3=85gren?= , Andrzej Hunt , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 04/10] unpack-trees API: don't have clear_unpack_trees_porcelain() reset Date: Mon, 4 Oct 2021 02:46:05 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1404.g83021034c5d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the clear_unpack_trees_porcelain() to be like a *_release() function, not a *_reset() (in strbuf.c terms). Let's move the only API user that relied on the latter to doing its own unpack_trees_options_init(). See the commit that introduced unpack_trees_options_init() for details on the control flow involved here. Signed-off-by: Ævar Arnfjörð Bjarmason --- merge-recursive.c | 1 + unpack-trees.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/merge-recursive.c b/merge-recursive.c index d24a4903f1d..a77f66b006c 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -442,6 +442,7 @@ static void unpack_trees_finish(struct merge_options *opt) { discard_index(&opt->priv->orig_index); clear_unpack_trees_porcelain(&opt->priv->unpack_opts); + unpack_trees_options_init(&opt->priv->unpack_opts); } static int save_files_dirs(const struct object_id *oid, diff --git a/unpack-trees.c b/unpack-trees.c index 94767d3f96f..e7365322e82 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -197,7 +197,6 @@ void clear_unpack_trees_porcelain(struct unpack_trees_options *opts) { strvec_clear(&opts->msgs_to_free); dir_clear(&opts->dir); - memset(opts->msgs, 0, sizeof(opts->msgs)); } static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce, From patchwork Mon Oct 4 00:46:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12533111 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 81042C4167D for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 760666103B for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232030AbhJDAsR (ORCPT ); Sun, 3 Oct 2021 20:48:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232008AbhJDAsM (ORCPT ); Sun, 3 Oct 2021 20:48:12 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E933C061780 for ; Sun, 3 Oct 2021 17:46:21 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id s198-20020a1ca9cf000000b0030d6986ea9fso2162897wme.1 for ; Sun, 03 Oct 2021 17:46:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mReH4798/aXY2sjYDQgIWxChOSmQ7Mc1m37pt0wf3KY=; b=e3HVO+UQO0iGpKiDtU+WVp8E1C87ooGWde1QqaLcGbPyjqzdoBlyS7zAB/5Fuhi/w2 AN+KM1NWRaQ/HnkxXoAwlCoe3RjGS0LL42y3d9jRlLnTzmC/IVUMBqgw4akvAeVi6CrG HsSV3L2UiG1mt4IXwQ2nSwvUt0XASJ+fe/htKRQ5Pl0hfqqPtk6sl6fBRsKSiAaYD3UP ojmtsZnhzm+wn6+WOjuXQBCYxfRUCLvl6krqGHL5i3f2b066iyAVBuBmAQxQ/L+uneVc RWIAeS2e2Co7BSaiVhpUZ0LqKd+Xhu5k/v/fldnp5LImPzwmzlClu90kNsKFiPYGiFOO lvlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mReH4798/aXY2sjYDQgIWxChOSmQ7Mc1m37pt0wf3KY=; b=i1NOdEAAR3LERAGcNE++nC55q2DOfFchC3pwXFMcjvvj7f6cPotTXsb9d5E61O2KQW 9ZRN8+DSsBnYRxBSVlW1sJxZSc4bTZ909Htg5puCssWgz5sDLQfmb+nUDqS8NBWCFwLw vn5sUdAdGTXSFLs8YoBYaHVnZISzawaGmuncHZGM2K6KLBrjF3sJoT4ZY9ZnJTPYnxCi 8w8a2Dgj5Y/9J9mv7lO80ozlaG3E0IjjJAryhGRNVM7U99bS7WF0ahjnEFf3jDLeVdZJ w4O97y6QkR8h18x5PLHCF22Sa3QOSN6BPccNU5HzpHgC389gXyTKyKI6m4EC4BqVR/vj rdbg== X-Gm-Message-State: AOAM533g/yCeWtAOkFRni0IPZXX9f8sn6wksly0M/3CCrDQ3JFFF4Exp BKeur2PcxwwumAil59rU++NhLv1m7tpGYA== X-Google-Smtp-Source: ABdhPJwaZylArgXxvduuSKwSkaCJiV9kepVUuy1yRTq7uFEuvBbhwWfYfPrWPYy0txXxE1MQrJBLAA== X-Received: by 2002:a1c:21d7:: with SMTP id h206mr10620164wmh.163.1633308379558; Sun, 03 Oct 2021 17:46:19 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q7sm13526773wrc.55.2021.10.03.17.46.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 17:46:19 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , =?utf-8?q?Martin_=C3=85gren?= , Andrzej Hunt , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 05/10] dir.[ch]: make DIR_INIT mandatory Date: Mon, 4 Oct 2021 02:46:06 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1404.g83021034c5d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The dir_init() initializer has been documented as being mandatory since eceba532141 (dir: fix problematic API to avoid memory leaks, 2020-08-18), but both it and my ce93a4c6127 (dir.[ch]: replace dir_init() with DIR_INIT, 2021-07-01) managed to miss this callsite in "add-interactive.c" added before those two commits in ab1e1cccaf6 (built-in add -i: re-implement `add-untracked` in C, 2019-11-29). In addition my change to remove dir_init() neglected to update this documentation. Let's use "must be initialized with" in reference to "DIR_INIT". We have one lazy initialization which pre-dates eceba532141 in dir.c. Adjusting this callsite is a prerequisite for removing it in favor of trusting the macro to initialize the "struct dir_struct" correctly. Signed-off-by: Ævar Arnfjörð Bjarmason --- add-interactive.c | 2 +- dir.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/add-interactive.c b/add-interactive.c index 6498ae196f1..27daea8d1b3 100644 --- a/add-interactive.c +++ b/add-interactive.c @@ -826,7 +826,7 @@ static int get_untracked_files(struct repository *r, struct prefix_item_list *files, const struct pathspec *ps) { - struct dir_struct dir = { 0 }; + struct dir_struct dir = DIR_INIT; size_t i; struct strbuf buf = STRBUF_INIT; diff --git a/dir.h b/dir.h index 83f46c0fb4c..ff3b4a7f602 100644 --- a/dir.h +++ b/dir.h @@ -19,7 +19,7 @@ * CE_SKIP_WORKTREE marked. If you want to exclude files, make sure you have * loaded the index first. * - * - Prepare `struct dir_struct dir` using `dir_init()` function. + * - The `struct dir_struct dir` must be initialized with `DIR_INIT`. * * - To add single exclude pattern, call `add_pattern_list()` and then * `add_pattern()`. From patchwork Mon Oct 4 00:46:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12533113 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8A966C41535 for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 818D9610CC for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232049AbhJDAsT (ORCPT ); Sun, 3 Oct 2021 20:48:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232011AbhJDAsM (ORCPT ); Sun, 3 Oct 2021 20:48:12 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33F99C061783 for ; Sun, 3 Oct 2021 17:46:22 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id d6so27451099wrc.11 for ; Sun, 03 Oct 2021 17:46:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=GWUdVnEpZXSuQnFu/I6N6ig92Hm2queX9QSH/S3rsSY=; b=YunObwZ6WPkO5H7tUDvqN4tC1c5FcAfgHp8yOx2XX4b3ul24ukcu9zcX7Azq47QVtg S+TrmmQc5i3dgXzMH+/ss7Y0ZmPc0gdQqtYbQf+8rxn7yI5AEQg4S+m1cMF9jWj5aTOy JTW9cR+0AuOMKUA9/BN81lFUKS3eyHya8Jthy8WeKhm+oIO5KHsdfPXEb0F+r4vv7s8i SX9CERzEP7bKLGqUOy+28+3LSpSH+D8LWyTEW6lMK7HzUCl+yG2h9Xr+FklVBeCD2OJG OAIWN58yRxICfUlGv9Q/AcGNmCdeR1gqJ0JAw4sbM7WGaBO6mSt1Lr1UmXbNwxCIFEPf UcPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GWUdVnEpZXSuQnFu/I6N6ig92Hm2queX9QSH/S3rsSY=; b=be3GbMpBd1m6djbJ0bif6HGD+46lB7j69goyMfKpRmL6BNs1j43QOu5xBK6ZA0OtrM GN95cytEKlx/ZtvxJUfbmAyXI5+/66BRm53K0Bul/nr/NrjKXuklJ0UQAgd0sCXrl0MH Wh/5JWazX5KHuKyjHiyLC42Z7RI2b1Q1YkIBBnB4yjyN0tC0dlwqE0xglLyxrKx88DHW OgGTFHUAof6wdcGRs+vpPzXH9LDgTUUvQJXwgxhUTzZlQzsKmSMOKYfcyn9FEoOK747I 7HxkRYWrIsHWtxmewgHLr+M7AbIFdu28ib1zNW1O2AQ3X/nB+XGnOm0Q8w/WYYkoFawZ 3Ulg== X-Gm-Message-State: AOAM530y4yLpZ4e0FUdcb4gQvICfJcU7/TgZaqGHI778gZZDqm7JvKUU 7NGppUs4r5v/XPY1uLvxaRUqaOEfrGGBBw== X-Google-Smtp-Source: ABdhPJycnyBbGXFCZmPpv4ofiSadzskf+WfM5TF3aM96K17vLm+3kJwiHK++Brux3QeJu1/N21TRGA== X-Received: by 2002:adf:8bd2:: with SMTP id w18mr11024820wra.432.1633308380548; Sun, 03 Oct 2021 17:46:20 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q7sm13526773wrc.55.2021.10.03.17.46.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 17:46:19 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , =?utf-8?q?Martin_=C3=85gren?= , Andrzej Hunt , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 06/10] dir.c: get rid of lazy initialization Date: Mon, 4 Oct 2021 02:46:07 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1404.g83021034c5d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Remove the "Lazy initialization" in prep_exclude() left behind by aceb9429b37 (prep_exclude: remove the artificial PATH_MAX limit, 2014-07-14). Now that every caller who sets up a "struct dir_struct" is using the DIR_INIT macro we can rely on it to have done the initialization. As noted in an analysis of the previous control flow[1] an earlier passing of of "dir->basebuf.buf" to strncmp() wasn't buggy, as we'd only reach that code on subsequent invocations of prep_exclude(), i.e. after this strbuf_init() had been run. But keeping track of that makes for hard-to-read code. Let's just rely on the initialization instead. This does change the behavior of this code in that it won't be pre-growing the strbuf to a size of PATH_MAX. I think that's OK. That we were using PATH_MAX at all is just a relic from this being a fixed buffer from way back in f87f9497486 (git-ls-files: --exclude mechanism updates., 2005-07-24). Pre-allocating PATH_MAX was the opposite of an optimization in this case. I logged all "basebuf.buf" values when running the test suite, and by far the most common one (around 80%) is "", which we now won't allocate at all for, and just use the "strbuf_slopbuf". The second most common one was "a/", followed by other common cases of short relative paths. So using the default "struct strbuf" growth pattern is a much better allocation optimization in this case. 1. https://lore.kernel.org/git/87sfxhohsj.fsf@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- dir.c | 8 -------- dir.h | 4 +++- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/dir.c b/dir.c index 39fce3bcba7..efc87c2e405 100644 --- a/dir.c +++ b/dir.c @@ -1550,14 +1550,6 @@ static void prep_exclude(struct dir_struct *dir, if (dir->pattern) return; - /* - * Lazy initialization. All call sites currently just - * memset(dir, 0, sizeof(*dir)) before use. Changing all of - * them seems lots of work for little benefit. - */ - if (!dir->basebuf.buf) - strbuf_init(&dir->basebuf, PATH_MAX); - /* Read from the parent directories and push them down. */ current = stk ? stk->baselen : -1; strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current); diff --git a/dir.h b/dir.h index ff3b4a7f602..e3757c6099e 100644 --- a/dir.h +++ b/dir.h @@ -342,7 +342,9 @@ struct dir_struct { unsigned visited_directories; }; -#define DIR_INIT { 0 } +#define DIR_INIT { \ + .basebuf = STRBUF_INIT, \ +} struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp); From patchwork Mon Oct 4 00:46:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12533115 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A68E5C4167E for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8FF22610CC for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232068AbhJDAsU (ORCPT ); Sun, 3 Oct 2021 20:48:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232017AbhJDAsO (ORCPT ); Sun, 3 Oct 2021 20:48:14 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2E4EC061786 for ; Sun, 3 Oct 2021 17:46:23 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id m22so22089094wrb.0 for ; Sun, 03 Oct 2021 17:46:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9nVZBfOfRQxVn39jyMFXapk1NHhUOqWXQYsM8w3j/SE=; b=mzZUNGrJj/dP6dJugq+Hffx/vY6XrQT0EwJD7ucc6ahrzLJU+NYw0PB8fCRlAZloRa UyV+yEjavpg1rxc+73Vvg5LE6leT5XUFwMGSJU6nsuja7lkrtXj/desvygJvbIienUHC xD89eA/H+5TJVPxq7WmLbZFbcwlkFTdp8kpz1mi0fT+sUG1uHpDqWmEsbZ2lQV8JqgCT CfSw54a7qMqyRIy3D+iMIVPcQ7EpT49hUBqrMqFTu60Po+dlBqR5oGbPXjVLl64rwln5 yMMPSivSoJTS5TpzO7mXYABIRJ1jy98EUGvBNYxHhcfgFsRe2MrEaH1zZw9OQB7Yp11G KGRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9nVZBfOfRQxVn39jyMFXapk1NHhUOqWXQYsM8w3j/SE=; b=76u4Hg64zLJWFHVSWJampapsyRYYILmdx4m4mRXyxGLRoqvMVbm1PTMKHKp40ikOmz /07qwDpbOchFlZoKQxH21VF+BcB2A2WKh8PoD8FqbPazOR9J4Xzx4YJoK/XX1zMcou/Q fzL63DQZAJXBZRg+ko0WM38dXibkyD9EHYtydyK+LO5MSXk28Nz5+vP/SLVLtPC0ABjX SHtZHKi4cMNRnYrVkI2XQxJ/fs6e3mRn4rc4pC3Dn2CaIs77KSoUFP3q/i2cIK9qqqbe dq2lGNGfUNlU8jIy6D8s3x88X18LxISD953X5eBrFZ/VcuyBaysya/crdHqRBxwdmpKn x+og== X-Gm-Message-State: AOAM5337Ed7UkTqemc3tadXP/8LNSUp6TmVgkFJZM/pqO2JjwzoUPZQt R/ExZZDNrwe3V9V+//F3KgQGc6Xmv9f+zA== X-Google-Smtp-Source: ABdhPJwcHJnddbcotu8PSp6t3bCVU6v7HqMq+HjoOOqc0IJCTduA0itETTXpSfo47wiNMb/xCu9CXQ== X-Received: by 2002:adf:d0cc:: with SMTP id z12mr10808438wrh.72.1633308381357; Sun, 03 Oct 2021 17:46:21 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q7sm13526773wrc.55.2021.10.03.17.46.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 17:46:20 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , =?utf-8?q?Martin_=C3=85gren?= , Andrzej Hunt , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 07/10] unpack-trees API: rename clear_unpack_trees_porcelain() Date: Mon, 4 Oct 2021 02:46:08 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1404.g83021034c5d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Since a preceding commit we've been using clear_unpack_trees_porcelain() to call dir_clear(). So it's no longer a function that corresponds to setup_unpack_trees_porcelain(), as it was when it was added in 1c41d2805e4 (unpack_trees_options: free messages when done, 2018-05-21). Instead it's similar to strbuf_release() and other similar generic "free" functions. Let's rename it to avoid any future confusion on the topic. Let's also update the API documentation for it to note this, and to cover e.g. the code added around update_sparsity() in 4ee5d50fc39 (sparse-checkout: use improved unpack_trees porcelain messages, 2020-03-27). Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/checkout.c | 2 +- builtin/sparse-checkout.c | 2 +- merge-ort.c | 2 +- merge-recursive.c | 2 +- merge.c | 4 ++-- reset.c | 2 +- unpack-trees.c | 2 +- unpack-trees.h | 8 +++++--- 8 files changed, 13 insertions(+), 11 deletions(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index d4b88affba7..482d17676a0 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -757,7 +757,7 @@ static int merge_working_tree(const struct checkout_opts *opts, init_tree_desc(&trees[1], tree->buffer, tree->size); ret = unpack_trees(2, trees, &topts); - clear_unpack_trees_porcelain(&topts); + unpack_trees_options_release(&topts); if (ret == -1) { /* * Unpack couldn't do a trivial merge; either diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c index 4c3c29fb580..b1221fd01d3 100644 --- a/builtin/sparse-checkout.c +++ b/builtin/sparse-checkout.c @@ -219,7 +219,7 @@ static int update_working_directory(struct pattern_list *pl) setup_unpack_trees_porcelain(&o, "sparse-checkout"); result = update_sparsity(&o); - clear_unpack_trees_porcelain(&o); + unpack_trees_options_release(&o); if (result == UPDATE_SPARSITY_WARNINGS) /* diff --git a/merge-ort.c b/merge-ort.c index e526b78b88d..0a5937364c9 100644 --- a/merge-ort.c +++ b/merge-ort.c @@ -4054,7 +4054,7 @@ static int checkout(struct merge_options *opt, init_tree_desc(&trees[1], next->buffer, next->size); ret = unpack_trees(2, trees, &unpack_opts); - clear_unpack_trees_porcelain(&unpack_opts); + unpack_trees_options_release(&unpack_opts); return ret; } diff --git a/merge-recursive.c b/merge-recursive.c index a77f66b006c..316cb2ca907 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -441,7 +441,7 @@ static int unpack_trees_start(struct merge_options *opt, static void unpack_trees_finish(struct merge_options *opt) { discard_index(&opt->priv->orig_index); - clear_unpack_trees_porcelain(&opt->priv->unpack_opts); + unpack_trees_options_release(&opt->priv->unpack_opts); unpack_trees_options_init(&opt->priv->unpack_opts); } diff --git a/merge.c b/merge.c index 9cb32990dd9..2f618425aff 100644 --- a/merge.c +++ b/merge.c @@ -95,10 +95,10 @@ int checkout_fast_forward(struct repository *r, if (unpack_trees(nr_trees, t, &opts)) { rollback_lock_file(&lock_file); - clear_unpack_trees_porcelain(&opts); + unpack_trees_options_release(&opts); return -1; } - clear_unpack_trees_porcelain(&opts); + unpack_trees_options_release(&opts); if (write_locked_index(r->index, &lock_file, COMMIT_LOCK)) return error(_("unable to write new index file")); diff --git a/reset.c b/reset.c index d13984ab781..f4bf3fbfac0 100644 --- a/reset.c +++ b/reset.c @@ -133,7 +133,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, leave_reset_head: strbuf_release(&msg); rollback_lock_file(&lock); - clear_unpack_trees_porcelain(&unpack_tree_opts); + unpack_trees_options_release(&unpack_tree_opts); while (nr) free((void *)desc[--nr].buffer); return ret; diff --git a/unpack-trees.c b/unpack-trees.c index e7365322e82..bea598c9ece 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -193,7 +193,7 @@ void unpack_trees_options_init(struct unpack_trees_options *o) memcpy(o, &blank, sizeof(*o)); } -void clear_unpack_trees_porcelain(struct unpack_trees_options *opts) +void unpack_trees_options_release(struct unpack_trees_options *opts) { strvec_clear(&opts->msgs_to_free); dir_clear(&opts->dir); diff --git a/unpack-trees.h b/unpack-trees.h index 40c4841748d..a8d1f083b33 100644 --- a/unpack-trees.h +++ b/unpack-trees.h @@ -41,10 +41,12 @@ enum unpack_trees_error_types { void setup_unpack_trees_porcelain(struct unpack_trees_options *opts, const char *cmd); -/* - * Frees resources allocated by setup_unpack_trees_porcelain(). +/** + * Frees resources allocated by function that take the "struct + * unpack_trees_options". Always call this after using unpack_trees(), + * update_sparsity() etc. */ -void clear_unpack_trees_porcelain(struct unpack_trees_options *opts); +void unpack_trees_options_release(struct unpack_trees_options *opts); struct unpack_trees_options { unsigned int reset, From patchwork Mon Oct 4 00:46:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12533117 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1563AC433EF for ; Mon, 4 Oct 2021 00:47:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E9CCF6124F for ; Mon, 4 Oct 2021 00:47:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232083AbhJDAsV (ORCPT ); Sun, 3 Oct 2021 20:48:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232018AbhJDAsO (ORCPT ); Sun, 3 Oct 2021 20:48:14 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6833EC061787 for ; Sun, 3 Oct 2021 17:46:24 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id r7so12014177wrc.10 for ; Sun, 03 Oct 2021 17:46:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2gDr1/7/NG70W4XTmct3idmTdsijB5o0xLFQ3c8ch3w=; b=VjjOtLBavnMgoQjUqjmEmQFU0Yick2oTtBXtna4Mz75dasPpVYpNlSE2E9NeStlA/t r4RpnUY0TtEr9VpMTd5WrZgdfyZTAXLEh+OKea9snuPEjNJO3pw6yALPKrEbEms6wQP2 yLzAYjc3q6STTNGry7tI4rx0Ys9dHEYxUzFTCwGGeAA+AgnMHrJ5RS9Bys5qIQGt7YBm hbCE3N/GR/6Hyf10DrzjKfmvHPrF/EbY/+DWMWbUzLblRcNPZhFKtodzlAqxnMTOUFvu WC+qWeI20hIxRzaBA4sbbFsEcyCaRLJsupxF+cfdcWnJorVvJ+gkZh8fK2H3Mf8OI7Hb cJzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2gDr1/7/NG70W4XTmct3idmTdsijB5o0xLFQ3c8ch3w=; b=vH3ajgYbkzjbAPo4ZIjILwaY0Ew1nj1hnrkfORzZDHwmecdhbzjgAptr4LgPcmEZBd Q6gGO7qNH2doa6j0hgqjyFzf5qZ63YJVF2VGFXmEHBUwBFIJfpNcg7xY6eKc+2Ij+DMH qh1WyHME803l5fTL6fotmG9qBKvQeL84XAzFDFJVU4+WcClSjdfPmXsrQVR48r/6iyMQ fP3rs9AGgN3lW7OK7YQza6VVzgTiJcJ7sjO+TDCIkLK7HyU6u8cvFJqTwPHbQJxeeTi+ cXk7U2ZvIWVJJQgcS3+1u5eb6Tb/EY+o/JtV7UeHSrJs6sDXW2xBaIqfw/FcSNmjvcog B5RA== X-Gm-Message-State: AOAM530Maa09D1u1x1D/HdPV8cOAETC/RFp1rNubSLJbNP3qSNo0TBOP 8HODUGGkO55LK2gCBtPlfu8fJlXEcG+hMg== X-Google-Smtp-Source: ABdhPJxZZ+JS0OwoFloKg/VmaPmerYEO+Mysvkgm80ZNqpUFb1fKwy+OFokMxsmy6Rx5jQWGEv/Ufw== X-Received: by 2002:a05:6000:1a89:: with SMTP id f9mr10864824wry.19.1633308382445; Sun, 03 Oct 2021 17:46:22 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q7sm13526773wrc.55.2021.10.03.17.46.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 17:46:21 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , =?utf-8?q?Martin_=C3=85gren?= , Andrzej Hunt , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 08/10] unpack-trees: don't leak memory in verify_clean_subdirectory() Date: Mon, 4 Oct 2021 02:46:09 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1404.g83021034c5d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix two different but related memory leaks in verify_clean_subdirectory(). We leaked both the "pathbuf" if read_directory() returned non-zero, and we never cleaned up our own "struct dir_struct" either. * "pathbuf": When the read_directory() call followed by the free(pathbuf) was added in c81935348be (Fix switching to a branch with D/F when current branch has file D., 2007-03-15) we didn't bother to free() before we called die(). But when this code was later libified in 203a2fe1170 (Allow callers of unpack_trees() to handle failure, 2008-02-07) we started to leak as we returned data to the caller. This fixes that memory leak, which can be observed under SANITIZE=leak with e.g. the "t1001-read-tree-m-2way.sh" test. * "struct dir_struct": We've leaked the dir_struct ever since this code was added back in c81935348be. When that commit was written there wasn't an equivalent of dir_clear(). Since it was added in 270be816049 (dir.c: provide clear_directory() for reclaiming dir_struct memory, 2013-01-06) we've omitted freeing the memory allocated here. This memory leak could also be observed under SANITIZE=leak and the "t1001-read-tree-m-2way.sh" test. Signed-off-by: Ævar Arnfjörð Bjarmason --- unpack-trees.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/unpack-trees.c b/unpack-trees.c index bea598c9ece..260e7ec5bb4 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -2138,9 +2138,10 @@ static int verify_clean_subdirectory(const struct cache_entry *ce, d.exclude_per_dir = o->dir.exclude_per_dir; i = read_directory(&d, o->src_index, pathbuf, namelen+1, NULL); + dir_clear(&d); + free(pathbuf); if (i) return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name); - free(pathbuf); return cnt; } From patchwork Mon Oct 4 00:46:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12533105 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75A89C4321E for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5D725610CC for ; Mon, 4 Oct 2021 00:46:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232026AbhJDAsQ (ORCPT ); Sun, 3 Oct 2021 20:48:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232020AbhJDAsO (ORCPT ); Sun, 3 Oct 2021 20:48:14 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01D91C061788 for ; Sun, 3 Oct 2021 17:46:25 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id z184-20020a1c7ec1000000b003065f0bc631so17895224wmc.0 for ; Sun, 03 Oct 2021 17:46:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4DwdVw2flRapc0zfN8iyO3mwISs9NRQQ+06uIRm9J10=; b=boePLSCrrGuJnef0BTKDvVrO9EIzz+/sTW9Jzuo3tq4zqlUZxyYcL/zD/Ij5d54kcg j+/Hd6DbqVgcQgIgylHLMSluLe2CQXPLRQtev96hNHzxdgxrHWMqa/4PD8g++zX3v2pt ypBrd+TJxP/ONtClFhxVGgpnR/aIw2hG+rOu0SDvozGhZnAmESMywOrXsT2SAk0X2klv O8hZDsmP+AS6Mmv7Qm7XvCJTdZsOHdYSTTJUhzm1Z4k09UN6ZmyWRsEENuB0BNHurUbS VDhV567AO6a3z1+6m9J2HoUf3plSDzJL2zT4i351u2XMXM3bpWNH2jujQ4zHz0hGgt+b Q5Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4DwdVw2flRapc0zfN8iyO3mwISs9NRQQ+06uIRm9J10=; b=f8v/PKva9rp2aL8D2ry5j3ReUc8cfRHcptuy16C+I6k9JGga/ohxulFE+P0udSgMWX 2PuHaWGQcl3PRDfXR+lB0kTnaIkQO2e33QplzUb7t+U35rJlII/b76dpGi0pVmKJuhwz sJmiFchiOQc9UWwAJeAQrnVT/7vB9EudXKrljuqf1ObP3oBBio3H3a6Y0VVtE1gRRQo0 TDerxUG+dcoBzzGYAoz6qMN/UH0KZLbk/AkC1KRMansO9HGr1Fsi51vg7lCyCoue14FM qM/nKPqWtmvzlqNtP4m8rIeQ/9xiji6ToNE5vc/LxRgKM6GrvzlG9SnSR+WuPppQLIpe HR8g== X-Gm-Message-State: AOAM533ylY5WWJ14H74o7Wgc+Mu6GMSfSJdqedW+iRRemjsmwVOzQZo3 /P7YwoMdg0Y8f7zO76ZReYHPx/4+R+hg+A== X-Google-Smtp-Source: ABdhPJzVWSZYNKTQ38XatA7kj1/vBlyg2LUghU/b/t9PfGZ1xLtANWL8QRKGV2qNqpUp6HiV9dEJJw== X-Received: by 2002:a1c:7d56:: with SMTP id y83mr10656992wmc.86.1633308383344; Sun, 03 Oct 2021 17:46:23 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q7sm13526773wrc.55.2021.10.03.17.46.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 17:46:22 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , =?utf-8?q?Martin_=C3=85gren?= , Andrzej Hunt , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 09/10] merge.c: avoid duplicate unpack_trees_options_release() code Date: Mon, 4 Oct 2021 02:46:10 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1404.g83021034c5d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Refactor code added in 1c41d2805e4 (unpack_trees_options: free messages when done, 2018-05-21) to use a ret/goto pattern, rather than duplicating the end cleanup in the function. This control flow will be matched in subsequent commits by various other similar code, which often needs to do more than just call unpack_trees_options_release(). Let's change this to consistently use the same pattern everywhere. Signed-off-by: Ævar Arnfjörð Bjarmason --- merge.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/merge.c b/merge.c index 2f618425aff..2e3714ccaa0 100644 --- a/merge.c +++ b/merge.c @@ -54,6 +54,7 @@ int checkout_fast_forward(struct repository *r, struct tree_desc t[MAX_UNPACK_TREES]; int i, nr_trees = 0; struct lock_file lock_file = LOCK_INIT; + int ret = 0; refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); @@ -95,12 +96,14 @@ int checkout_fast_forward(struct repository *r, if (unpack_trees(nr_trees, t, &opts)) { rollback_lock_file(&lock_file); - unpack_trees_options_release(&opts); - return -1; + ret = -1; + goto cleanup; } - unpack_trees_options_release(&opts); if (write_locked_index(r->index, &lock_file, COMMIT_LOCK)) - return error(_("unable to write new index file")); - return 0; + ret = error(_("unable to write new index file")); + +cleanup: + unpack_trees_options_release(&opts); + return ret; } From patchwork Mon Oct 4 00:46:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12533119 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B475C433F5 for ; Mon, 4 Oct 2021 00:47:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 35FBE6124F for ; Mon, 4 Oct 2021 00:47:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232099AbhJDAsW (ORCPT ); Sun, 3 Oct 2021 20:48:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232035AbhJDAsO (ORCPT ); Sun, 3 Oct 2021 20:48:14 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF81CC061794 for ; Sun, 3 Oct 2021 17:46:25 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id l18-20020a05600c4f1200b002f8cf606262so17852139wmq.1 for ; Sun, 03 Oct 2021 17:46:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pNGAzF2L6GF/Rm6GdZu/R2+R4qby32vzkVXcoF03WB0=; b=YItCNnMHq9Q6DyhFkqPMXOM8G8y1rbnZ8oK6eI350Elj8Qeu91tE0jpojoWfmDGoCg dKI9xVoFVWHoF1nBOx66USQMjCggAxt0bMqfxqT+29Id4WuA/ve07/ZeeGi+S8Uk+HSm NHMVFu83RQoLn81xggSzhTv3TLU5iYbEpeUfv1jLxEAxb7l1klxs6nlJWKB3zufTwbNR jq3Rf+xQR+ie8g7txAlxoa9wt2DUUaecANQsvymstiV9CWVoErtr1ikAjnednm7lbs4b R3zdo2X659NgxObfeiLuvAKwend+Gl1byCw4/h06/XsC7ltuzqXrnQ/OiS3WtkthwZAL MZLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pNGAzF2L6GF/Rm6GdZu/R2+R4qby32vzkVXcoF03WB0=; b=GlOs07GocsNedzU272BLREmRQo6gNY80rEKBBX8/m4v9cROxmUTEcZ0j6Txtb1FMe8 y4lX9ILj0XAp7i9BuLNJNF44EK4tj+LxnDZV/5xx76xGO/CTTr+C8rSMy8HREblCN8K8 y+wDCDwwdM4kv+I4wuukL3MHloTnZ+5bZhvj03/AYbTNVwo4VcLcW5WsnPQ7F6pL7YmI ibht7Wl0WP18JzCyog95Coe0gC5i+qqt3hQPifbmgbY+RtWWnnfbKK9xHT9KEofqU+t+ BwJUqwEB6bRe0tFGuXeQYTsyxZjEZyrEbp6DOW4sWQbComNGZn5p/5dVJTJI6B2SaA5e fmLw== X-Gm-Message-State: AOAM533tqj4r6RlhfIVNOhxPZA6Fp1QWE98RUTNngWT/dE6FGtRxN9+s g/xXjn3PqBRN0XTGG2yqJJrG49s+PwNZYw== X-Google-Smtp-Source: ABdhPJyMaYaffnmgzjp94RPXz4F17gOICDvuj6ODoGmpZpHFFZudqPi5A9g+rYCP/H2SBwznMiT2Hg== X-Received: by 2002:a1c:f216:: with SMTP id s22mr16294941wmc.27.1633308384155; Sun, 03 Oct 2021 17:46:24 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q7sm13526773wrc.55.2021.10.03.17.46.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 17:46:23 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , =?utf-8?q?Martin_=C3=85gren?= , Andrzej Hunt , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 10/10] built-ins: plug memory leaks with unpack_trees_options_release() Date: Mon, 4 Oct 2021 02:46:11 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1404.g83021034c5d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Plug memory leaks in various built-ins that were missing unpack_trees_options_release() calls. In an earlier commit these functions were all made to use the "UNPACK_TREES_OPTIONS_INIT" macro, now let's have the ones that didn't clean up their memory do so. Signed-off-by: Ævar Arnfjörð Bjarmason --- archive.c | 9 +++++++-- builtin/am.c | 17 ++++++++++++----- builtin/checkout.c | 9 +++++++-- builtin/clone.c | 1 + builtin/commit.c | 6 +++++- builtin/merge.c | 6 ++++-- builtin/read-tree.c | 14 ++++++++++---- builtin/reset.c | 13 +++++++++---- builtin/stash.c | 14 ++++++++++---- diff-lib.c | 5 ++++- 10 files changed, 69 insertions(+), 25 deletions(-) diff --git a/archive.c b/archive.c index 210d7235c5a..003db7d355d 100644 --- a/archive.c +++ b/archive.c @@ -306,8 +306,10 @@ int write_archive_entries(struct archiver_args *args, opts.dst_index = args->repo->index; opts.fn = oneway_merge; init_tree_desc(&t, args->tree->buffer, args->tree->size); - if (unpack_trees(1, &t, &opts)) - return -1; + if (unpack_trees(1, &t, &opts)) { + err = -1; + goto cleanup; + } git_attr_set_direction(GIT_ATTR_INDEX); } @@ -346,8 +348,11 @@ int write_archive_entries(struct archiver_args *args, if (err) break; } + +cleanup: strbuf_release(&path_in_archive); strbuf_release(&content); + unpack_trees_options_release(&opts); return err; } diff --git a/builtin/am.c b/builtin/am.c index 82641ce68ec..4d4bb473c0f 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -1903,6 +1903,7 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset) struct lock_file lock_file = LOCK_INIT; struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree_desc t[2]; + int ret = 0; if (parse_tree(head) || parse_tree(remote)) return -1; @@ -1923,13 +1924,15 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset) if (unpack_trees(2, t, &opts)) { rollback_lock_file(&lock_file); - return -1; + ret = -1; + goto cleanup; } if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) die(_("unable to write new index file")); - - return 0; +cleanup: + unpack_trees_options_release(&opts); + return ret; } /** @@ -1941,6 +1944,7 @@ static int merge_tree(struct tree *tree) struct lock_file lock_file = LOCK_INIT; struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree_desc t[1]; + int ret = 0; if (parse_tree(tree)) return -1; @@ -1956,13 +1960,16 @@ static int merge_tree(struct tree *tree) if (unpack_trees(1, t, &opts)) { rollback_lock_file(&lock_file); - return -1; + ret = -1; + goto cleanup; } if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) die(_("unable to write new index file")); - return 0; +cleanup: + unpack_trees_options_release(&opts); + return ret; } /** diff --git a/builtin/checkout.c b/builtin/checkout.c index 482d17676a0..fd76b504861 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -641,6 +641,7 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o, { struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree_desc tree_desc; + int ret; opts.head_idx = -1; opts.update = worktree; @@ -667,10 +668,14 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o, */ /* fallthrough */ case 0: - return 0; + ret = 0; + break; default: - return 128; + ret = 128; } + + unpack_trees_options_release(&opts); + return ret; } static void setup_branch_path(struct branch_info *branch) diff --git a/builtin/clone.c b/builtin/clone.c index 0df820c5970..df3bb9a7884 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -697,6 +697,7 @@ static int checkout(int submodule_progress) init_tree_desc(&t, tree->buffer, tree->size); if (unpack_trees(1, &t, &opts) < 0) die(_("unable to checkout working tree")); + unpack_trees_options_release(&opts); free(head); diff --git a/builtin/commit.c b/builtin/commit.c index 6cc7313bad8..84c79ecb5a5 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -305,6 +305,7 @@ static void create_base_index(const struct commit *current_head) struct tree *tree; struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; struct tree_desc t; + int exit_early = 0; if (!current_head) { discard_cache(); @@ -324,7 +325,10 @@ static void create_base_index(const struct commit *current_head) parse_tree(tree); init_tree_desc(&t, tree->buffer, tree->size); if (unpack_trees(1, &t, &opts)) - exit(128); /* We've already reported the error, finish dying */ + exit_early = 1; /* We've already reported the error, finish dying */ + unpack_trees_options_release(&opts); + if (exit_early) + exit(128); } static void refresh_cache_or_die(int refresh_flags) diff --git a/builtin/merge.c b/builtin/merge.c index 73290a07fcc..28089e2c5ed 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -671,6 +671,7 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head, struct tree *trees[MAX_UNPACK_TREES]; struct tree_desc t[MAX_UNPACK_TREES]; struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; + int ret = 0; opts.head_idx = 2; opts.src_index = &the_index; @@ -695,8 +696,9 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head, init_tree_desc(t+i, trees[i]->buffer, trees[i]->size); } if (unpack_trees(nr_trees, t, &opts)) - return -1; - return 0; + ret = -1; + unpack_trees_options_release(&opts); + return ret; } static void write_tree_trivial(struct object_id *oid) diff --git a/builtin/read-tree.c b/builtin/read-tree.c index 06f3b97ac05..8f1b8a7e74c 100644 --- a/builtin/read-tree.c +++ b/builtin/read-tree.c @@ -154,6 +154,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix) OPT__QUIET(&opts.quiet, N_("suppress feedback messages")), OPT_END() }; + int ret = 0; opts.head_idx = -1; opts.src_index = &the_index; @@ -243,11 +244,13 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix) parse_tree(tree); init_tree_desc(t+i, tree->buffer, tree->size); } - if (unpack_trees(nr_trees, t, &opts)) - return 128; + if (unpack_trees(nr_trees, t, &opts)) { + ret = 128; + goto cleanup; + } if (opts.debug_unpack || opts.dry_run) - return 0; /* do not write the index out */ + goto cleanup; /* do not write the index out */ /* * When reading only one tree (either the most basic form, @@ -262,5 +265,8 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix) if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) die("unable to write new index file"); - return 0; + +cleanup: + unpack_trees_options_release(&opts); + return ret; } diff --git a/builtin/reset.c b/builtin/reset.c index 86c604b21e9..713d084c3eb 100644 --- a/builtin/reset.c +++ b/builtin/reset.c @@ -78,10 +78,14 @@ static int reset_index(const char *ref, const struct object_id *oid, int reset_t if (reset_type == KEEP) { struct object_id head_oid; - if (get_oid("HEAD", &head_oid)) - return error(_("You do not have a valid HEAD.")); - if (!fill_tree_descriptor(the_repository, desc + nr, &head_oid)) - return error(_("Failed to find tree of HEAD.")); + if (get_oid("HEAD", &head_oid)) { + error(_("You do not have a valid HEAD.")); + goto out; + } + if (!fill_tree_descriptor(the_repository, desc + nr, &head_oid)) { + error(_("Failed to find tree of HEAD.")); + goto out; + } nr++; opts.fn = twoway_merge; } @@ -103,6 +107,7 @@ static int reset_index(const char *ref, const struct object_id *oid, int reset_t ret = 0; out: + unpack_trees_options_release(&opts); for (i = 0; i < nr; i++) free((void *)desc[i].buffer); return ret; diff --git a/builtin/stash.c b/builtin/stash.c index 1137e5fcbe8..be6ecb1ae11 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -237,6 +237,7 @@ static int reset_tree(struct object_id *i_tree, int update, int reset) struct tree_desc t[MAX_UNPACK_TREES]; struct tree *tree; struct lock_file lock_file = LOCK_INIT; + int ret = 0; read_cache_preload(NULL); if (refresh_cache(REFRESH_QUIET)) @@ -258,13 +259,17 @@ static int reset_tree(struct object_id *i_tree, int update, int reset) opts.update = update; opts.fn = oneway_merge; - if (unpack_trees(nr_trees, t, &opts)) - return -1; + if (unpack_trees(nr_trees, t, &opts)) { + ret = -1; + goto cleanup; + } if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) - return error(_("unable to write new index file")); + ret = error(_("unable to write new index file")); - return 0; +cleanup: + unpack_trees_options_release(&opts); + return ret; } static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit) @@ -815,6 +820,7 @@ static void diff_include_untracked(const struct stash_info *info, struct diff_op if (unpack_trees(ARRAY_SIZE(tree_desc), tree_desc, &unpack_tree_opt)) die(_("failed to unpack trees")); + unpack_trees_options_release(&unpack_tree_opt); do_diff_cache(&info->b_commit, diff_opt); } diff --git a/diff-lib.c b/diff-lib.c index 8a08d9af4eb..2d8a90a51b2 100644 --- a/diff-lib.c +++ b/diff-lib.c @@ -527,6 +527,7 @@ static int diff_cache(struct rev_info *revs, struct tree *tree; struct tree_desc t; struct unpack_trees_options opts = UNPACK_TREES_OPTIONS_INIT; + int ret; tree = parse_tree_indirect(tree_oid); if (!tree) @@ -545,7 +546,9 @@ static int diff_cache(struct rev_info *revs, opts.pathspec->recursive = 1; init_tree_desc(&t, tree->buffer, tree->size); - return unpack_trees(1, &t, &opts); + ret = unpack_trees(1, &t, &opts); + unpack_trees_options_release(&opts); + return ret; } void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb)