From patchwork Tue Dec 18 07:25:26 2018 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: 10734947 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 709B014E2 for ; Tue, 18 Dec 2018 07:25:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 586392A3EA for ; Tue, 18 Dec 2018 07:25:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4C84B2A3F4; Tue, 18 Dec 2018 07:25:59 +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 E5A002A3EA for ; Tue, 18 Dec 2018 07:25:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726397AbeLRHZx (ORCPT ); Tue, 18 Dec 2018 02:25:53 -0500 Received: from mail-lj1-f194.google.com ([209.85.208.194]:45440 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726316AbeLRHZx (ORCPT ); Tue, 18 Dec 2018 02:25:53 -0500 Received: by mail-lj1-f194.google.com with SMTP id s5-v6so13256999ljd.12 for ; Mon, 17 Dec 2018 23:25:51 -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=3dHhS5L56ogl2LVxjQH3RHOMZnyWybCL1qQEDbZ/Zts=; b=clm7j9Tt5GJIJYwzbjKFozNxJLr0x04eqv8dUX8HKlbeHH7WC63nHubPRhPAJ56M/s rSTnBlwfAeSynTFomggzTRBNA+wPBy9UIVf6YaoyG8q26nZRcRbdwFTxvLphumL/Wl2w s+TxE6MtBiiqvu3UPs6GhiaCoRfzn3LeDyFfbRCgOIIHOOYp3sjdi6JL7mBIHZhSVRJ1 F50gEnEPWwUixrjoFbAbR29jZmnXBh4NEYWh/YyUf3vrvIWXLRu57LK3dbqIRcp0RBp6 q2Pv8W7wn1JBaIuPsY8vgF9NwnCt5BX/vMRVijfgKsvUA72OhG33c22oSr8cA+PgmLbL zrgg== 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=3dHhS5L56ogl2LVxjQH3RHOMZnyWybCL1qQEDbZ/Zts=; b=QXiJDXh4NFWpEKJMa7xTN+2JrqYMWgcKKi23pfYrvOubddyO0DQYx73zIUh9q0XeoN 69Rc4K1k3awhussGOvFX/FW8Hq0CQ5ELidzlYLTW5VQgfd7ejZlJZhEYPKuZKVgD2kv8 yQpLmp5karJQ6uExk9ddqEqwAGRyWi7PHKI8etsw8wKXqdw7qJrXxiSTjzRZnN5D/dCh OPa0sIAy4DuDzWwepJmRk618oUplUAPNfBQMFek+PDhSiJF/zp7TlTNFdE6x6uZlOQP1 hTrfjsjL4QCyE97k56nruk708Q15sf9E8IS/gPdYxDSyvy+iNKu3KoPSrAk+RCo3JaUI yJZA== X-Gm-Message-State: AA+aEWaLiQ70/4CTNH/NOszNQZBONIurtWP04BwgE1I+z/+Ca6bZ7jo5 PXr9QT0EBp3+jQ+q/+uZZjz+DBL/ X-Google-Smtp-Source: AFSGD/VrfOVJjax1F+q6dzcwltDpT7uBTdGp90WAGKMqVrZADyuCb6crebGCnt7kZ/LVnyFjCYBGdA== X-Received: by 2002:a2e:b04f:: with SMTP id d15-v6mr10418318ljl.3.1545117950478; Mon, 17 Dec 2018 23:25:50 -0800 (PST) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id f11sm3142441lfi.12.2018.12.17.23.25.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Dec 2018 23:25:49 -0800 (PST) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Jeff King , "brian m . carlson" Subject: [PATCH 1/3] setup: drop return value from `read_repository_format()` Date: Tue, 18 Dec 2018 08:25:26 +0100 Message-Id: <20181218072528.3870492-2-martin.agren@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181218072528.3870492-1-martin.agren@gmail.com> 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 No-one looks at the return value, so we might as well drop it. It's still available as `format->version`. In v1 of what became commit 2cc7c2c737 ("setup: refactor repo format reading and verification", 2016-03-11), this function actually had return type "void", but that was changed in v2. Almost three years later, no-one has used this return value. Signed-off-by: Martin Ågren --- I only discovered the full history after writing the patch. Had I known it from the beginning, maybe I'd have just skipped this step, but I was sufficiently disturbed by the redundant and unused return value that I dropped it before working on the actual meat of this series. cache.h | 7 +++---- setup.c | 3 +-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/cache.h b/cache.h index ca36b44ee0..8b9e592c65 100644 --- a/cache.h +++ b/cache.h @@ -974,11 +974,10 @@ struct repository_format { /* * Read the repository format characteristics from the config file "path" into - * "format" struct. Returns the numeric version. On error, -1 is returned, - * format->version is set to -1, and all other fields in the struct are - * undefined. + * "format" struct. On error, format->version is set to -1, and all other + * fields in the struct are undefined. */ -int read_repository_format(struct repository_format *format, const char *path); +void read_repository_format(struct repository_format *format, const char *path); /* * Verify that the repository described by repository_format is something we diff --git a/setup.c b/setup.c index 1be5037f12..27747af7a3 100644 --- a/setup.c +++ b/setup.c @@ -509,7 +509,7 @@ static int check_repository_format_gently(const char *gitdir, struct repository_ return 0; } -int read_repository_format(struct repository_format *format, const char *path) +void read_repository_format(struct repository_format *format, const char *path) { memset(format, 0, sizeof(*format)); format->version = -1; @@ -517,7 +517,6 @@ int read_repository_format(struct repository_format *format, const char *path) format->hash_algo = GIT_HASH_SHA1; string_list_init(&format->unknown_extensions, 1); git_config_from_file(check_repo_format, path, format); - return format->version; } int verify_repository_format(const struct repository_format *format, From patchwork Tue Dec 18 07:25:27 2018 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: 10734949 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 D873C13BF for ; Tue, 18 Dec 2018 07:26:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BFA4E2A3EA for ; Tue, 18 Dec 2018 07:26:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B3B272A3F4; Tue, 18 Dec 2018 07:26:03 +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 5C85C2A3EA for ; Tue, 18 Dec 2018 07:26:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726409AbeLRH0A (ORCPT ); Tue, 18 Dec 2018 02:26:00 -0500 Received: from mail-lj1-f176.google.com ([209.85.208.176]:37271 "EHLO mail-lj1-f176.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726316AbeLRH0A (ORCPT ); Tue, 18 Dec 2018 02:26:00 -0500 Received: by mail-lj1-f176.google.com with SMTP id t18-v6so1272859ljd.4 for ; Mon, 17 Dec 2018 23:25:59 -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=Wwr9gD7/3GMH8hKiwWfqoGyLKYAmhCZNtWrQ7jYakms=; b=Uu1piUxbgUu3gMm9PxIPVqIEa1ilSqpmugAAiCCW2JTKMEtMVAimHYS34DYdxgFEky Pxky5wTJc8fG84up4t10oQHNmMk0jQcqG2KN37PLj50+IKWCl17fdoxhkdDU4nh3AzqZ YGiBNKkkjd9F+mFChKGagvMIuL7pUao1uoN4dZO/h7J2sKdIWSRQPOsbQlcMayhdV2hC jU6iHJBW0zeoEcgY/d2wRtZ/2bOLIl5LOG2ExSxgajv5r+gWI3gEYSnyb18PU8sJn/+G nvcaq7naSagYgD2RVOSmI2gFyfEDzY4DJqsEzB1Log1UrihOH9k+v7ZOwFdcpzTe9pwR nUyw== 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=Wwr9gD7/3GMH8hKiwWfqoGyLKYAmhCZNtWrQ7jYakms=; b=XeBtuizHFzzg8uH8U5b37k6TmXEemD2rAV8TBLNyigybx8fVC4rdOMz4a5pPiCIQN/ TCNcq/WtmLob6Ta1eNU/AYbBtYP3hYQ5UV6r7mwQUo5qaEqD6vVzUylwoBHbxtkxZ6CH TJzIKDZxWBOdotglzP6zOBgdmi6tBXJIEYDR7RDlZ9IokXuC93R5nT9FjOJ9G4ceLvO3 I8Bjc5ReD0sH5WGWbRmq4joF+WXj5hyBYWaLQdGGosn+qbYBhq5/CVAlt91zq9wJRdT4 XokJ602sccQyDi4oEAD29IsWiiXW7gmvmf8EmbDzxIi1PzL8obddI8DAdH9rhkfBNv48 LcQg== X-Gm-Message-State: AA+aEWbQrKlbCT/YZf/RX7Iw68g+urzhgpjabtNHpJDP27B2G+1wji0V 3Zi+HG9jQeFx6rHpRc4sPxFEb026 X-Google-Smtp-Source: AFSGD/Xzdr1NZ7DQz/4yKdF29ffSrg2A08qnZp2Q+EOwQax2VneB2ObMJxP7xPCBJhpaYXKqfZXTSw== X-Received: by 2002:a2e:e02:: with SMTP id 2-v6mr7979893ljo.10.1545117958018; Mon, 17 Dec 2018 23:25:58 -0800 (PST) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id f11sm3142441lfi.12.2018.12.17.23.25.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Dec 2018 23:25:57 -0800 (PST) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Jeff King , "brian m . carlson" Subject: [PATCH 2/3] setup: do not use invalid `repository_format` Date: Tue, 18 Dec 2018 08:25:27 +0100 Message-Id: <20181218072528.3870492-3-martin.agren@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181218072528.3870492-1-martin.agren@gmail.com> 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 --- I fully admit to not understanding all of this setup code, neither in its current incarnation, nor in terms of an ideal end game. This check seems like a good thing to do though. setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.c b/setup.c index 27747af7a3..52c3c9d31f 100644 --- a/setup.c +++ b/setup.c @@ -1138,7 +1138,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 Tue Dec 18 07:25:28 2018 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: 10734951 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 C406014E2 for ; Tue, 18 Dec 2018 07:26:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AC60C2A3EA for ; Tue, 18 Dec 2018 07:26:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9FC222A3F4; Tue, 18 Dec 2018 07:26:08 +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 286B62A3EA for ; Tue, 18 Dec 2018 07:26:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726357AbeLRH0G (ORCPT ); Tue, 18 Dec 2018 02:26:06 -0500 Received: from mail-lj1-f196.google.com ([209.85.208.196]:33955 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726316AbeLRH0F (ORCPT ); Tue, 18 Dec 2018 02:26:05 -0500 Received: by mail-lj1-f196.google.com with SMTP id u89-v6so2588027lje.1 for ; Mon, 17 Dec 2018 23:26:04 -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=UU/np2Zlc8d9b9YQNH6kCuGloWL7zkbWUMVwJoMfab4=; b=CNfQVzUaRepdyoe9tR/Kt/FLjMZmZCpbYGxR5Zdd2uIl+1O8sVdxvg99/q5yIGf+3e a13oJTfSwngJjUbZv7TADxqqPHJe9yWfHS4p0k/6MIW/24/VidACzf8Z04e/3NeuOMcL 5QChlp+S4YVLtxGfUT/UKI5ddqpiiNx3AXf1u+8UQwzXe8cZTfkeTYUMj4iLlPIcCZwV YF5EBzJ+mBoAUiuN6RgbI2fHjaVznXmd69fKFtE26qRXIAsf0Y11zTYhoua5Ib0dnH6o ZOvBoKZJAX/saKXBnbsV6CVE2TkkZ70H2frVOAO+rtRQzV4B7CEo8BQDVwHebGjnYfnY 60Bw== 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=UU/np2Zlc8d9b9YQNH6kCuGloWL7zkbWUMVwJoMfab4=; b=Xlcu2f9oytUQPR5klgP4+Z9BBo31uestrERY6ucSWnhcGkOXV10L18XUrNX+GtiYEg 00tiJ1vSFBxscj+NSyH1hCyiqd2g6Mz8wztcIjFOEQhts66GKgLFylfLOpYeSL9jBNWV zQWEcOCWI6xA/SwnibMTogF8en2odqcZehDCbDSduYDEiTHyxgq2Uq8TGAsCU+Qsjxz3 eWoF6Ep99TLT8O5h0Dx7MOFYNtnS9v1Pk5Q2Oi1kZiUSzQsvSZBzxSiCDcJ8jbx9LHEw iJuzfI0GAPY/DvPJ+mkgq9p8v6wvuWX28aqT3aOzMSh8DvfBHuQ08bVHaXZHIo5diwPu V/vw== X-Gm-Message-State: AA+aEWZGbayJItRkKPl3ey8PkVTojBcD09kF5y4q6lFBqIl0+j3lclEm 64y35xOXWoJtBQnvbV6mkHHNASiH X-Google-Smtp-Source: AFSGD/WCKsMgLj5EEgZXmfuCdtJGT8U+Q99Pc/fr0gHCEs6DEOmuoWxfIIcQhPugmDR5nTYGE4roMA== X-Received: by 2002:a2e:4601:: with SMTP id t1-v6mr10264602lja.111.1545117962975; Mon, 17 Dec 2018 23:26:02 -0800 (PST) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id f11sm3142441lfi.12.2018.12.17.23.26.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Dec 2018 23:26:02 -0800 (PST) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Jeff King , "brian m . carlson" Subject: [PATCH 3/3] setup: add `clear_repository_format()` Date: Tue, 18 Dec 2018 08:25:28 +0100 Message-Id: <20181218072528.3870492-4-martin.agren@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181218072528.3870492-1-martin.agren@gmail.com> 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. Introduce a function `clear_repository_format()` which frees the memory the struct holds on to. Call it in the code paths where we currently leak the memory. Also call it in the error path of `read_repository_format()` to clean up any partial result. For hygiene, we need to at least set the pointers that we free to NULL. For future-proofing, let's zero the entire struct instead. It just means that in the error path of `read_...()` we need to restore the error sentinel in the `version` field. We could take this opportunity to stop claiming that all fields except `version` are undefined in case of an error. On the other hand, having them defined as zero is not much better than having them undefined. We could define them to some fallback configuration (`is_bare = -1` and `hash_algo = GIT_HASH_SHA1`?), but "clear()" and/or "read()" seem like the wrong places to enforce fallback configurations. Let's leave things as "undefined" instead to encourage users to check `version`. Signed-off-by: Martin Ågren --- The error state can always be defined later. Defining it now, then trying to backpedal, is probably not so fun. Filling the struct with non-zero values might help flush out bugs like the one fixed in the previous patch, but I'm wary of going that far in this patch. cache.h | 6 ++++++ repository.c | 1 + setup.c | 14 ++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/cache.h b/cache.h index 8b9e592c65..53ac01efa7 100644 --- a/cache.h +++ b/cache.h @@ -979,6 +979,12 @@ struct repository_format { */ void 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/repository.c b/repository.c index 5dd1486718..efa9d1d960 100644 --- a/repository.c +++ b/repository.c @@ -159,6 +159,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 52c3c9d31f..babe5ea156 100644 --- a/setup.c +++ b/setup.c @@ -517,6 +517,18 @@ void read_repository_format(struct repository_format *format, const char *path) 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; + } +} + +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, @@ -1043,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; }