From patchwork Mon Jan 14 18:34:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 10763295 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D462513B5 for ; Mon, 14 Jan 2019 18:35:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C6FA529632 for ; Mon, 14 Jan 2019 18:35:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BAE7629868; Mon, 14 Jan 2019 18:35:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 684E729632 for ; Mon, 14 Jan 2019 18:35:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726764AbfANSfJ (ORCPT ); Mon, 14 Jan 2019 13:35:09 -0500 Received: from mail-lf1-f68.google.com ([209.85.167.68]:39531 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726708AbfANSfJ (ORCPT ); Mon, 14 Jan 2019 13:35:09 -0500 Received: by mail-lf1-f68.google.com with SMTP id n18so8414lfh.6 for ; Mon, 14 Jan 2019 10:35:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qS3BNiSUt2fZfz0WCH9bVtrLjGAdEPGYhOcH46aAxdo=; b=haqqwqrhsBJzjl2IESdFjqgO7nDnt97WrFbm+gwfiSSypsdLCTQqrrzLJD5RbkY5Cf R567qvEwV7KefDnDPEUJnuOx5tahSLp1lOx/bIITs9boMBVh4NiyX6QJDfWbh644+9B5 PsQ61QnTxDQY++wNrXu5x9QVJwk+ZoGVTeGJpDVIBynTVndrpHTJxq0KYjcYnLg/tFW0 U5cCIIIh61cX+3ke7FqP+0VTlW+hSNO6+wx4Qd9P/qGvUu5o46xZwKv7AkM+EDhDerfO TaT/IQF8GwOk0gMbgOMiWDmAYTESmVPTmkrndcOveaDH9F/iq0f9TBAq+CLozMHrj/no fwHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qS3BNiSUt2fZfz0WCH9bVtrLjGAdEPGYhOcH46aAxdo=; b=olkQR4chUqCsNKleXitcYOblJHB69lKpWq61fLxLzJp/rq+GHgzR1wTa1DdBhWyfRj hB1SNtOjdmgdBuhh15U1e9r9mza8e9jBNktizsj1AuVi1FkrwGFt4P7PJopLzGHja35u GENHiYW0KbFVcgTOqYjdvQbaqy40kiUE9QQYo80wQ7+ECnQAwPIlzSA6Xv9d0F2XJfz8 J5Zqzdh4LQ2wj5WXTtzspcorrm+u/UTLTfTDR3OlKr9EbPX5HGnzYh4Xz3TDIhtge6Xe nLSRohR4j0ABaA5VaGPozktvr1+Rb4+lqBJ3VSDI2sbCZtul9vGKtYz6MUqQEHjiV0jY C5yg== X-Gm-Message-State: AJcUukfIZ4ujMCXMn8RnRgPiyA2mwX7e/fMOLHNYKjYRctiwXBEgykQA 5jwM65zXjrJ/9qd8WBaHocsg1b/b X-Google-Smtp-Source: ALg8bN7LsDYrsRqaTLTK3s2jLK/bB8jHDz8SvvAEorAhrr1kbVuwCb33kOcy8WVQdZ1vLxbi1w3UXw== X-Received: by 2002:a19:7352:: with SMTP id o79mr15329853lfc.104.1547490906601; Mon, 14 Jan 2019 10:35:06 -0800 (PST) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id x24-v6sm224492ljc.54.2019.01.14.10.35.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Jan 2019 10:35:06 -0800 (PST) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Jeff King , "brian m . carlson" Subject: [PATCH v2 1/3] setup: free old value before setting `work_tree` Date: Mon, 14 Jan 2019 19:34:55 +0100 Message-Id: <8585c0bfdc071f17e75346b801a16580d360c18a.1547488709.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: <20181218072528.3870492-1-martin.agren@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Before assigning to `data->work_tree` in `read_worktree_config()`, free any value we might already have picked up, so that we do not leak it. Signed-off-by: Martin Ågren --- setup.c | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.c b/setup.c index 1be5037f12..bb633942bb 100644 --- a/setup.c +++ b/setup.c @@ -411,6 +411,7 @@ static int read_worktree_config(const char *var, const char *value, void *vdata) } else if (strcmp(var, "core.worktree") == 0) { if (!value) return config_error_nonbool(var); + free(data->work_tree); data->work_tree = xstrdup(value); } return 0; From patchwork Mon Jan 14 18:34:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 10763297 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 82F156C2 for ; Mon, 14 Jan 2019 18:35:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 76E7C28960 for ; Mon, 14 Jan 2019 18:35:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6B82229866; Mon, 14 Jan 2019 18:35:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 078B728960 for ; Mon, 14 Jan 2019 18:35:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726795AbfANSfM (ORCPT ); Mon, 14 Jan 2019 13:35:12 -0500 Received: from mail-lj1-f196.google.com ([209.85.208.196]:44914 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726708AbfANSfL (ORCPT ); Mon, 14 Jan 2019 13:35:11 -0500 Received: by mail-lj1-f196.google.com with SMTP id k19-v6so19932225lji.11 for ; Mon, 14 Jan 2019 10:35:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=o0VIYgX1c9jC3smjGNXEBNkVwQPlGBUU5SeAS4NKSa8=; b=MXhOufAFeVlOXBufhPrtR6E6aaMnzQUsHm5SSxjCwIAHV2mQs67Z6UWE1zWTJnQTlx uhjJmwD/kb3aqBLZjgbdTNOEW8K9brlLQMdkZTxiQ0jO1tcBk1fFjN1SrYhQblRxkrbD X8cLa7g/CfAmwN63hPwY8RHdGtw7Iyks7b8c1pUZbCXX6ymUk1gRo6XqCcQDbTK0NkML a0k2sgbW+dRzdlG7IbDCzHObKF/hVVHvHacaKxOao665NErYLMMpbgNlLXkKO2Y3nTMa 4lXOlR7crHFug9aFim80OIAw2mNDboYxKEhZu64ROy6bFrlwkwEMJv2HcjOz/lvopMBK awkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=o0VIYgX1c9jC3smjGNXEBNkVwQPlGBUU5SeAS4NKSa8=; b=WH5h7GpkKtrlCcDnKQZTxrxUV0eXKucJhNvZdP7UAluqT3gsdYwFF75SZVd4y6UBQ9 svmt0hjdLqR/pbYx5ueww+wYg6jnOM+iGQfWWTkOa4W2x7OY44E6qF7WtQAc5hhygCMu uK9OglHs5gWyJJXOmZqsLhRqJhbFTZnkaSvXzUYz9KI57xk9frtTgF/EDwbocIcTH21S PifDEUz2HyzR+rD1CkXYsQGripXC8X8UQCK7tVI0Qnn3BpxCZ1Apkz4Vm7rSbwnJysKw RvqyQHXkObD5VRgdwP5dizwAac7TEdqWAaB3aLDZDnbaMNAoTTOqQVnG3vkz83OKDcI9 TkWA== X-Gm-Message-State: AJcUukcpW0K1ZMeGKQIo9kJwVCrZweQsPOeLucptUiuuXV92PQ5IGfXd PrLtUQfY2HCv6VhtzRY+eW3+7WbP X-Google-Smtp-Source: ALg8bN7GpHTF7obdOJueW4mS0Rs9MJ5GP9QPmS/sBjiKXw6wgODjKyILIeyZM4pDxms4iP4CcArrzA== X-Received: by 2002:a2e:841:: with SMTP id g1-v6mr252354ljd.21.1547490909151; Mon, 14 Jan 2019 10:35:09 -0800 (PST) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id x24-v6sm224492ljc.54.2019.01.14.10.35.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Jan 2019 10:35:08 -0800 (PST) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Jeff King , "brian m . carlson" Subject: [PATCH v2 2/3] setup: do not use invalid `repository_format` Date: Mon, 14 Jan 2019 19:34:56 +0100 Message-Id: X-Mailer: git-send-email 2.20.1 In-Reply-To: References: <20181218072528.3870492-1-martin.agren@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If `read_repository_format()` encounters an error, `format->version` will be -1 and all other fields of `format` will be undefined. However, in `setup_git_directory_gently()`, we use `repo_fmt.hash_algo` regardless of the value of `repo_fmt.version`. This can be observed by adding this to the end of `read_repository_format()`: if (format->version == -1) format->hash_algo = 0; /* no-one should peek at this! */ This causes, e.g., "git branch -m q q2 without config should succeed" in t3200 to fail with "fatal: Failed to resolve HEAD as a valid ref." because it has moved .git/config out of the way and is now trying to use a bad hash algorithm. Check that `version` is non-negative before using `hash_algo`. This patch adds no tests, but do note that if we skip this patch, the next patch would cause existing tests to fail as outlined above. Signed-off-by: Martin Ågren --- setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.c b/setup.c index bb633942bb..4d3d67c50b 100644 --- a/setup.c +++ b/setup.c @@ -1140,7 +1140,7 @@ const char *setup_git_directory_gently(int *nongit_ok) gitdir = DEFAULT_GIT_DIR_ENVIRONMENT; setup_git_env(gitdir); } - if (startup_info->have_repository) + if (startup_info->have_repository && repo_fmt.version > -1) repo_set_hash_algo(the_repository, repo_fmt.hash_algo); } From patchwork Mon Jan 14 18:34:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 10763299 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0BF8313B5 for ; Mon, 14 Jan 2019 18:35:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F32A128960 for ; Mon, 14 Jan 2019 18:35:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E7F0C29866; Mon, 14 Jan 2019 18:35:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3156528960 for ; Mon, 14 Jan 2019 18:35:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726802AbfANSfO (ORCPT ); Mon, 14 Jan 2019 13:35:14 -0500 Received: from mail-lj1-f177.google.com ([209.85.208.177]:37172 "EHLO mail-lj1-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726708AbfANSfN (ORCPT ); Mon, 14 Jan 2019 13:35:13 -0500 Received: by mail-lj1-f177.google.com with SMTP id t18-v6so26045ljd.4 for ; Mon, 14 Jan 2019 10:35:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pWK5I93Byk/rouU7TgtXihxUgScoLQqR9h1DmpnZGLk=; b=tOw5PG4JIVNfTMQDlx6xPN2gvY7nzZB9IIc0u27Oh7Fs9XDVRQbqwwz3EdMCjE+sf9 YdTuTTq7b6tu3neryxwQjAuzyaug6cZapecHDh3itttOyp6+2HT/qxsMBM7tAv7gBE+0 4Fu4jxKPmZRrENVRyFpShsQQbp5how3mvoc6B3E3aIP4cDtc961KBzTOaVKUlIOhjTQ0 faHzNCWHoK7LFeQRNXYBSrqiFXN6ccWavhA49j6hNKGYMjDQp98bZo6oEVpwerhSx0Ns YitGm4H8CWYy9fSg+/+kIAgCQd+1sTSQRjVdZt3bCRig4d3POGZP+T7GinyHXQoiefGR P5fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pWK5I93Byk/rouU7TgtXihxUgScoLQqR9h1DmpnZGLk=; b=h+q7/Ss1JSts1eCetV/1J67//WluyD/f80xhPjHpDlYYklJxYooAbaTXS6b2GOPbHK 1DzRhV1IUAwCoUiYRzZAwbscLg6oDEoVDLOHNz2qosApg5elYKC/lZoArfb8OWrCnjsP dX8luBS24SkFTyrkMjDaE1a9izYPq9JGLeUV+ErP+tvmw8d292FXtDZXz51jLEUMxR2i wfd9jkqO6bn5IiL0dvp0yWs+7vmOGLaAmYcsmjujG4e9ACgVCPMatrmh+dHRuC4dmS5j gJg0k35/wh0qW5Et7AGDwLputN2KZpZX0EDJ/yAHHPqh5uT8v2BgrSsDgfOwd0782lJF 1lLQ== X-Gm-Message-State: AJcUukcGDtm1TEn5fnnI1T4NNPHDo+04Yg7KsR/FY2qWJDpoRir7Wa3f TfHOe+me1qeGTdiNYloxhhss4v/c X-Google-Smtp-Source: ALg8bN4PZ83Fr7h5aHaIW0InlRGJfVr/NzkACel4uUxeAXNUXyRRkASwLdj5epaKFbR2c507vCtSmA== X-Received: by 2002:a2e:9715:: with SMTP id r21-v6mr15212132lji.30.1547490911295; Mon, 14 Jan 2019 10:35:11 -0800 (PST) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id x24-v6sm224492ljc.54.2019.01.14.10.35.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Jan 2019 10:35:10 -0800 (PST) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Jeff King , "brian m . carlson" Subject: [PATCH v2 3/3] setup: add `clear_repository_format()` Date: Mon, 14 Jan 2019 19:34:57 +0100 Message-Id: <7b0ca165bce684bde38b753ab98f7858f20130c0.1547488709.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: <20181218072528.3870492-1-martin.agren@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP After we set up a `struct repository_format`, it owns various pieces of allocated memory. We then either use those members, because we decide we want to use the "candidate" repository format, or we discard the candidate / scratch space. In the first case, we transfer ownership of the memory to a few global variables. In the latter case, we just silently drop the struct and end up leaking memory. Fixing these memory leaks is complicated by two issues: 1) As described above, we "steal" the fields of the struct in case of success. 2) We might end up calling `read_repository_format()` more than once -- as we enter it a second time, we lose track of our pointers and leak memory. As a further complication, we do not initialize (zero) the structs before calling `read_...()` so as we first enter it, we cannot blindly free the pointers in it. To free this memory, in light of (1), we must either carefully cover all error paths but no success paths; or we should stop grabbing the pointers. To address 2), we must either zero the struct before calling `read_repository_format()`, or try to keep track of when we should zero it and when we should first free the memory. Introduce `REPO_FORMAT_INIT` and `clear_repository_format()` to be used on each side of `read_repository_format()`. Make the users duplicate the memory from the structs, rather than copying the pointers. Call `clear_...()` at the start of `read_...()` instead of just zeroing the struct. In the error path of the same function, be sure to restore the error sentinel after we clear it with the rest of the struct. Signed-off-by: Martin Ågren --- I do wonder if introducing and depending on `REPOSITORY_FORMAT_INIT` like this is 100% sane. Out-of-tree users could be in for a nasty surprise. :-/ cache.h | 12 ++++++++++++ builtin/init-db.c | 3 ++- repository.c | 3 ++- setup.c | 30 ++++++++++++++++++++++-------- worktree.c | 4 +++- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/cache.h b/cache.h index ca36b44ee0..3ef63d27c4 100644 --- a/cache.h +++ b/cache.h @@ -972,6 +972,12 @@ struct repository_format { struct string_list unknown_extensions; }; +/** + * Always use this to initialize a `struct repository_format` + * to a well-defined state before calling `read_repository()`. + */ +#define REPOSITORY_FORMAT_INIT { 0 } + /* * Read the repository format characteristics from the config file "path" into * "format" struct. Returns the numeric version. On error, -1 is returned, @@ -980,6 +986,12 @@ struct repository_format { */ int read_repository_format(struct repository_format *format, const char *path); +/* + * Free the memory held onto by `format`, but not the struct itself. + * (No need to use this after `read_repository_format()` fails.) + */ +void clear_repository_format(struct repository_format *format); + /* * Verify that the repository described by repository_format is something we * can read. If it is, return 0. Otherwise, return -1, and "err" will describe diff --git a/builtin/init-db.c b/builtin/init-db.c index 41faffd28d..04c60eaad5 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -96,7 +96,7 @@ static void copy_templates(const char *template_dir) struct strbuf path = STRBUF_INIT; struct strbuf template_path = STRBUF_INIT; size_t template_len; - struct repository_format template_format; + struct repository_format template_format = REPOSITORY_FORMAT_INIT; struct strbuf err = STRBUF_INIT; DIR *dir; char *to_free = NULL; @@ -148,6 +148,7 @@ static void copy_templates(const char *template_dir) free(to_free); strbuf_release(&path); strbuf_release(&template_path); + clear_repository_format(&template_format); } static int git_init_db_config(const char *k, const char *v, void *cb) diff --git a/repository.c b/repository.c index 7b02e1dffa..df88705574 100644 --- a/repository.c +++ b/repository.c @@ -148,7 +148,7 @@ int repo_init(struct repository *repo, const char *gitdir, const char *worktree) { - struct repository_format format; + struct repository_format format = REPOSITORY_FORMAT_INIT; memset(repo, 0, sizeof(*repo)); repo->objects = raw_object_store_new(); @@ -165,6 +165,7 @@ int repo_init(struct repository *repo, if (worktree) repo_set_worktree(repo, worktree); + clear_repository_format(&format); return 0; error: diff --git a/setup.c b/setup.c index 4d3d67c50b..70d9007ae5 100644 --- a/setup.c +++ b/setup.c @@ -477,7 +477,7 @@ static int check_repository_format_gently(const char *gitdir, struct repository_ } repository_format_precious_objects = candidate->precious_objects; - repository_format_partial_clone = candidate->partial_clone; + repository_format_partial_clone = xstrdup_or_null(candidate->partial_clone); repository_format_worktree_config = candidate->worktree_config; string_list_clear(&candidate->unknown_extensions, 0); @@ -500,11 +500,9 @@ static int check_repository_format_gently(const char *gitdir, struct repository_ } if (candidate->work_tree) { free(git_work_tree_cfg); - git_work_tree_cfg = candidate->work_tree; + git_work_tree_cfg = xstrdup(candidate->work_tree); inside_work_tree = -1; } - } else { - free(candidate->work_tree); } return 0; @@ -512,15 +510,27 @@ static int check_repository_format_gently(const char *gitdir, struct repository_ int read_repository_format(struct repository_format *format, const char *path) { - memset(format, 0, sizeof(*format)); + clear_repository_format(format); format->version = -1; format->is_bare = -1; format->hash_algo = GIT_HASH_SHA1; string_list_init(&format->unknown_extensions, 1); git_config_from_file(check_repo_format, path, format); + if (format->version == -1) { + clear_repository_format(format); + format->version = -1; + } return format->version; } +void clear_repository_format(struct repository_format *format) +{ + string_list_clear(&format->unknown_extensions, 0); + free(format->work_tree); + free(format->partial_clone); + memset(format, 0, sizeof(*format)); +} + int verify_repository_format(const struct repository_format *format, struct strbuf *err) { @@ -1008,7 +1018,7 @@ int discover_git_directory(struct strbuf *commondir, struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT; size_t gitdir_offset = gitdir->len, cwd_len; size_t commondir_offset = commondir->len; - struct repository_format candidate; + struct repository_format candidate = REPOSITORY_FORMAT_INIT; if (strbuf_getcwd(&dir)) return -1; @@ -1045,9 +1055,11 @@ int discover_git_directory(struct strbuf *commondir, strbuf_release(&err); strbuf_setlen(commondir, commondir_offset); strbuf_setlen(gitdir, gitdir_offset); + clear_repository_format(&candidate); return -1; } + clear_repository_format(&candidate); return 0; } @@ -1056,7 +1068,7 @@ const char *setup_git_directory_gently(int *nongit_ok) static struct strbuf cwd = STRBUF_INIT; struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT; const char *prefix; - struct repository_format repo_fmt; + struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; /* * We may have read an incomplete configuration before @@ -1146,6 +1158,7 @@ const char *setup_git_directory_gently(int *nongit_ok) strbuf_release(&dir); strbuf_release(&gitdir); + clear_repository_format(&repo_fmt); return prefix; } @@ -1203,9 +1216,10 @@ int git_config_perm(const char *var, const char *value) void check_repository_format(void) { - struct repository_format repo_fmt; + struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; check_repository_format_gently(get_git_dir(), &repo_fmt, NULL); startup_info->have_repository = 1; + clear_repository_format(&repo_fmt); } /* diff --git a/worktree.c b/worktree.c index d6a0ee7f73..b45bfeb9d3 100644 --- a/worktree.c +++ b/worktree.c @@ -444,7 +444,7 @@ int submodule_uses_worktrees(const char *path) DIR *dir; struct dirent *d; int ret = 0; - struct repository_format format; + struct repository_format format = REPOSITORY_FORMAT_INIT; submodule_gitdir = git_pathdup_submodule(path, "%s", ""); if (!submodule_gitdir) @@ -462,8 +462,10 @@ int submodule_uses_worktrees(const char *path) read_repository_format(&format, sb.buf); if (format.version != 0) { strbuf_release(&sb); + clear_repository_format(&format); return 1; } + clear_repository_format(&format); /* Replace config by worktrees. */ strbuf_setlen(&sb, sb.len - strlen("config"));