From patchwork Wed Aug 31 23:14:08 2022 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: 12961504 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90C2FECAAD1 for ; Wed, 31 Aug 2022 23:15:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232624AbiHaXO5 (ORCPT ); Wed, 31 Aug 2022 19:14:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232427AbiHaXOy (ORCPT ); Wed, 31 Aug 2022 19:14:54 -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 4D6CC26A for ; Wed, 31 Aug 2022 16:14:45 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id ay39-20020a05600c1e2700b003a5503a80cfso429475wmb.2 for ; Wed, 31 Aug 2022 16:14:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=bjHn49QNWUcCrQDZ1Nyq7ulMnZPyRUd+CXbc93Hw/cA=; b=OzNCKwYL/Leb4Yekvx8sJBpJ2XYY+FNgubFyECahHHoPlIrGzK6pWrxeJtIhkFgc6A +tt8uuiRJH0eGbgX+WdryBFQuEr/q0Xt/TQJ/tU7w8oeVplXRzSz4/+bW3aouI39c7WA uQn677Pf0N450CC9QhdYWLDvCmkd1Bahms65mevi2TwRe0v5RcoWJiQX6zuK9gbEETaB zQL5KV4AnTAZokXpFnJ/Yi5vmBvil3hKAs2LItGmVHty8bniv0B5Xt6nvakAojiFN5ly NdnaCVaJpwnFXSuriqpSdyzD+UUOkNEMDrzpZQpj5I5WSQRspEZ08zpMDFVb2cB8XNqi vcaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=bjHn49QNWUcCrQDZ1Nyq7ulMnZPyRUd+CXbc93Hw/cA=; b=tWAAFzpFrvyAUERT4WhbfRBWWQen/r48Z8h978OyJI+6AuSs0xB7jZlirgKI8tAa+U vYF85yhCPwovhqrwUin+s9ZTw2U2xhNFntN+FEZh9xLVZoZofGaTgdvn/8XJYmJ8t7zO X8Fp6ITCOHYTLb39LwYujuFnaS4I+RxPuDByyUfkwvLtaBL78XkKkIHuJmxXNB00Jasx BqxMQmUr8fYJ/GGNwrK3JTBCSu5ip8J08ZZ1xUnEBUc/cz9FbiI29YbhrR2tfkJkpB0I Ohv+QicDkxTTaZVZihPUwE/pP3kpBLKOGJ4D6N/6HCyP/gjROByl8uFROcqwuC6nncbf MxTQ== X-Gm-Message-State: ACgBeo02HG72zOYn1y47HerGeWUDte+2vQ0luomMWAejKxuTRmgDWxIo Wav3nrp2J0aFKvTYiwFSxIQ4zHnSgp3+MA== X-Google-Smtp-Source: AA6agR57EPnW2tR+HRX2UNmTLMyaoEg5wTu24YsWYaRYJo6RRXwQMVrg8JSIQUWMi2LDgk9/MJB8Qw== X-Received: by 2002:a7b:cb95:0:b0:3a6:166:6e23 with SMTP id m21-20020a7bcb95000000b003a601666e23mr3289333wmi.15.1661987683454; Wed, 31 Aug 2022 16:14:43 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:42 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 01/17] submodule--helper: fix a leak in "clone_submodule" Date: Thu, 1 Sep 2022 01:14:08 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a memory leak of the "clone_data_path" variable that we copy or derive from the "struct module_clone_data" in clone_submodule(). This code was refactored in preceding commits, but the leak has been with us since f8eaa0ba98b (submodule--helper, module_clone: always operate on absolute paths, 2016-03-31). For the "else" case we don't need to xstrdup() the "clone_data->path", and we don't need to free our own "clone_data_path". We can therefore assign the "clone_data->path" to our own "clone_data_path" right away, and only override it (and remember to free it!) if we need to xstrfmt() a replacement. In the case of the module_clone() caller it's from "argv", and doesn't need to be free'd, and in the case of the add_submodule() caller we get a pointer to "sm_path", which doesn't need to be directly free'd either. Fixing this leak makes several tests pass, so let's mark them as passing with TEST_PASSES_SANITIZE_LEAK=true. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 10 +++++----- t/t1500-rev-parse.sh | 1 + t/t6008-rev-list-submodule.sh | 1 + t/t7414-submodule-mistakes.sh | 2 ++ t/t7506-status-submodule.sh | 1 + t/t7507-commit-verbose.sh | 2 ++ 6 files changed, 12 insertions(+), 5 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index c4149f11e7f..2e042c8a043 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -1590,13 +1590,12 @@ static int clone_submodule(const struct module_clone_data *clone_data, char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name); char *sm_alternate = NULL, *error_strategy = NULL; struct child_process cp = CHILD_PROCESS_INIT; - const char *clone_data_path; + const char *clone_data_path = clone_data->path; + char *to_free = NULL; if (!is_absolute_path(clone_data->path)) - clone_data_path = xstrfmt("%s/%s", get_git_work_tree(), - clone_data->path); - else - clone_data_path = xstrdup(clone_data->path); + clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(), + clone_data->path); if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0) die(_("refusing to create/use '%s' in another submodule's " @@ -1681,6 +1680,7 @@ static int clone_submodule(const struct module_clone_data *clone_data, free(sm_gitdir); free(p); + free(to_free); return 0; } diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh index 1c2df08333b..0e13bcb4ebb 100755 --- a/t/t1500-rev-parse.sh +++ b/t/t1500-rev-parse.sh @@ -4,6 +4,7 @@ test_description='test git rev-parse' GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh test_one () { diff --git a/t/t6008-rev-list-submodule.sh b/t/t6008-rev-list-submodule.sh index 3153a0d8910..12e67e187ef 100755 --- a/t/t6008-rev-list-submodule.sh +++ b/t/t6008-rev-list-submodule.sh @@ -8,6 +8,7 @@ test_description='git rev-list involving submodules that this repo has' GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh test_expect_success 'setup' ' diff --git a/t/t7414-submodule-mistakes.sh b/t/t7414-submodule-mistakes.sh index f2e7df59cf2..3269298197c 100755 --- a/t/t7414-submodule-mistakes.sh +++ b/t/t7414-submodule-mistakes.sh @@ -1,6 +1,8 @@ #!/bin/sh test_description='handling of common mistakes people may make with submodules' + +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh test_expect_success 'create embedded repository' ' diff --git a/t/t7506-status-submodule.sh b/t/t7506-status-submodule.sh index 3fcb44767f5..f5426a8e589 100755 --- a/t/t7506-status-submodule.sh +++ b/t/t7506-status-submodule.sh @@ -2,6 +2,7 @@ test_description='git status for submodule' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh test_create_repo_with_commit () { diff --git a/t/t7507-commit-verbose.sh b/t/t7507-commit-verbose.sh index ed2653d46fe..92462a22374 100755 --- a/t/t7507-commit-verbose.sh +++ b/t/t7507-commit-verbose.sh @@ -1,6 +1,8 @@ #!/bin/sh test_description='verbose commit template' + +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh write_script "check-for-diff" <<\EOF && From patchwork Wed Aug 31 23:14:09 2022 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: 12961506 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E3231ECAAD4 for ; Wed, 31 Aug 2022 23:15:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232664AbiHaXPM (ORCPT ); Wed, 31 Aug 2022 19:15:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232562AbiHaXOy (ORCPT ); Wed, 31 Aug 2022 19:14:54 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56EBFBF8 for ; Wed, 31 Aug 2022 16:14:46 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id b16so12501362wru.7 for ; Wed, 31 Aug 2022 16:14:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=o0gld5eRk48ZnzQWDrnNIcWWdmLkaozT9ViqvnlbBqM=; b=hQN1wMO/rzNt07rMepX4fbAm+C1eCqMRztLt4dX8YEi/zWhoc8FWxm8+ZlGXkFrY27 vVmQIjEErc61wy2vo0fbir2H+pbCYuqVxL5Z5+S5o4YpijELhyMrr5E74GAu5XvULd1v hZBhuMorW0+12IZ2m1ByyCZy8gBDU6FOMt0CIl54X6LzRIchKx3g1E7grEHUh2OK+YFl Lby0T2oSENvXjqbmMx4hiCqHGd/lRZ7whKkoJpl6+tjz7/tj1jmpY44kcz2z3NFuOovL Vq153615Oe4YyTCLMCOMAvR/DVkzwsTzjAS/CEkS76HxgIQ2tCaXzfJkm2EvVOaXBYSS OwWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=o0gld5eRk48ZnzQWDrnNIcWWdmLkaozT9ViqvnlbBqM=; b=RnEfxu80S/ILDs2O9WE9EkykiVsie/AIFc9/Yec+gznhCaSfHa3iWBtGwW8iYKai4w kqcijLs+n6AiFdsFuMztJPD7teOi7JHkBkZtJQwnmIPGNuFCHscPT3Wsg5XIq45yyUXz cmF+rduVE58st2qDhQREaPjP4+BQRCNbbaRUneP932hxSVfLaMFTvhTyTNd6p2NB4QYN ZliP+1Q926J1aOEheAdsOFx9MP7NApgeJxzV0s24wHMhEvyDqbyrvEWarWSQnKuDJWKc adqia7dp1lLRC/W/ivm8N1Vwwc3KF63td/lpTnJSltBhW1TUqN3XqP5aiplzaHq3j1an TG2g== X-Gm-Message-State: ACgBeo1ztR7dh83lIjk5sczgltj+RKuD9VIhQhjbsrtzaGEk7OBj6asR yt8SWEFSa3bcnQUC0pk/ALeKiBVthxj/qg== X-Google-Smtp-Source: AA6agR7CLD6TN+/grnKvcPJkiAwtakdKleEvx4rxS6DnE0cuBIVKii861dC5saRb/OEja9iuatmkDA== X-Received: by 2002:a5d:5272:0:b0:226:eedb:ee59 with SMTP id l18-20020a5d5272000000b00226eedbee59mr2166510wrc.668.1661987684550; Wed, 31 Aug 2022 16:14:44 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:43 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 02/17] submodule--helper: fix trivial get_default_remote_submodule() leak Date: Thu, 1 Sep 2022 01:14:09 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a leak in code added in 1012a5cbc3f (submodule--helper run-update-procedure: learn --remote, 2022-03-04), we need to free() the xstrdup()'d string. This gets e.g. t/t7419-submodule-set-branch.sh closer to passing under SANITIZE=leak. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 2e042c8a043..b51a5074b42 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2451,6 +2451,8 @@ static int update_submodule(struct update_data *update_data) return code; remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch); + free(remote_name); + if (!update_data->nofetch) { if (fetch_in_submodule(update_data->sm_path, update_data->depth, 0, NULL)) From patchwork Wed Aug 31 23:14:10 2022 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: 12961508 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C0A0CECAAD4 for ; Wed, 31 Aug 2022 23:15:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232688AbiHaXPP (ORCPT ); Wed, 31 Aug 2022 19:15:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232569AbiHaXOz (ORCPT ); Wed, 31 Aug 2022 19:14:55 -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 521A3DA9 for ; Wed, 31 Aug 2022 16:14:47 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id bd26-20020a05600c1f1a00b003a5e82a6474so422745wmb.4 for ; Wed, 31 Aug 2022 16:14:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=Wh6AkCo5h0AC6stoz4WJGwnkr0B8J3jS899jv7xtxho=; b=lSBw6CppDwn3576+c5lGCrKeXH4VcytTK6tHOZi1pA/LVGANzLmv+p0BvQgUtL3zuT 2KXiWFifbVYlzFf8iPokT5LkRgq6YJhgj5l9xXYGaskBS550FHpT40T0OCUHXuat1pVA m+VPhYqASftjK1Fy4b1UzJL4g5z7O2OvyhXJJ1nlTqLPidqPHjrd+GnkyOKbFvl09NYU oE2LXCxOItVA7kCtAO2Tdn8Esc8B8Hz3uryl70sjeGwxUG1pGAelztj0SJWOsr5OABt8 wbRqZ8O4FLbRnMnhK4a2KMcErlQ4kIxOlowYFUv02+yuChNFK9Gqw0pnfOeQUp7lE3P1 qb+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=Wh6AkCo5h0AC6stoz4WJGwnkr0B8J3jS899jv7xtxho=; b=izoZtm0X+WKMP9cXYXy9YUsqMLanZM1Z74GBSZs/Rn3zmKZf9d6iK7LPMtPe3a8bA/ yzvSNvG5US7+V82ii184+Leo3ffeoMWNfyNBNCm3UyZflkDOqsDh6DLh7FQw4B4fZ7IQ gw81ZPo4SHzKX7piA0APtCnyv4pNyeJ+/NC9vdOvjGrq8et/RrkSZ7zShJSZai53BCe7 peqDU7MrmUOGKMW4jzCRfN8fz4HG0YrP29cGNmFGdWxv5YysIN7Lnf1kWlvToHoJCPfp svhb07pkXrXVy+ieaHDjDAW07+I9kmYFp73cBWEKn8Lb/nDXEv3y8UGiW9hxECdapNm/ JRwg== X-Gm-Message-State: ACgBeo2VxPJbkNQLhzIzcRFxeDAFkHnAkK9g7KHgPDmhvXyQdhMEtd1u jaa3f4UGp/Pe24s2a47rpjtpurFl6yignA== X-Google-Smtp-Source: AA6agR7spp4bu6WHeHtzlWQEArcR6m793O5OSMkLn3nmgTUisy5hFkkIGHU4nsNIJooZGApyZ6N3AQ== X-Received: by 2002:a05:600c:6025:b0:3a6:ff6:de7e with SMTP id az37-20020a05600c602500b003a60ff6de7emr3258830wmb.173.1661987685831; Wed, 31 Aug 2022 16:14:45 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:44 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 03/17] submodule--helper: fix most "struct pathspec" memory leaks Date: Thu, 1 Sep 2022 01:14:10 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Call clear_pathspec() at the end of various functions that work with and allocate a "struct pathspec". In some cases the zero-initialization here isn't strictly needed, but as we're moving to a "goto cleanup" pattern let's make sure that it's safe to call clear_pathspec(), we don't want the data to be uninitialized. E.g. for module_foreach() we can see from looking at module_list_compute() that if it returns non-zero that the "pathspec" will always have been initialized. But relying on that both assumes knowledge about parse_pathspec(), and would set up a fragile pattern going forward. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 74 +++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index b51a5074b42..2f8603a6bb5 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -379,7 +379,7 @@ static void runcommand_in_submodule_cb(const struct cache_entry *list_item, static int module_foreach(int argc, const char **argv, const char *prefix) { struct foreach_cb info = FOREACH_CB_INIT; - struct pathspec pathspec; + struct pathspec pathspec = { 0 }; struct module_list list = MODULE_LIST_INIT; struct option module_foreach_options[] = { OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")), @@ -391,12 +391,13 @@ static int module_foreach(int argc, const char **argv, const char *prefix) N_("git submodule foreach [--quiet] [--recursive] [--] "), NULL }; + int ret = 1; argc = parse_options(argc, argv, prefix, module_foreach_options, git_submodule_helper_usage, 0); if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0) - return 1; + goto cleanup; info.argc = argc; info.argv = argv; @@ -404,7 +405,10 @@ static int module_foreach(int argc, const char **argv, const char *prefix) for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info); - return 0; + ret = 0; +cleanup: + clear_pathspec(&pathspec); + return ret; } static int starts_with_dot_slash(const char *const path) @@ -515,7 +519,7 @@ static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data static int module_init(int argc, const char **argv, const char *prefix) { struct init_cb info = INIT_CB_INIT; - struct pathspec pathspec; + struct pathspec pathspec = { 0 }; struct module_list list = MODULE_LIST_INIT; int quiet = 0; struct option module_init_options[] = { @@ -526,12 +530,13 @@ static int module_init(int argc, const char **argv, const char *prefix) N_("git submodule init [] []"), NULL }; + int ret = 1; argc = parse_options(argc, argv, prefix, module_init_options, git_submodule_helper_usage, 0); if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) - return 1; + goto cleanup; /* * If there are no path args and submodule.active is set then, @@ -546,7 +551,10 @@ static int module_init(int argc, const char **argv, const char *prefix) for_each_listed_submodule(&list, init_submodule_cb, &info); - return 0; + ret = 0; +cleanup: + clear_pathspec(&pathspec); + return ret; } struct status_cb { @@ -693,7 +701,7 @@ static void status_submodule_cb(const struct cache_entry *list_item, static int module_status(int argc, const char **argv, const char *prefix) { struct status_cb info = STATUS_CB_INIT; - struct pathspec pathspec; + struct pathspec pathspec = { 0 }; struct module_list list = MODULE_LIST_INIT; int quiet = 0; struct option module_status_options[] = { @@ -706,12 +714,13 @@ static int module_status(int argc, const char **argv, const char *prefix) N_("git submodule status [--quiet] [--cached] [--recursive] [...]"), NULL }; + int ret = 1; argc = parse_options(argc, argv, prefix, module_status_options, git_submodule_helper_usage, 0); if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) - return 1; + goto cleanup; info.prefix = prefix; if (quiet) @@ -719,7 +728,10 @@ static int module_status(int argc, const char **argv, const char *prefix) for_each_listed_submodule(&list, status_submodule_cb, &info); - return 0; + ret = 0; +cleanup: + clear_pathspec(&pathspec); + return ret; } struct module_cb { @@ -1261,7 +1273,7 @@ static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data static int module_sync(int argc, const char **argv, const char *prefix) { struct sync_cb info = SYNC_CB_INIT; - struct pathspec pathspec; + struct pathspec pathspec = { 0 }; struct module_list list = MODULE_LIST_INIT; int quiet = 0; int recursive = 0; @@ -1275,12 +1287,13 @@ static int module_sync(int argc, const char **argv, const char *prefix) N_("git submodule sync [--quiet] [--recursive] []"), NULL }; + int ret = 1; argc = parse_options(argc, argv, prefix, module_sync_options, git_submodule_helper_usage, 0); if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) - return 1; + goto cleanup; info.prefix = prefix; if (quiet) @@ -1290,7 +1303,10 @@ static int module_sync(int argc, const char **argv, const char *prefix) for_each_listed_submodule(&list, sync_submodule_cb, &info); - return 0; + ret = 0; +cleanup: + clear_pathspec(&pathspec); + return ret; } struct deinit_cb { @@ -1399,7 +1415,7 @@ static void deinit_submodule_cb(const struct cache_entry *list_item, static int module_deinit(int argc, const char **argv, const char *prefix) { struct deinit_cb info = DEINIT_CB_INIT; - struct pathspec pathspec; + struct pathspec pathspec = { 0 }; struct module_list list = MODULE_LIST_INIT; int quiet = 0; int force = 0; @@ -1414,6 +1430,7 @@ static int module_deinit(int argc, const char **argv, const char *prefix) N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [...]]"), NULL }; + int ret = 1; argc = parse_options(argc, argv, prefix, module_deinit_options, git_submodule_helper_usage, 0); @@ -1428,7 +1445,7 @@ static int module_deinit(int argc, const char **argv, const char *prefix) die(_("Use '--all' if you really want to deinitialize all submodules")); if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) - return 1; + goto cleanup; info.prefix = prefix; if (quiet) @@ -1438,7 +1455,10 @@ static int module_deinit(int argc, const char **argv, const char *prefix) for_each_listed_submodule(&list, deinit_submodule_cb, &info); - return 0; + ret = 0; +cleanup: + clear_pathspec(&pathspec); + return ret; } struct module_clone_data { @@ -2543,7 +2563,7 @@ static int update_submodules(struct update_data *update_data) static int module_update(int argc, const char **argv, const char *prefix) { - struct pathspec pathspec; + struct pathspec pathspec = { 0 }; struct update_data opt = UPDATE_DATA_INIT; struct list_objects_filter_options filter_options = { 0 }; int ret; @@ -2620,8 +2640,8 @@ static int module_update(int argc, const char **argv, const char *prefix) opt.update_strategy.type = opt.update_default; if (module_list_compute(argc, argv, prefix, &pathspec, &opt.list) < 0) { - list_objects_filter_release(&filter_options); - return 1; + ret = 1; + goto cleanup; } if (pathspec.nr) @@ -2632,8 +2652,10 @@ static int module_update(int argc, const char **argv, const char *prefix) struct init_cb info = INIT_CB_INIT; if (module_list_compute(argc, argv, opt.prefix, - &pathspec, &list) < 0) - return 1; + &pathspec, &list) < 0) { + ret = 1; + goto cleanup; + } /* * If there are no path args and submodule.active is set then, @@ -2650,7 +2672,9 @@ static int module_update(int argc, const char **argv, const char *prefix) } ret = update_submodules(&opt); +cleanup: list_objects_filter_release(&filter_options); + clear_pathspec(&pathspec); return ret; } @@ -2734,7 +2758,7 @@ static int push_check(int argc, const char **argv, const char *prefix) static int absorb_git_dirs(int argc, const char **argv, const char *prefix) { int i; - struct pathspec pathspec; + struct pathspec pathspec = { 0 }; struct module_list list = MODULE_LIST_INIT; unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES; struct option embed_gitdir_options[] = { @@ -2749,17 +2773,21 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix) N_("git submodule absorbgitdirs [] [...]"), NULL }; + int ret = 1; argc = parse_options(argc, argv, prefix, embed_gitdir_options, git_submodule_helper_usage, 0); if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) - return 1; + goto cleanup; for (i = 0; i < list.nr; i++) absorb_git_dir_into_superproject(list.entries[i]->name, flags); - return 0; + ret = 0; +cleanup: + clear_pathspec(&pathspec); + return ret; } static int module_config(int argc, const char **argv, const char *prefix) From patchwork Wed Aug 31 23:14:11 2022 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: 12961509 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3DC5EECAAD5 for ; Wed, 31 Aug 2022 23:15:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232691AbiHaXPQ (ORCPT ); Wed, 31 Aug 2022 19:15:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232386AbiHaXOz (ORCPT ); Wed, 31 Aug 2022 19:14:55 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 522A8DC8 for ; Wed, 31 Aug 2022 16:14:48 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id b5so20114435wrr.5 for ; Wed, 31 Aug 2022 16:14:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=DQx6imhlBUneY92pgX1y9t5DR4IRY0cgGsLxwK8nfqo=; b=B6hoFkFCjdImar2LDzc6RNq1f0eYizjxI9btNdFE4F5oI1ipkZRbgR0+1R68Es1417 CTYSDycAskjEe0djc0PVxD/1tK3iXJBCR5PazN0UrC/pG7mqUpQNVkXUx0E61A3TDyg8 VKcNq7QaG8I4EYuEGIvZB7ZH5ydox9I2l9lgI+h3JITrWT+mQ4eDSc6tb4OFYwaJtyxS E6o/iqLiGmpVVbd5hLr8f5a5tnlQorvR9g2PlAtI5xG4REZxiyy63wISePyvqY0jbfeE KpY1boppDDeRomz1SDUk0Wt0/fMABbCHO2+bpEhRhClhtDCgzTeqM7WQiI4Fb4VuvmAp KUGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=DQx6imhlBUneY92pgX1y9t5DR4IRY0cgGsLxwK8nfqo=; b=NctsSlDB94S9GRBLEWcaEufqmP8ASPGhm2YUDr1B8AgJYaSeDe1FKVnzx1oXeuZWG2 2EdGzN8B3osOWMGeopLHdlQ8aF4HDfD36mVL+lr3YD8gqd7RjqWeBHH3N8cA3VB1WQs/ jZ8FdHKV7GKHFJNwfCTTr9iZ6xTikNhYJG8Xw7h9qfejZ9IujIhd4oJC4MB+ej5/6NRe QZTbxDnTaboyruVahv+ZCh10pPJm+h147gdMt95izwJ9URfmuA+i0aCM9h08mHb+HMje 0g1nj9xy07bdixoLaNn13cBCJunvP4eNOvL2FwMbjXXHgBmKGmlpBTGSczp0KjkhYezi kzWQ== X-Gm-Message-State: ACgBeo2/To4KERqy4Z/LwuEfS+ofXBaFMWPPd9Co3jdRlLyw587eXyaP WYjzQ3SM+qlABCyZQHmuGzAtS4qiZ27UkA== X-Google-Smtp-Source: AA6agR74m2hU5jOD2mBc/itOLJv2WkhALqmePDBx4vpOZ/EVgKBOfbHqgV/KGC0ijAhuFqEVfbJZ4g== X-Received: by 2002:a05:6000:243:b0:226:d241:11e6 with SMTP id m3-20020a056000024300b00226d24111e6mr12005506wrz.187.1661987686788; Wed, 31 Aug 2022 16:14:46 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:46 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 04/17] submodule--helper: "struct pathspec" memory leak in module_update() Date: Thu, 1 Sep 2022 01:14:11 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The module_update() function calls module_list_compute() twice, which in turn will reset the "struct pathspec" passed to it. Let's instead track two of them, and clear them both. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 2f8603a6bb5..e8bc6f17330 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2564,6 +2564,7 @@ static int update_submodules(struct update_data *update_data) static int module_update(int argc, const char **argv, const char *prefix) { struct pathspec pathspec = { 0 }; + struct pathspec pathspec2 = { 0 }; struct update_data opt = UPDATE_DATA_INIT; struct list_objects_filter_options filter_options = { 0 }; int ret; @@ -2652,7 +2653,7 @@ static int module_update(int argc, const char **argv, const char *prefix) struct init_cb info = INIT_CB_INIT; if (module_list_compute(argc, argv, opt.prefix, - &pathspec, &list) < 0) { + &pathspec2, &list) < 0) { ret = 1; goto cleanup; } @@ -2675,6 +2676,7 @@ static int module_update(int argc, const char **argv, const char *prefix) cleanup: list_objects_filter_release(&filter_options); clear_pathspec(&pathspec); + clear_pathspec(&pathspec2); return ret; } From patchwork Wed Aug 31 23:14:12 2022 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: 12961507 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3A43ECAAD1 for ; Wed, 31 Aug 2022 23:15:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232672AbiHaXPO (ORCPT ); Wed, 31 Aug 2022 19:15:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232566AbiHaXOz (ORCPT ); Wed, 31 Aug 2022 19:14:55 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52540DF1 for ; Wed, 31 Aug 2022 16:14:49 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id r204-20020a1c44d5000000b003a84b160addso392280wma.2 for ; Wed, 31 Aug 2022 16:14:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=vRxWElH2jM/mIQMTkiaDRlEwvo+aI4aVcwyDfJthgeA=; b=FwBsMZhZMrm0zLDt+jX0NdJuVSlCpxG69CJmdTsGIJ4v8D5Z4UUGCOeDfkZ6E7WFVD wpuwclS9J4ALR31DkDIuBrYlmu3yfihZxXpVaB72TDHN8tTC3rRaJhs0fcNxmi5DsSUp tFc42QpJsaZJMqWhapyIutV0SgcUNPmcmdOL+1DLAr62ei2VqGr9kvH9YyYQfMj93GTM E+n2MtEtdkBXLBuQ2ybEg6c0/oFXgwzrTHKBpse/xmpXS/ComI5EEXYLEFJpqZM5pxNu POL344D1z8E7HEuFqmuqQtMh3TfobZ58/kY+fsJ1qugh6O6B3xKTkM797tISR3ObOAGQ GN3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=vRxWElH2jM/mIQMTkiaDRlEwvo+aI4aVcwyDfJthgeA=; b=V9xYyVez76dIqByzDa3v8KZ5Ky44wQwnczcksWuzPxz91JcZFlUV6AgvmLRKRK1hNY zZ/xSBSBr6mx29RYDjHZrECv4sGIytjemJcMa5Z4QZxLtJjkqMTVRtVzS3YH/hfa1f4Y MmcibpRuwYF4g3HmAGyypuLwgEtxHogbWVt6HZBDsS5kdfsmzPTBmcoL0Ay/a1wF7V3G f6SISQaFBQrdMfZH7xr0QgNNL3b4jnbAPVLir3WgqtV/69Xk10ZoapjENyisTF7ADaYi x39l8H5hOXWt6EY/N3LLNHqX1HIbkJr/AwwCMIM/5/CBk3ge4yU2DcJpkMuJlg8lZliG a+2w== X-Gm-Message-State: ACgBeo1ibYWiQq3n84XkXMLf1Ez1Y3l9eXBq9hAb5m582B5vvmrSVx9R dBT0ETTRmZ9HgT2BMW0PFvulIQ3YNOfpqw== X-Google-Smtp-Source: AA6agR5T7OXOLkOKvIRa4stgJBn7Aqg/pF3GWpXqYHqzyFKp2T7nBeVBGAcyi8MKxEuuZ453zIyAJA== X-Received: by 2002:a05:600c:2311:b0:3a5:af18:75c1 with SMTP id 17-20020a05600c231100b003a5af1875c1mr3273884wmo.90.1661987687893; Wed, 31 Aug 2022 16:14:47 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:47 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 05/17] submodule--helper: don't leak {run,capture}_command() cp.dir argument Date: Thu, 1 Sep 2022 01:14:12 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a memory leak in c51f8f94e5b (submodule--helper: run update procedures from C, 2021-08-24) and 3c3558f0953 (submodule--helper: run update using child process struct, 2022-03-15) by not allocating memory in the first place. The "dir" member of "struct child_process" will not be modified by that API, and it's declared to be "const char *". So let's not needlessly duplicate these strings. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index e8bc6f17330..0c23dd93d96 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2129,7 +2129,7 @@ static int is_tip_reachable(const char *path, const struct object_id *oid) char *hex = oid_to_hex(oid); cp.git_cmd = 1; - cp.dir = xstrdup(path); + cp.dir = path; cp.no_stderr = 1; strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL); @@ -2148,7 +2148,7 @@ static int fetch_in_submodule(const char *module_path, int depth, int quiet, prepare_submodule_repo_env(&cp.env); cp.git_cmd = 1; - cp.dir = xstrdup(module_path); + cp.dir = module_path; strvec_push(&cp.args, "fetch"); if (quiet) @@ -2201,7 +2201,7 @@ static int run_update_command(const struct update_data *ud, int subforce) } strvec_push(&cp.args, oid); - cp.dir = xstrdup(ud->sm_path); + cp.dir = ud->sm_path; prepare_submodule_repo_env(&cp.env); if ((ret = run_command(&cp))) { switch (ud->update_strategy.type) { From patchwork Wed Aug 31 23:14:13 2022 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: 12961510 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9D55DECAAD1 for ; Wed, 31 Aug 2022 23:15:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232696AbiHaXPS (ORCPT ); Wed, 31 Aug 2022 19:15:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232585AbiHaXOz (ORCPT ); Wed, 31 Aug 2022 19:14:55 -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 3F062F3D for ; Wed, 31 Aug 2022 16:14:51 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id az27so20127114wrb.6 for ; Wed, 31 Aug 2022 16:14:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=Qi4agyD/LA8El8RXzhvsSF8DI0jluhgQGU6OkmGzv0s=; b=R6mns3HlB8Z4ilJ1GrCKRy670935pvlXE7UT9PVIRKT8iLNUzSLVY4+nZG/mi1vLyu 0FfUoiw5xcDPDZ3io2JYonBIC6BWUa/Ad81KeJHVy13e+wH1MM90TMMnqU+850JVlnJJ qYldOqf8XpiL2tyNktvb67W1n4nqeihMEhLHro6qmuZZfspZrX3EObmSgIqA50WaIUfY 2e5cvh8KrkiGzd13YJtI33iqnqGJlVRrJ2PNvOvHP1gSu/1BGrTtoJJKW7EFh399v2jB Q63a/H07TjCnPnW+tSGEVgIA814/OojNam8T3OaKfWCaPqRhcsl+v2NPtAXRK1+udzU2 V5Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=Qi4agyD/LA8El8RXzhvsSF8DI0jluhgQGU6OkmGzv0s=; b=UQGCpwLN7Fi3mCH9UQ6meY1ZjHOd//lzyR2dJXHT/gPe7ebirG0iJZzQUknkS0D95M yVWXTyoY8/rNwJ/GT3lRMmpfRHQLj3v5elQp5VGh14H99pD68b795pGx3tCe51yI0ImJ mxrsW4Dv0/fqPVBPwF8h3lX1xq1Tg3Uc4ilxHNR4eDMe3hAmcY0jzZvQCz+n3iJeBqQP c2mDLO/ZNbSEC/8wEO8ZMsE8T7agrEDlzB9eBj9NqjY9aeGnHzxel/vS8PZ40OyJ09Iv Uk02yv16zZFlgqqBqgwp5fG5YjJih01/vLCuhF15coi4KaS/Bs7zU9m/HwV33WEMHWD4 zFyg== X-Gm-Message-State: ACgBeo12S7pCH65LAqnxBux8V4G7Qt6oVMlP/f3dRw17wznAxwIM/X7t 9tthQaNf9GcNKeWN6KBqu5KAJAo7z5zeZA== X-Google-Smtp-Source: AA6agR7W39zuL3/Gt+fs1sryx5jbqUwGDnBzfGTKre+PqpP5NDUgyytQtFt3ZDHGJFYEgPGbbdcdKg== X-Received: by 2002:a5d:5350:0:b0:225:7560:8403 with SMTP id t16-20020a5d5350000000b0022575608403mr12229065wrv.507.1661987689133; Wed, 31 Aug 2022 16:14:49 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:48 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 06/17] submodule--helper: add and use *_release() functions Date: Thu, 1 Sep 2022 01:14:13 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add release functions for "struct module_list", "struct submodule_update_clone" and "struct update_data". Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 28 +++++++++++++++++++++++++++- t/t6134-pathspec-in-submodule.sh | 1 + 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 0c23dd93d96..ae918a662b4 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -172,6 +172,11 @@ struct module_list { }; #define MODULE_LIST_INIT { 0 } +static void module_list_release(struct module_list *ml) +{ + free(ml->entries); +} + static int module_list_compute(int argc, const char **argv, const char *prefix, struct pathspec *pathspec, @@ -234,7 +239,7 @@ static void module_list_active(struct module_list *list) active_modules.entries[active_modules.nr++] = ce; } - free(list->entries); + module_list_release(list); *list = active_modules; } @@ -407,6 +412,7 @@ static int module_foreach(int argc, const char **argv, const char *prefix) ret = 0; cleanup: + module_list_release(&list); clear_pathspec(&pathspec); return ret; } @@ -553,6 +559,7 @@ static int module_init(int argc, const char **argv, const char *prefix) ret = 0; cleanup: + module_list_release(&list); clear_pathspec(&pathspec); return ret; } @@ -730,6 +737,7 @@ static int module_status(int argc, const char **argv, const char *prefix) ret = 0; cleanup: + module_list_release(&list); clear_pathspec(&pathspec); return ret; } @@ -1305,6 +1313,7 @@ static int module_sync(int argc, const char **argv, const char *prefix) ret = 0; cleanup: + module_list_release(&list); clear_pathspec(&pathspec); return ret; } @@ -1457,6 +1466,7 @@ static int module_deinit(int argc, const char **argv, const char *prefix) ret = 0; cleanup: + module_list_release(&list); clear_pathspec(&pathspec); return ret; } @@ -1834,6 +1844,12 @@ struct submodule_update_clone { }; #define SUBMODULE_UPDATE_CLONE_INIT { 0 } +static void submodule_update_clone_release(struct submodule_update_clone *suc) +{ + free(suc->update_clone); + free(suc->failed_clones); +} + struct update_data { const char *prefix; const char *displaypath; @@ -1872,6 +1888,11 @@ struct update_data { .max_jobs = 1, \ } +static void update_data_release(struct update_data *ud) +{ + module_list_release(&ud->list); +} + static void next_submodule_warn_missing(struct submodule_update_clone *suc, struct strbuf *out, const char *displaypath) { @@ -2557,6 +2578,7 @@ static int update_submodules(struct update_data *update_data) } cleanup: + submodule_update_clone_release(&suc); string_list_clear(&update_data->references, 0); return ret; } @@ -2654,6 +2676,7 @@ static int module_update(int argc, const char **argv, const char *prefix) if (module_list_compute(argc, argv, opt.prefix, &pathspec2, &list) < 0) { + module_list_release(&list); ret = 1; goto cleanup; } @@ -2670,10 +2693,12 @@ static int module_update(int argc, const char **argv, const char *prefix) info.flags |= OPT_QUIET; for_each_listed_submodule(&list, init_submodule_cb, &info); + module_list_release(&list); } ret = update_submodules(&opt); cleanup: + update_data_release(&opt); list_objects_filter_release(&filter_options); clear_pathspec(&pathspec); clear_pathspec(&pathspec2); @@ -2789,6 +2814,7 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix) ret = 0; cleanup: clear_pathspec(&pathspec); + module_list_release(&list); return ret; } diff --git a/t/t6134-pathspec-in-submodule.sh b/t/t6134-pathspec-in-submodule.sh index 0f1cb49cedc..3a241f259de 100755 --- a/t/t6134-pathspec-in-submodule.sh +++ b/t/t6134-pathspec-in-submodule.sh @@ -2,6 +2,7 @@ test_description='test case exclude pathspec' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh test_expect_success 'setup a submodule' ' From patchwork Wed Aug 31 23:14:14 2022 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: 12961511 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C077FECAAD1 for ; Wed, 31 Aug 2022 23:15:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232698AbiHaXPT (ORCPT ); Wed, 31 Aug 2022 19:15:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232487AbiHaXO4 (ORCPT ); Wed, 31 Aug 2022 19:14:56 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E10D639F for ; Wed, 31 Aug 2022 16:14:52 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id s23so8109126wmj.4 for ; Wed, 31 Aug 2022 16:14:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=9cS3P9v0M+cXvpQIjdziH8zupYYJcid/h1ZbFDHkCJo=; b=UQo2yDnyjS3u0aR16S9Fs9oRB2HzD5u4suk21tcIDr9yofgfhRcfGItR9jaTSRcjy3 f5ho/G9/DZYALko/POmV3T5a6s2RmNB/CMzbLRbg3FsWHRI+1SwEMt2uwj/UiaKA6HL9 bFkyWM/cpIkdpJBHTZOziXzXZ2m2DL4egRVahKQjxboBmZ3ET0cFDvxMaLFmtBH+KEjF w3fqGuVmIQq/EuufAfMxH1T97JTp6hq7MYwR6p7GjmT2TOxaWbs2+Uif1mnNAVEJ9oD4 TIcLarTZ+H0FgXuDW260u7Japd1XpUfU+WjnyKkULSaLc+KejDeIVUWC3Msa2VTen1oo baTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=9cS3P9v0M+cXvpQIjdziH8zupYYJcid/h1ZbFDHkCJo=; b=nKn7WEd70rOLHDU0Q8Jf/SYZbQYMLGDZdIlMdPvyBx7bZCwsnF/0qS2BK/qPhGXhv4 xCFRalRZN69phva2W2cBPnxt9FwWkLTwRGJb6IV9No4IXnsXvOwQYyLWTebb4hNeqkIv iomgA2vX2Qm1rGpLQCKv6fwhC+fcPJJW4X7U8zJp+rZTh05Xv0XnYF09Tga9oxM9Qup8 DwrUqKCU33fxXKaCbnRNytVPtNCE+4bSAGY6p0uEYN+BHOkm6yHhbvZ8m2kqJyLd9Vj4 j5GUOX3g+LyN7z9Uf/qJYYDvyPSCd36mLInr6izMxPTPRZUbte3iv+58piY6LpNHFeUo zVCA== X-Gm-Message-State: ACgBeo2Kb4HtkUdc2VGd/2E2tNYiVsguP/Jd5w3otXs+ELzA0PL10RTY jKiGWG3JkBai8VwYfUm5KQ4BsObrrYFICQ== X-Google-Smtp-Source: AA6agR4txkT+/Z7kP5mAH/qCLB2HICH7sPyX6a9YJ+vfB3Hvrr0gD4O9nKaiT+/NwT1B+DkrobTxLA== X-Received: by 2002:a05:600c:22cd:b0:3a6:7b62:3778 with SMTP id 13-20020a05600c22cd00b003a67b623778mr3332847wmg.45.1661987690311; Wed, 31 Aug 2022 16:14:50 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:49 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 07/17] submodule--helper: fix "errmsg_str" memory leak Date: Thu, 1 Sep 2022 01:14:14 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a memory leak introduced in e83e3333b57 (submodule: port submodule subcommand 'summary' from shell to C, 2020-08-13), we sometimes append to the "errmsg", and need to free the "struct strbuf". Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 1 + 1 file changed, 1 insertion(+) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index ae918a662b4..75a5d9d47fd 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -973,6 +973,7 @@ static void generate_submodule_summary(struct summary_cb *info, free(displaypath); free(src_abbrev); free(dst_abbrev); + strbuf_release(&errmsg); } static void prepare_submodule_summary(struct summary_cb *info, From patchwork Wed Aug 31 23:14:15 2022 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: 12961512 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F41DECAAD5 for ; Wed, 31 Aug 2022 23:15:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232710AbiHaXPV (ORCPT ); Wed, 31 Aug 2022 19:15:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232500AbiHaXO4 (ORCPT ); Wed, 31 Aug 2022 19:14:56 -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 5D65EDF40 for ; Wed, 31 Aug 2022 16:14:53 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id k6-20020a05600c1c8600b003a54ecc62f6so424957wms.5 for ; Wed, 31 Aug 2022 16:14:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=SgC20aquWKdv3k3UUBSIC+olHZ/FWNAKia64zB/F3fo=; b=ilXfMS/8Zg4rNfyzKGD7bs6DGFVpbm89VpjjZfI60QS+9O7Aj/3AF0PozvbzOw4GRM j49R7DaHIwpOCplSJrZ38yadAb5Pqw8iooRsd24I8UwBcj/3ocDUeTJL7EQmXem8YG7Y Ee9fy0sBD4jZwkD6SwAZeNwTe64GTJ4Pb1a0Y3ugbyndhGXB02hH/4dcuJ2mpN6SYc22 04koubveqYVLCwFG+EfwBHSN5Fg6eYPGdx2nzdwvh/bmcI6kHyI79ev7dkX4YwosJMrC /7DrgOQj++SuMCHJwIDVfeo50c4UZm5cULRo3fmbBDcxnfNVSdI86Jre54lVA3RMBA+R N22g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=SgC20aquWKdv3k3UUBSIC+olHZ/FWNAKia64zB/F3fo=; b=j12Ue2TvttlsqUblRbwqPWHBEAyigNcQY7Lsubo9rp63Y24SGigznoUbfrenuP3MVs HKEQBrTLdA1lBjQKGZgIYbkBKzduRhUYdztyu9wEK2hz9oc85Z/JWu5/xLZ8rRW/Pyh9 HqkTmwr2JcVr+E2p5RAHZroGgi8wFVX6qPE7cxtUjMh7AVpI7znpOMu+46lEyQsXV1c0 hat7QI3U6Ttptpf/+zjEqyXkfd+FAvZXdaJQwIHJTAqiHJtvAZM/HlXd9XtKrevTz558 ywP1/nTBSCyfLKQlRlir+aqH0qNRdvVt0ADeYQ+HLj8xFo0agVPo9M4Rw656oCKPgzO2 ornA== X-Gm-Message-State: ACgBeo18BfcPBT352i+l/AINtck8jN/Sw0eqWXt7/Ta8Hc0xf+ZUBO8n duUX/b8wYMmJ5Y9atDmCm8q/Y4PLnW1R9g== X-Google-Smtp-Source: AA6agR5UpIhlzAq04xZgRr4uJ2I0tuZrAN5Rv8Wd3tGUH2hyuiPSNpzxB3yVFL3vzYKoQ0U3ILZHBA== X-Received: by 2002:a05:600c:3c96:b0:3a6:59b1:5eb5 with SMTP id bg22-20020a05600c3c9600b003a659b15eb5mr3328859wmb.187.1661987691434; Wed, 31 Aug 2022 16:14:51 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:50 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 08/17] submodule--helper: fix "sm_path" and other "module_cb_list" leaks Date: Thu, 1 Sep 2022 01:14:15 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix leaks in "struct module_cb_list" and the "struct module_cb" which it contains, these fix leaks in e83e3333b57 (submodule: port submodule subcommand 'summary' from shell to C, 2020-08-13). The "sm_path" should always have been a "char *", not a "const char *", we always create it with xstrdup(). We can't mark any tests passing passing with SANITIZE=leak using "TEST_PASSES_SANITIZE_LEAK=true" as a result of this change, but "t7401-submodule-summary.sh" gets closer to passing as a result of this change. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 21 ++++++++++++++++++++- t/t7401-submodule-summary.sh | 1 + 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 75a5d9d47fd..162e92c033e 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -748,16 +748,34 @@ struct module_cb { struct object_id oid_src; struct object_id oid_dst; char status; - const char *sm_path; + char *sm_path; }; #define MODULE_CB_INIT { 0 } +static void module_cb_release(struct module_cb *mcb) +{ + free(mcb->sm_path); +} + struct module_cb_list { struct module_cb **entries; int alloc, nr; }; #define MODULE_CB_LIST_INIT { 0 } +static void module_cb_list_release(struct module_cb_list *mcbl) +{ + int i; + + for (i = 0; i < mcbl->nr; i++) { + struct module_cb *mcb = mcbl->entries[i]; + + module_cb_release(mcb); + free(mcb); + } + free(mcbl->entries); +} + struct summary_cb { int argc; const char **argv; @@ -1104,6 +1122,7 @@ static int compute_summary_module_list(struct object_id *head_oid, cleanup: strvec_clear(&diff_args); release_revisions(&rev); + module_cb_list_release(&list); return ret; } diff --git a/t/t7401-submodule-summary.sh b/t/t7401-submodule-summary.sh index 9c3cc4cf404..542b3331a78 100755 --- a/t/t7401-submodule-summary.sh +++ b/t/t7401-submodule-summary.sh @@ -17,6 +17,7 @@ This test script tries to verify the sanity of summary subcommand of git submodu # various reasons, one of them being that there are lots of commands taking place # outside of 'test_expect_success' block, which is no longer in good-style. +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh add_file () { From patchwork Wed Aug 31 23:14:16 2022 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: 12961515 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DAFAECAAD4 for ; Wed, 31 Aug 2022 23:15:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232743AbiHaXPh (ORCPT ); Wed, 31 Aug 2022 19:15:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232630AbiHaXO6 (ORCPT ); Wed, 31 Aug 2022 19:14:58 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50A121408B for ; Wed, 31 Aug 2022 16:14:55 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id e20so20089544wri.13 for ; Wed, 31 Aug 2022 16:14:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=b+Fin/U8My2sXpZAkBUbN0lsrFkTq6kXsZD4l19AFog=; b=Y6jAXeCQ0FoTO29cJVy534EWUBGPef5Xvv7lnEG9JB9yQFB9rL9wrVekcbcIVj1smR T4ArRNo0eY8E3oqTirXBZSGQ+24dIJDCCxc4COEpvn70iWnIedijGguKPbhPmtPfXx6w wKB1EM7evc0ME+YzLfrVRVJrq+M9rFjYHrjPnLs1UpQT4I1rXmG1pml4W5GWHFeiLRBM 0mP7JRjIDIQ1EsMFSJqF4Bb1zA9Q+jumcKBFsYkVnxOBWSmgMjenLa0vtaqxXnSuVbUh +L7nEqgCG2iG3oQVTWIrg8MFaNm8JVtNtm9M2gpCQfga9XVv4yOy+/FZeO48hLpFgZW1 aQKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=b+Fin/U8My2sXpZAkBUbN0lsrFkTq6kXsZD4l19AFog=; b=rV9Dkido/OKp+MGMgRTCcrzbv2bbNfG3OiuvTOMkcA+BgvfbbKsnELKe/TiPCAbR4D 3GCPHENgvGzL1AaPogykg+iaxLSXtLKN5GPwG99VU1BQd5mrRBP5YVomm7elWEJCZxEg 5QBduZ9Or4RKnOtLcuWKGj7N7iZxbmW/ugjlmq42KlRysjaZ1dFpNW4FC1DvlZo/Y0vE 0eMbG/+SAiEefsTQ2YO6+Mu02yix+6WcC5G7wolvTwmdqj6JKCMVkVdC8IZFkicACSYu XPeUJOvFfKyKi/qcZS1k4bNabKG0SC7Y1NB2W8jEXI3aKpmG3bXGfJ4j8IFslKcoKi8R jCUw== X-Gm-Message-State: ACgBeo2u5B+MZ21zepT/4kWAwGu9Kc7/OKrOU9A6QZQeHNHwBxvLQXad RusCJww14wsLNxl7PTUcE4wRUEInXigRyA== X-Google-Smtp-Source: AA6agR6rxsfcZ1c+HPOMqNuPdwbuoku4dBNYFLjHldGH4SFA6trytSgtFgg3xq0FRXyQlANUwAbCWg== X-Received: by 2002:adf:f642:0:b0:226:d4b1:8502 with SMTP id x2-20020adff642000000b00226d4b18502mr11539401wrp.553.1661987692542; Wed, 31 Aug 2022 16:14:52 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:51 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 09/17] submodule--helper: fix a leak with repo_clear() Date: Thu, 1 Sep 2022 01:14:16 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Call repo_clear() in ensure_core_worktree() to free the "struct repository". Fixes a leak that's been here since 74d4731da1f (submodule--helper: replace connect-gitdir-workingtree by ensure-core-worktree, 2018-08-13). Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 1 + 1 file changed, 1 insertion(+) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 162e92c033e..9a277057012 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2400,6 +2400,7 @@ static int ensure_core_worktree(const char *path) strbuf_release(&sb); } + repo_clear(&subrepo); return 0; } From patchwork Wed Aug 31 23:14:17 2022 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: 12961513 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D005ECAAD1 for ; Wed, 31 Aug 2022 23:15:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232731AbiHaXPe (ORCPT ); Wed, 31 Aug 2022 19:15:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232631AbiHaXO7 (ORCPT ); Wed, 31 Aug 2022 19:14:59 -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 129A626A for ; Wed, 31 Aug 2022 16:14:55 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id d12-20020a05600c34cc00b003a83d20812fso429704wmq.1 for ; Wed, 31 Aug 2022 16:14:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=ShTHgaNE6xXKX0jsipPBvSt4Te3BNeZ8p2MOAsFRmmI=; b=IkZ4qpmhPiLi4ppCzehSOTiGfzEVuomEAHnisVMCpRznUYZKPo3eUb2IptyrUxWchy MERBj2H8r6+VyiHSkG/mg3oTDaPorn8qeNsk4CKRIkZWewlO4l8wIHJgxykKMC4V7/4k AJRGk2Us4yArZsgcOzVL/9ducsR/XnPwOE+s1Nxvtgs1PhuyScB4NwarHoH3G9+eYy8C 0pt1z2cmBB4aXvQRN6a+EOM+i8sARPNtBcoXq32stmJSvgBM1GWDrkzyCr+8ekDiHPTO dXkXn+cHCPsI83Gbt9snyAs8J6WfzKfnn2RmjYI5JkE8kQNd3hDous+WTWcBlXKPL2LO 9eXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=ShTHgaNE6xXKX0jsipPBvSt4Te3BNeZ8p2MOAsFRmmI=; b=HRL04YeyxFVfDV9kenWFzskHZfw6SeRYYB9iln32sjqALZwTIROquKXkD/DYXTzBrn eGAGfVKHJvzeWY8FKg1OeGYRMt0RKeeVUcUBK9vzLaltat9unuBFl1JiPZsF4SI8tgre 2Y0fgmLdso09zSrdGitwWbTE8PleyQ782qcUiYPWJf/4HszAD5qKcLaGZGIJAjlhclPj MRgkMbe3iEuxwuOY0fOOd3yeURNyIQE6ec6OLcEaE7VelphSpdlNAcy22uefItITyb6+ mlI1fwiD1P/JklnvWmPDE1BIWHy7uNwr3L9YlpNvA4J3cxXEB1d57gwJn3KUDWp4OpS9 qvdw== X-Gm-Message-State: ACgBeo0aOhjRYdJmNDTbzoTFt8Qry+tHAzJmO5Msnx6EeBWjPaG6jqWr oReLiAZ8EIVuMNIuJvnFutM5fc0JWuVKPw== X-Google-Smtp-Source: AA6agR70rwFnAoALmqhtDL22GL7HvIa/cAFE572CQnZ0TAxQLIbXPtr7mevJOq2beWh5sVl8t3Ir2A== X-Received: by 2002:a05:600c:1e05:b0:3a5:b441:e9c with SMTP id ay5-20020a05600c1e0500b003a5b4410e9cmr3335630wmb.24.1661987693795; Wed, 31 Aug 2022 16:14:53 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:53 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 10/17] submodule--helper: fix a memory leak in get_default_remote_submodule() Date: Thu, 1 Sep 2022 01:14:17 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a memory leak in the get_default_remote_submodule() function added in a77c3fcb5ec (submodule--helper: get remote names from any repository, 2022-03-04), we need to repo_clear() the submodule we initialize. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 9a277057012..9f9e5f05094 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -65,12 +65,16 @@ static int repo_get_default_remote(struct repository *repo, char **default_remot static int get_default_remote_submodule(const char *module_path, char **default_remote) { struct repository subrepo; + int ret; if (repo_submodule_init(&subrepo, the_repository, module_path, null_oid()) < 0) return die_message(_("could not get a repository handle for submodule '%s'"), module_path); - return repo_get_default_remote(&subrepo, default_remote); + ret = repo_get_default_remote(&subrepo, default_remote); + repo_clear(&subrepo); + + return ret; } static char *get_default_remote(void) From patchwork Wed Aug 31 23:14:18 2022 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: 12961514 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5EFCCECAAD4 for ; Wed, 31 Aug 2022 23:15:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232740AbiHaXPg (ORCPT ); Wed, 31 Aug 2022 19:15:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232632AbiHaXO7 (ORCPT ); Wed, 31 Aug 2022 19:14:59 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8A70BA3 for ; Wed, 31 Aug 2022 16:14:56 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id w5so3947064wrn.12 for ; Wed, 31 Aug 2022 16:14:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=4eJmDU3g6LqrwEvZWabChjP+noxCms3uGQbVJGBv4ww=; b=BUevs4cYur1FN3XhZ0AtNCt6GReOXVaHvneGGjWrhDRq6Jr97rC9IWri108DI60Tt3 kniklj9C4fucdO4PsnANK921TdaXNBVzF47e8jc0KQlqK8Ez1oMu6csjM82PZ7hT/tNA PP8Vy6i6+++BHdPHe/Fsu7wAVNUHbucM1y8cxzmrXfXlxDO7PZIB8RonoUgD3qpK6z/P JevvpyRCOMV1OmX9yQCK3DA4wVV7N8riFQDZQPz+CwPU1RXcdG8qk6IS75ZdMpmLjoeY dekWJrclz4RJ75p39m5YicgNEdedXan+2bPJ2uHx0btlS0NFKtb9sQy99EVC7AcQqpQj oCkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=4eJmDU3g6LqrwEvZWabChjP+noxCms3uGQbVJGBv4ww=; b=QO8v/MUTDflVku9/9XGo6EKUC8RKPBnMTyBfpurel15yEmTp3CFUOeDg2wZeHyPZYb w1t8T35ST0blgw5nXtZezJD8392S4+vMzdq36En6rY+CSD5VCuoghG2Jv0wJ17ThuYln aKgwScj5eZRR0iz0IyNXjeHIwpZZ6YMMW2VtQh1lxBX+GFuINUXGcgPg6N0JuINOd9Ot 8uiu122YTphjFGxwRGuasf6ZaLQM+cP+7t5oT1XqTphFnM7VtZOj75o3yZ2Pknbp2NKm LvoawUdU3zXTcqX6dp7vFNxgEgeN15UYNFcEwGTWnQfLqZrsHgc0+Hsj+D9sLt+Ksyy/ BCow== X-Gm-Message-State: ACgBeo3ukQzZ3Tff7t5Dxk0z5aS0vcP5PH2l5+Jv1IDW10BOEdLOAGUX +7BRncMzv463XGqiXieEIP5p51QPQs+ZAA== X-Google-Smtp-Source: AA6agR7m4qnErFDxWmnVEIZXTCzNRGXLjuur3DVBvp+/z1I9Fcn/JtkfSwWKnNcFYKtsfP0MP4ZPwQ== X-Received: by 2002:a5d:6daa:0:b0:225:5eeb:f76f with SMTP id u10-20020a5d6daa000000b002255eebf76fmr12463618wrs.479.1661987695026; Wed, 31 Aug 2022 16:14:55 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:54 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 11/17] submodule--helper: fix "reference" leak Date: Thu, 1 Sep 2022 01:14:18 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix leaks in the "reference" variable declared in add_submodule() and module_clone(). In preceding commits this variable was refactored out of the "struct module_clone_data", but the leak has been with us since 31224cbdc72 (clone: recursive and reference option triggers submodule alternates, 2016-08-17) and 8c8195e9c3e (submodule--helper: introduce add-clone subcommand, 2021-07-10). Those commits added an xstrdup()'d member of the STRING_LIST_INIT_NODUP'd "struct string_list". We need to free() those, but not the ones we get from argv, let's make use of the "util" member, if it has a pointer it's the pointer we'll need to free, otherwise it'll be NULL (i.e. from argv). Note that the free() of the "util" member is needed in both module_clone() and add_submodule(). The module_clone() function itself doesn't populate the "util" pointer as add_submodule() does, but module_clone() is upstream of the add_possible_reference_from_superproject() caller we're modifying here, which does do that. This does preclude the use of the "util" pointer for any other reasons for now, but that's OK. If we ever need to use it for something else we could turn it into a small "struct" with an optional "to_free" member, and switch to using string_list_clear_func(). Alternatively we could have another "struct string_list to_free" which would keep a copy of the strings we've dup'd to free(). But for now this is perfectly adequate. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 9f9e5f05094..170eaf010ac 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -1566,7 +1566,9 @@ static int add_possible_reference_from_superproject( sm_alternate = compute_alternate_path(sb.buf, &err); if (sm_alternate) { - string_list_append(sas->reference, xstrdup(sb.buf)); + char *p = strbuf_detach(&sb, NULL); + + string_list_append(sas->reference, p)->util = p; free(sm_alternate); } else { switch (sas->error_mode) { @@ -1798,6 +1800,7 @@ static int module_clone(int argc, const char **argv, const char *prefix) clone_submodule(&clone_data, &reference); list_objects_filter_release(&filter_options); + string_list_clear(&reference, 1); return 0; } @@ -3045,6 +3048,7 @@ static int add_submodule(const struct add_data *add_data) char *submod_gitdir_path; struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT; struct string_list reference = STRING_LIST_INIT_NODUP; + int ret = -1; /* perhaps the path already exists and is already a git repo, else clone it */ if (is_directory(add_data->sm_path)) { @@ -3100,15 +3104,17 @@ static int add_submodule(const struct add_data *add_data) clone_data.url = add_data->realrepo; clone_data.quiet = add_data->quiet; clone_data.progress = add_data->progress; - if (add_data->reference_path) - string_list_append(&reference, - xstrdup(add_data->reference_path)); + if (add_data->reference_path) { + char *p = xstrdup(add_data->reference_path); + + string_list_append(&reference, p)->util = p; + } clone_data.dissociate = add_data->dissociate; if (add_data->depth >= 0) clone_data.depth = xstrfmt("%d", add_data->depth); if (clone_submodule(&clone_data, &reference)) - return -1; + goto cleanup; prepare_submodule_repo_env(&cp.env); cp.git_cmd = 1; @@ -3127,7 +3133,10 @@ static int add_submodule(const struct add_data *add_data) if (run_command(&cp)) die(_("unable to checkout submodule '%s'"), add_data->sm_path); } - return 0; + ret = 0; +cleanup: + string_list_clear(&reference, 1); + return ret; } static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value) From patchwork Wed Aug 31 23:14:19 2022 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: 12961516 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B5AFECAAD1 for ; Wed, 31 Aug 2022 23:15:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232747AbiHaXPm (ORCPT ); Wed, 31 Aug 2022 19:15:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232648AbiHaXPL (ORCPT ); Wed, 31 Aug 2022 19:15:11 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC396205FB for ; Wed, 31 Aug 2022 16:14:57 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id u17so858258wrp.3 for ; Wed, 31 Aug 2022 16:14:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=P3z2thcGVWYUQbMKsW9j3j+BCZPoI5S6eUNvyNy2kBc=; b=hjUP7qKBXWnvdmuVUeq+6Mtf8uHKnkhLbeSU81gDGTwISiJWIb+YgtARAMFPGbBY/6 hgd9RHOqrxVN+BkXjH1AB08VOPp6o9syY0RkDbCW8lvIi5/i7tDpobO3vVo78g6yv9el YUtTBo8y98/FZY9fRuaqjY4Qiwg6R2mIwansRiGnWevDt4+nA+OGLENa1N9jWvLHYz0Q cAUQ7TLcb+0EWqxN9ysUrN2IHfoGhBBPfyUJsWyx4IEXovA6DZIsMbe1a+ldB7/4alTO pz9qGfi2aXSmo7/K/JuHp46wkklRpMzu3MmEA0o9uh/lHh9ZnMZVAeKenClQwRMqXRgp 2guw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=P3z2thcGVWYUQbMKsW9j3j+BCZPoI5S6eUNvyNy2kBc=; b=N7l4beZfo+bcnTskyddR0621umezVS6q0nNIBDAinstUlIUYJtVBD8aaEDo2dUfWEc R/Hbner5dnMWOceE4VzlO4zcslG7oO5YhSKVwVaHmE5oUyycA9i3UoC5C/5fmLhS7FKs N9Au2cplcSbEte5ZxVVp7HjkqI6cCxqzAl6zT7QmaLnML5jxJlYw9dEyjgzSdxbv1h4B ru8GfkCIGFA6LAf1yKZE8gHlxEctKibtbuwkqsSN3gIfsKHAJCNgKur4MdDyBDvXlZOc KhVjC3F2NGYwvsOpl8PdFCD8Sq3x3c62cWR8KlJdmMc374+6jCY4iOAA8tZ3zJHAW5bk uM6Q== X-Gm-Message-State: ACgBeo0S9AXhxS8R94QWqZW5yfD1iWkqAIbJG1rY9h/D6MLTXuA/gvSB F5qBgLyzP6krSJkbUbN/ttWK6nvBUSfj/A== X-Google-Smtp-Source: AA6agR78rWWfFRl4XC/B5ppP4E9qpLyVML8LsgJs6r8Drks54v3MzT9aYSLv+GYXgh9g0MEtU8DKBg== X-Received: by 2002:adf:dbc1:0:b0:224:fc61:fef7 with SMTP id e1-20020adfdbc1000000b00224fc61fef7mr13001613wrj.312.1661987696035; Wed, 31 Aug 2022 16:14:56 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:55 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 12/17] submodule--helper: fix obscure leak in module_add() Date: Thu, 1 Sep 2022 01:14:19 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix an obscure leak in module_add(), if the "git add" command we were piping to failed we'd fail to strbuf_release(&sb). This fixes a leak introduced in a6226fd772b (submodule--helper: convert the bulk of cmd_add() to C, 2021-08-10). In fixing it move to a "goto cleanup" pattern, and since we need to introduce a "ret" variable to do that let's also get rid of the intermediate "exit_code" variable. The initialization to "-1" in a6226fd772b has always been redundant, we'd only use the "exit_code" value after assigning the return value of pipe_command() to it. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 170eaf010ac..675dc3f16a3 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -3296,6 +3296,8 @@ static int module_add(int argc, const char **argv, const char *prefix) N_("git submodule add [] [--] []"), NULL }; + struct strbuf sb = STRBUF_INIT; + int ret = 1; argc = parse_options(argc, argv, prefix, options, usage, 0); @@ -3345,21 +3347,17 @@ static int module_add(int argc, const char **argv, const char *prefix) die_on_repo_without_commits(add_data.sm_path); if (!force) { - int exit_code = -1; - struct strbuf sb = STRBUF_INIT; struct child_process cp = CHILD_PROCESS_INIT; cp.git_cmd = 1; cp.no_stdout = 1; strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing", "--no-warn-embedded-repo", add_data.sm_path, NULL); - if ((exit_code = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) { + if ((ret = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) { strbuf_complete_line(&sb); fputs(sb.buf, stderr); - free(add_data.sm_path); - return exit_code; + goto cleanup; } - strbuf_release(&sb); } if(!add_data.sm_name) @@ -3374,15 +3372,17 @@ static int module_add(int argc, const char **argv, const char *prefix) add_data.progress = !!progress; add_data.dissociate = !!dissociate; - if (add_submodule(&add_data)) { - free(add_data.sm_path); - return 1; - } + if (add_submodule(&add_data)) + goto cleanup; configure_added_submodule(&add_data); + + ret = 0; +cleanup: free(add_data.sm_path); free(to_free); + strbuf_release(&sb); - return 0; + return ret; } #define SUPPORT_SUPER_PREFIX (1<<0) From patchwork Wed Aug 31 23:14:20 2022 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: 12961517 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D656DECAAD1 for ; Wed, 31 Aug 2022 23:15:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232658AbiHaXPr (ORCPT ); Wed, 31 Aug 2022 19:15:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232651AbiHaXPL (ORCPT ); Wed, 31 Aug 2022 19:15:11 -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 0209920F4E for ; Wed, 31 Aug 2022 16:14:58 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id ay39-20020a05600c1e2700b003a5503a80cfso429725wmb.2 for ; Wed, 31 Aug 2022 16:14:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=Mwib3DNjDZg7XvybHwUOZcR9r4G7HN5s+CcQsOj+xxY=; b=P4V24/LJrRtwEFovzkA+YLQcfvVjHDJok0C/dWf5Rpxz7Yj2hNPbm1IzLENFwbJmRD 3AD31HaJjBwtmk7wsSW0AN2NZNrUBffWYkn7156QRmBxyrDrMa3bj1fzievmEZpdOj9j 9r4osWJy9XYOUiLt239eEoEE6mrqQejN5baE3Cod53VXLYZzdMY8ab1tA+uC16MMuyHu FSeWiLy+DOtbAYj+lh50p10q1/uTeFX6RAEuO9j7jKJp1o7W72Jfq9d1NoTMnlzBhm1M J9P9foJsC04UBQu3rN3oSYaUmcUJP69MF/mdt2hs2ddt+bFzpGPT/xHgvwtH7RdvZ+QA bW6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=Mwib3DNjDZg7XvybHwUOZcR9r4G7HN5s+CcQsOj+xxY=; b=SnXXE1v+EW5dN9YzVVDsiKn5myrrLRE1kzLZQ0ez87gBlkFgj6DZi0+m6GtMvCDtvM 01ZfqP2plQYya8Z8lucQ0ZZxD3q25pIWd66SElowMVJmI4MFkj2y7aLdBBzLmgFCSNKW 59AFCgNUPkWNBXFIpBpyrGaoHM0FVazhc9vnMRtGyMCYVhR4oqwhz0uyk2SfaDokWtAS 3y7F0jZmN+9hTkagz4wjqEWpBUJ/aLmc4YwrU3Xrb1bjwDnqVbpdORCGmoNoh7xYdOvp 5J2LfXaUD2QAWi+oPqyKshRcQUL5HbaaOAUmPtVJqdFPiZAZfoWJGnU8gR9WqcJWNOgc Q5mg== X-Gm-Message-State: ACgBeo2wm8Dro5b4oEJwumXcPlpG0A7aqJ9cg6zxBY+zEh6FTBC2F6+G 6DZ75L64J6do1zYBima7roFHH0MftNozlQ== X-Google-Smtp-Source: AA6agR5iJraMH1THcuCDofuX7UN3vHFuwPvd2Km0NQizDiB0NxoJAyLFJ1NWzjTR0f6ZO2qeHuhE2Q== X-Received: by 2002:a05:600c:2853:b0:3a8:3d66:2653 with SMTP id r19-20020a05600c285300b003a83d662653mr3375456wmb.156.1661987697240; Wed, 31 Aug 2022 16:14:57 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:56 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 13/17] submodule--helper: fix a leak in module_add() Date: Thu, 1 Sep 2022 01:14:20 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a leak in module_path(), since a6226fd772b (submodule--helper: convert the bulk of cmd_add() to C, 2021-08-10), we've been freeing add_data.sm_path, but in this case we clobbered it, and didn't free the value we clobbered. This makes test 28 of "t/t7400-submodule-basic.sh" ("submodule add in subdirectory") pass when we're compiled with SANITIZE=leak.. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 675dc3f16a3..7c62916da3c 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -3317,8 +3317,12 @@ static int module_add(int argc, const char **argv, const char *prefix) else add_data.sm_path = xstrdup(argv[1]); - if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) - add_data.sm_path = xstrfmt("%s%s", prefix, add_data.sm_path); + if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) { + char *sm_path = add_data.sm_path; + + add_data.sm_path = xstrfmt("%s%s", prefix, sm_path); + free(sm_path); + } if (starts_with_dot_dot_slash(add_data.repo) || starts_with_dot_slash(add_data.repo)) { From patchwork Wed Aug 31 23:14:21 2022 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: 12961518 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80CCAECAAD1 for ; Wed, 31 Aug 2022 23:16:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232773AbiHaXP5 (ORCPT ); Wed, 31 Aug 2022 19:15:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232670AbiHaXPN (ORCPT ); Wed, 31 Aug 2022 19:15:13 -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 29138BC95 for ; Wed, 31 Aug 2022 16:15:00 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id n17so20125542wrm.4 for ; Wed, 31 Aug 2022 16:15:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=9rTRwJ+AE9rbWIGNQ76h4RR+VhmupN0i+3XMiMJWDBM=; b=fwScGzepvxMDM6of0qgv4TzMedXuZ1mlqTV3QZr4RkgKvcoaKZZvJa+QKdzjgpB7fE lXUy69vI906pFr4Rtz7dq2XL1VAMTfY63EOhD9qDoBn1bjXvI3G+d2Kjal48Fw06kl5O PkR6YOn28HmniIuU8dqpvOaiVeG+645+s3lqH9QKczn1X2aPxErGIVlQyqu5y6Bywkhl OPROR3jVN9g9I6VcgvMIU97+HfXBbPJ/FJAqb1Aii/2zKq8XLnkXrCFT9KC5KpnrBfeN 9NMv1murRZ9bX1Lcax1pOPUBeyRje6UdKPKOTiTiUG+PqRmy0zE7UMZpyQeqLBTFTo7r fuCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=9rTRwJ+AE9rbWIGNQ76h4RR+VhmupN0i+3XMiMJWDBM=; b=LdkpKbOIj3BxDvda6kzzje5B41denfT/tecAxeG3u1fXswzVO1LjQiR6bqgRXE2k9w Ncov6ARSVhYZkohxUFwZ4OllIlbgzV2xY14k2VaDmYzYmRegEJK06OV/rktKm0yl5lHU CAyCriChA8poWoEMVp41rJaG5Z7JullHJ7Eurwvb09269NhzmlFGJrOejkG4dGyrJH8X qzsRZHMskB3uCGK3YKt2T4dII87rhZMgfzgfqIcXQ7raJXJ+3kQ5SiZuy+K6elk+q6Wf RzS0WaZO0SvW+bHwn8N8Q9pOhv+dMgeXoR7AfTZvjtFeEI2rjcfF9M8MAzLcoVoYX0Y5 7LZA== X-Gm-Message-State: ACgBeo2ZHHLxLG8EcegfZJeHQQ7DK7OtL9T/vg1ksrPpLfhh4C4zF5u3 C+SJOH44pHiCq7luorpYm8Giz7GtHixI7g== X-Google-Smtp-Source: AA6agR6poOCCu9WrLLZQGWILMISsye5uUeZLkqcYWYx6HO3PhbcB7PkvSwOYTMUZWRC1M5UBHH2GSA== X-Received: by 2002:a5d:6488:0:b0:225:f99:338b with SMTP id o8-20020a5d6488000000b002250f99338bmr13327815wri.638.1661987698180; Wed, 31 Aug 2022 16:14:58 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:57 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 14/17] submodule--helper: fix a memory leak in print_status() Date: Thu, 1 Sep 2022 01:14:21 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a leak in print_status(), the compute_rev_name() function implemented in this file will return a strbuf_detach()'d value, or NULL. This leak has existed since this code was added in a9f8a37584a (submodule: port submodule subcommand 'status' from shell to C, 2017-10-06), but in 0b5e2ea7cf3 (submodule--helper: don't print null in 'submodule status', 2018-04-18) we added a "const" intermediate variable for the return value, that "const" should be removed. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 7c62916da3c..23c9e28deaa 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -583,10 +583,11 @@ static void print_status(unsigned int flags, char state, const char *path, printf("%c%s %s", state, oid_to_hex(oid), displaypath); if (state == ' ' || state == '+') { - const char *name = compute_rev_name(path, oid_to_hex(oid)); + char *name = compute_rev_name(path, oid_to_hex(oid)); if (name) printf(" (%s)", name); + free(name); } printf("\n"); From patchwork Wed Aug 31 23:14:22 2022 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: 12961520 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA8BDECAAD5 for ; Wed, 31 Aug 2022 23:16:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232790AbiHaXQD (ORCPT ); Wed, 31 Aug 2022 19:16:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232599AbiHaXPP (ORCPT ); Wed, 31 Aug 2022 19:15:15 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47E1725C48 for ; Wed, 31 Aug 2022 16:15:01 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id m16so20097330wru.9 for ; Wed, 31 Aug 2022 16:15:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=oeZNjQ57DkDPQMEZahF9BONdvx2gWqe1N4RLb4YPUfU=; b=M2BNKDPorQG8YhP7hgEcVIxea7SN0VvdWMZ+9WCR/QJOg/dcncNGczMS2gu9Ig3p51 7C5kEsJhxzNQzpCSAOt/zTQps2LX/nMSLVhNsKlZq2/MlNviWsv1SZpqQENqNpU4uOJs YozkCBQWjn5ZdIL+oqk7yoWUxpNLfswTWnm8A/1bTd5GFyMOw2FPFci+TyPJ0YhBkz39 48zMwgqxJqdyNYrazdXe/Cv1hgxTGs8K6FTWLoKHV7sQFWbJNSnQESlDT0YrlFqNzb1q t1HGO57TMa8hx/xvXjwj+DY43cjC6X0sEL5gGlX9s1+tp30md4Yxa/nflyEjY1l98lhb LdQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=oeZNjQ57DkDPQMEZahF9BONdvx2gWqe1N4RLb4YPUfU=; b=TKBtJbNh6iF3GvKBA1QC3XKxDOwsw9CL1NPrlFBPmCLLYp1Q2aX2ixmyy27Wrlj7zz /yxEaR71Fi20l8fiy1xlof2EN1LFnf9HYVv1/N1AcnGeMhIj6VVBOxIv4YpmS8m5MaSI 8eYnI8VQiADFb11EmWRBSCrqjoyMIoEwDyzqVRN/fJD0N9qY9zet/l6ozzrFbDD/wPcX mXxGUZtZ3Y3cwY5Sb09qEFlQpCZGCnlhXMh9j8HE0/iJLydc08GgntX2BwWAqu4Q1bKp X1iOu66LThd30mvDamWjNF9HtjHVrFm0bmrsFmGiR8V2107j1IvrxHoLl39jthsAE4RS Mc5g== X-Gm-Message-State: ACgBeo1ZAezFgekUg66ymZaHvVypkCEqN4hnBH+Xzu1Dj6jxYr7XMEcr xDui7U63Jf70ZLNL0SVdy+2QxJBsoLTW2w== X-Google-Smtp-Source: AA6agR5iJ9eLIeylF9ax/G9qQ4nHdJDymYCy+JuIXrO7ABnoFROCkrISGvX3Ke81f5qbYZrqUUBrDA== X-Received: by 2002:a05:6000:1f18:b0:226:eacb:2d77 with SMTP id bv24-20020a0560001f1800b00226eacb2d77mr3294728wrb.0.1661987699447; Wed, 31 Aug 2022 16:14:59 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:58 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 15/17] submodule--helper: free some "displaypath" in "struct update_data" Date: Thu, 1 Sep 2022 01:14:22 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Make the update_data_release() function free "displaypath" member when appropriate. The "displaypath" member is always ours, the "const" on the "char *" was wrong to begin with. This leaves a leak of "displaypath" in update_submodule(), which as we'll see in subsequent commits is harder to deal with than this trivial fix. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 3 ++- t/t2403-worktree-move.sh | 1 + t/t7412-submodule-absorbgitdirs.sh | 1 + t/t7419-submodule-set-branch.sh | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 23c9e28deaa..94ebd8ea38e 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -1880,7 +1880,7 @@ static void submodule_update_clone_release(struct submodule_update_clone *suc) struct update_data { const char *prefix; - const char *displaypath; + char *displaypath; enum submodule_update_type update_default; struct object_id suboid; struct string_list references; @@ -1918,6 +1918,7 @@ struct update_data { static void update_data_release(struct update_data *ud) { + free(ud->displaypath); module_list_release(&ud->list); } diff --git a/t/t2403-worktree-move.sh b/t/t2403-worktree-move.sh index a4e1a178e0a..1168e9f9982 100755 --- a/t/t2403-worktree-move.sh +++ b/t/t2403-worktree-move.sh @@ -2,6 +2,7 @@ test_description='test git worktree move, remove, lock and unlock' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh test_expect_success 'setup' ' diff --git a/t/t7412-submodule-absorbgitdirs.sh b/t/t7412-submodule-absorbgitdirs.sh index 1cfa150768d..2859695c6d2 100755 --- a/t/t7412-submodule-absorbgitdirs.sh +++ b/t/t7412-submodule-absorbgitdirs.sh @@ -6,6 +6,7 @@ This test verifies that `git submodue absorbgitdirs` moves a submodules git directory into the superproject. ' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh test_expect_success 'setup a real submodule' ' diff --git a/t/t7419-submodule-set-branch.sh b/t/t7419-submodule-set-branch.sh index 3b925c302fc..96e98423214 100755 --- a/t/t7419-submodule-set-branch.sh +++ b/t/t7419-submodule-set-branch.sh @@ -9,6 +9,7 @@ This test verifies that the set-branch subcommand of git-submodule is working as expected. ' +TEST_PASSES_SANITIZE_LEAK=true TEST_NO_CREATE_REPO=1 . ./test-lib.sh From patchwork Wed Aug 31 23:14:23 2022 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: 12961519 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9BD0FECAAD1 for ; Wed, 31 Aug 2022 23:16:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232673AbiHaXQA (ORCPT ); Wed, 31 Aug 2022 19:16:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232438AbiHaXPO (ORCPT ); Wed, 31 Aug 2022 19:15: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 41D311275B for ; Wed, 31 Aug 2022 16:15:01 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id k6-20020a05600c1c8600b003a54ecc62f6so425097wms.5 for ; Wed, 31 Aug 2022 16:15:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=l6vfCrlMMcS38yTxJXWM2QQyvGXQ4csCQID3UHfoRxc=; b=UuigeML0snWmPu30DrIrsTlxM5MgTYj6rLUcessrouzRpkvrfZKFDYEQd69ONygm8E HvKOU1BqoUq+t7/R7bCsBfDgtYVEW9Egpksm6w2IJMMYzRgetlnY0yLtVqlSeOF984IQ G5GY9hy7xe9D9wMzYmM7BisLjoX55pOhkJYHcetdDpdfXbUrbhi1vpDhi6YATFD0q6/k Q+9pY+L5v4PkUkYJ2VFLcbl2Tim9cH1wYlctp3+YxUOWwsZRHZYxRsb84YEVfRkuQkN4 6yiPp6D/Wbr5o6onNo1/nbWdBmNYMQZtBZjVK1RG3qJxRd5bcoyq9D2e3c2sHDaOrQk6 t5DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=l6vfCrlMMcS38yTxJXWM2QQyvGXQ4csCQID3UHfoRxc=; b=Wnd+hE/KwSdvADcVHE93DJoes0Lfq5UWCx3tatgRT9d4WoXcGZsp0trK6tT9QT3icC YqtZb0VSWJJhrSY5IaPPz89kyGWrpcu8u3hbcP23UCYYDlB348ZOeC6nS2VMrds0J1mL /McHzFbjiJLYnKfJ+amtkBr0ZJk3VCrHcoKZuInVrFz6DJzHdBxLKodCMm3+1zVvyv3+ vaAjovyF5i0Iuu7dxYctmzHWlMiSb/BG4MH9ywX9HXgy9DTuyf8YqE5itlCTqq2VS76i CE+SrVixwEODvJEWkzMRawv/m/2jI2Vg7cQYSbjU9heE5luG1E3jp7gUk9BOGoilqMh7 KpTg== X-Gm-Message-State: ACgBeo105pGz4Gt9DW22gTn+12pDzqrarYlD7GsFZhqJYphkYKS4jT3S U7K2uxk1wSxi9Qa30D4F5YghtnBukFpyHQ== X-Google-Smtp-Source: AA6agR4y+M/csMcjUpAVw8/u96Jvtkz2kGKTgv3ZkVWU0NrQL6MG8MVbBgS8jarYHZapxxzo8cBvMw== X-Received: by 2002:a1c:3584:0:b0:3a5:fb0e:102e with SMTP id c126-20020a1c3584000000b003a5fb0e102emr3405244wma.105.1661987700495; Wed, 31 Aug 2022 16:15:00 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.14.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:14:59 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 16/17] submodule--helper: free rest of "displaypath" in "struct update_data" Date: Thu, 1 Sep 2022 01:14:23 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a leak in code added in c51f8f94e5b (submodule--helper: run update procedures from C, 2021-08-24), we clobber the "displaypath" member of the passed-in "struct update_data" both so that die() messages in this update_submodule() function itself can use it, and for the run_update_procedure() called within this function. Fix a leak in code added in 51f8f94e5b (submodule--helper: run update procedures from C, 2021-08-24). We'd always clobber the old "displaypath" member of the previously passed-in "struct update_data". A better fix for this would be to remove the "displaypath" member from the "struct update_data" entirely. Along with "oid", "suboid", "just_cloned" and "sm_path" it's managing members that mainly need to be passed between 1-3 stack frames of functions adjacent to this code. But doing so would be a much larger change (I have it locally, and fully untangling that in an incremental way is a 10 patch journey). So let's go for this much more isolated fix suggested by Glen. We FREE_AND_NULL() the "update_data->displaypath", the "AND_NULL()" part of that is needed due to the later "free(ud->displaypath)" in "update_data_release()" introduced in the preceding commit Moving ensure_core_worktree() out of update_submodule() may not be strictly required, but in doing so we are left with the exact same ordering as before, making this a smaller functional change. Helped-by: Glen Choo Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 94ebd8ea38e..1650bf0070b 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2487,13 +2487,6 @@ static int update_submodule(struct update_data *update_data) { int ret; - ret = ensure_core_worktree(update_data->sm_path); - if (ret) - return ret; - - update_data->displaypath = get_submodule_displaypath( - update_data->sm_path, update_data->prefix); - ret = determine_submodule_update_strategy(the_repository, update_data->just_cloned, update_data->sm_path, @@ -2599,7 +2592,15 @@ static int update_submodules(struct update_data *update_data) update_data->just_cloned = ucd.just_cloned; update_data->sm_path = ucd.sub->path; + code = ensure_core_worktree(update_data->sm_path); + if (code) + goto fail; + + update_data->displaypath = get_submodule_displaypath( + update_data->sm_path, update_data->prefix); code = update_submodule(update_data); + FREE_AND_NULL(update_data->displaypath); +fail: if (!code) continue; ret = code; From patchwork Wed Aug 31 23:14:24 2022 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: 12961521 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E46DAECAAD4 for ; Wed, 31 Aug 2022 23:16:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232689AbiHaXQF (ORCPT ); Wed, 31 Aug 2022 19:16:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37538 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232680AbiHaXPP (ORCPT ); Wed, 31 Aug 2022 19:15:15 -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 8590C286F3 for ; Wed, 31 Aug 2022 16:15:03 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id u18so7457197wrq.10 for ; Wed, 31 Aug 2022 16:15:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=m2wIJ+eMldhmzxDpylakTSu8nlttM2dDEYwKo5tiRdA=; b=a61+gZ42Sj2RGR1Qef7dgcrR906oS4bkmRERnxBPTe2wDy/T4E1naMoSQu2cl34U+H yHdnhMWzqFlhy9q/evHHyITsz+TVPYs0RaInymouGdN+4q9sKQ4EFu4agudTYuzh0dap aLV3cSBaia8HGMYPIHq6x8ODh1r13pyQ1OjcV/S3DX5+gTy3uRLSIvbzVc/BjpRFyfIF I3RkgVdSSfAzZjZJ0NczaEOodegad0dvGVjNb1zS1BwyUmWqGg7WYFZT1skgJ2YhMXcJ /wDl4KATOOCL3Lwq0SCi+W1fGhZZDkvUop54a+g97d3R5QVU4tuF1t40VuHf4hBR3g1i 2gvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=m2wIJ+eMldhmzxDpylakTSu8nlttM2dDEYwKo5tiRdA=; b=QVtXVhUn3xjpYbnNO3jVk57ez0HR5xfgCpFt4OhXtpBBcUmtACSNYmizlle7XXvNSK 4Gn8/EmJzv5BVDIYbj9W0BlIPMUlyJ+ZonQ9fEf+CES1c+Kn9nMeMeJSyix+QzFoRRq4 WGefiIX6FjhJRjKCoGn+xeUtgeshkBcLuE6h45wSEJibgnPRESeX0bcYeJxtYEjbTQiF UoK72Toj6o2b2cmG3ESaCtpXi4T6Fku0gjrsw1XI69bSYMhViBXg1xk9jNidRYYDRKEG Yhx2X3IE4to0DJS7fVF7u58bkEgiHcaqL52/lq6DhtIgHMZa3PQ6MA3UbHnZEcOJWb6H L+nA== X-Gm-Message-State: ACgBeo08/ffpGH99zLbpJu9vvdFA88odX10jkq885siHXPETDiKtJ4VO BmtowgvZQeBTVGtR3y2RlK3OB2FEQ7d0JA== X-Google-Smtp-Source: AA6agR6NOmSCQmKIVazAqBrSFYAaxDkt9/CYlEgXEAiu771c/f4vCerF9Rl2xze+ybVJ7d/sqYOx3g== X-Received: by 2002:a5d:5847:0:b0:220:9a9c:d819 with SMTP id i7-20020a5d5847000000b002209a9cd819mr12619606wrf.643.1661987701587; Wed, 31 Aug 2022 16:15:01 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o14-20020a05600c058e00b003a5ad7f6de2sm3484666wmd.15.2022.08.31.16.15.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 16:15:00 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Atharva Raykar , Prathamesh Chavan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v7 17/17] submodule--helper: fix a configure_added_submodule() leak Date: Thu, 1 Sep 2022 01:14:24 +0200 Message-Id: X-Mailer: git-send-email 2.37.3.1420.g76f8a3d556c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix config API a memory leak added in a452128a36c (submodule--helper: introduce add-config subcommand, 2021-08-06) by using the *_tmp() variant of git_config_get_string(). In this case we're only checking whether the (repo|git)_config_get_string() call is telling us that the "submodule.active" key exists. As with the preceding commit we'll find many other such patterns in the codebase if we go fishing. E.g. "git gc" leaks in the code added in 61f7a383d3b (maintenance: use 'incremental' strategy by default, 2020-10-15). Similar code in "git gc" added in b08ff1fee00 (maintenance: add --schedule option and config, 2020-09-11) doesn't leak, but we could avoid the malloc() & free() in that case. A coccinelle rule to find those would find and fix some leaks, and cases where we're doing needless malloc() + free()'s but only care about the key existence, or are copying the (repo|git)_config_get_string() return value right away. But as with the preceding commit let's punt on all of that for now, and just narrowly fix this specific case in submodule--helper. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/submodule--helper.c | 4 ++-- t/t7413-submodule-is-active.sh | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 1650bf0070b..37b6db5cbb4 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -3160,7 +3160,7 @@ static int config_submodule_in_gitmodules(const char *name, const char *var, con static void configure_added_submodule(struct add_data *add_data) { char *key; - char *val = NULL; + const char *val; struct child_process add_submod = CHILD_PROCESS_INIT; struct child_process add_gitmodules = CHILD_PROCESS_INIT; @@ -3205,7 +3205,7 @@ static void configure_added_submodule(struct add_data *add_data) * is_submodule_active(), since that function needs to find * out the value of "submodule.active" again anyway. */ - if (!git_config_get_string("submodule.active", &val)) { + if (!git_config_get_string_tmp("submodule.active", &val)) { /* * If the submodule being added isn't already covered by the * current configured pathspec, set the submodule's active flag diff --git a/t/t7413-submodule-is-active.sh b/t/t7413-submodule-is-active.sh index ede6f02dbd5..4dc7d089423 100755 --- a/t/t7413-submodule-is-active.sh +++ b/t/t7413-submodule-is-active.sh @@ -9,6 +9,7 @@ This is a unit test of the submodule.c is_submodule_active() function, which is also indirectly tested elsewhere. ' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh test_expect_success 'setup' '