From patchwork Thu Jul 28 16:29:55 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: 12931570 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 5ACE6C04A68 for ; Thu, 28 Jul 2022 16:30:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231966AbiG1QaZ (ORCPT ); Thu, 28 Jul 2022 12:30:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230049AbiG1QaV (ORCPT ); Thu, 28 Jul 2022 12:30:21 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 298AD64C7 for ; Thu, 28 Jul 2022 09:30:19 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id l22so2891628wrz.7 for ; Thu, 28 Jul 2022 09:30:19 -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; bh=d8q7zEFl8mKBKagMnt3ypvC+lSzqbRASaEJBkaJS/p0=; b=YlEKpNbsmA+tCFxMTeXHsebaIrldNZZYkZkhkc3FpU4h9iZ1Z4U2exJ8HfDCnOMyme 6jDwqRgkIGnaRIm7YvOEsFmyvoQnjOm+VJ1yQ4SQClJiL3wCRGRK/E7FarnHQGutwgLq QlPwj2jhTVTR5nOjEI8uXWohNfSCVbIiYrCldAm0cwRPHV+9Fzx6MPwhdmuDxJTPsWiY vV6MEbASoNsF/tpTOCwI9q9/p2egiWz19RgjEZwxoMBOh4iiQlveJ5Vq61tELEpJMuYf cQbBU+KrFjHBk4HGpX7DFA3wfe0TerTfhbQ1PJRGFAGcjFLrlQZeNl5B5rfh3XRZhbTt P5SA== 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; bh=d8q7zEFl8mKBKagMnt3ypvC+lSzqbRASaEJBkaJS/p0=; b=sQfrTCE3x3i6VZVLyRfUKcMEZR1+LvaVmOJGchlb94tgD+FO7t6aOq6YaYaXUf38LW fwQ47QJrRcf6gE1qRirF8TQkyi2JAdgeUPsFWVkyTw+xJ3PbBJwuVnWz/HUpzLoCaxAz rnDvSY8Fk0JOxcUYtBAGcHiw0f/aionkMCLsZuOVVN50Hl7fVyXEFgZvqrPYJ49RKBH5 9VlFPiCrmerF5Xg43Rj6krAx9s+kk8ritTd/6ZdD8dSJqFJn8+Ph4GGnjAODNNBD56I7 loyT5xZnkz8naKjmvpPz1O3Mer8WInUA4GnDd0Oilgr7leYiVoI8U2RD5viEjGcCH5CR OM6A== X-Gm-Message-State: AJIora+UpDT72YYMMtxoSCYEQhenvGfhkV7RASM9becb66xePcc3CCNO jZNJ6jYqqIdB/yNLFJWVz8k73DN/Sj0IXg== X-Google-Smtp-Source: AGRyM1vxstP0ielTtkxAkrcsSDVIGP0azyXYy3XAHifKItgdyO+XouOsJ1xBHuWXReN0EFm9phr11g== X-Received: by 2002:a05:6000:1241:b0:21e:f1e1:2ca2 with SMTP id j1-20020a056000124100b0021ef1e12ca2mr3049779wrx.122.1659025817165; Thu, 28 Jul 2022 09:30:17 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:15 -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 v4 01/17] submodule--helper: fix a leak in "clone_submodule" Date: Thu, 28 Jul 2022 18:29:55 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 22c79353bb0..d3dd47588d5 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -1572,13 +1572,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 " @@ -1663,6 +1662,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 Thu Jul 28 16:29:56 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: 12931572 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 6D5E3C04A68 for ; Thu, 28 Jul 2022 16:30:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232255AbiG1Qa3 (ORCPT ); Thu, 28 Jul 2022 12:30:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230092AbiG1QaW (ORCPT ); Thu, 28 Jul 2022 12:30:22 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39B02654D for ; Thu, 28 Jul 2022 09:30:20 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id i10-20020a1c3b0a000000b003a2fa488efdso610581wma.4 for ; Thu, 28 Jul 2022 09:30:20 -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; bh=QN5g+Fa5PM3RH4LwhXA74J0vuej8lFThEevT2870LC4=; b=bT0Dsc8LwVqlCmb6kEkfa9RGBA1+ySNeA1rVk+vnw4Yeb0SwvLj+3p8PfUvWH3WuHk PAPsQu/vZvCwFplGFHcflM+MxMoyBP2xLIzKXxEozkFT5PXQYT9A85+BwHotqt7MUZbx TxX/RMA125Yuwirg7P/OqDtKOLBpPqUpQ8Pmo+L1JjroFY9cqhzI57mZHQzxt76nPfsl XdeP9MkYBvthW70bCp5+jZ8rCl4/Vj+68pc0KKaByB8ktHVBdOlRBL+sqQpzL1fDcwec MdP8KOsURvmlFn5PbZOv3GsyhmBDLj3TvzNNdFwdtSFcbPuj+cZeG/tOYJYmW/ryr2s3 6dvw== 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; bh=QN5g+Fa5PM3RH4LwhXA74J0vuej8lFThEevT2870LC4=; b=nLe5pqXh9Fpi2rP6UxLLfeo3D49bcydFat9NKMDu2UD9sBCDIt6+CdGGU45SBve2sp fYZ0Yl9X4EdoLonXmzr+y2gj0sD6pQfYmWyOfp7fAI+QoEed37xFHZqrKaWjyH6TG2e+ zTDw0fOd+6dBPqJmYdKgkbcmk+GRzCqDl8UaK86rZIEogPekPXp4F9jsxQKXeCZQRXi7 qJj1Z6q+Q8wLHHs6DXqT6rHRQVyaQwyN7WA8Kdj0gm4ZeRJcMMsOawQUrwhH3eQTbHFH jxNM0w5ZW+GuhRzB0Wz5MFnFAoomcMU38q9w5rGr0BL0iS75AEnGz0aoYL2B+ki4pb3e m65g== X-Gm-Message-State: AJIora+KgGfUQmjL/Ip+xVt44oelTTyB1hrjF/qx9134I3Ja0bpUANsG 7Vtqy36W/7Yp43vhwmMGZGuFGdWwVx5Hpw== X-Google-Smtp-Source: AGRyM1s1GMsu2Tp+v1Wz5ePTJxFovBGOhPLUztI2DJnPOHdkQz+FCESYEV5WgG0ugKGAnSkBEPFwGw== X-Received: by 2002:a05:600c:3ac8:b0:3a3:63ad:d53b with SMTP id d8-20020a05600c3ac800b003a363add53bmr158184wms.104.1659025818695; Thu, 28 Jul 2022 09:30:18 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:17 -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 v4 02/17] submodule--helper: fix trivial get_default_remote_submodule() leak Date: Thu, 28 Jul 2022 18:29:56 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 d3dd47588d5..d452af40d2d 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2410,6 +2410,8 @@ static int update_submodule(struct update_data *update_data, const char *branch = remote_submodule_branch(update_data->sm_path); char *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 Thu Jul 28 16:29:57 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: 12931571 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 70D1BC19F29 for ; Thu, 28 Jul 2022 16:30:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232161AbiG1Qa2 (ORCPT ); Thu, 28 Jul 2022 12:30:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230242AbiG1QaW (ORCPT ); Thu, 28 Jul 2022 12:30:22 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8B62DD8 for ; Thu, 28 Jul 2022 09:30:21 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id a11so1262885wmq.3 for ; Thu, 28 Jul 2022 09:30:21 -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; bh=rH9cgA8h1eI+KecCw1kRKi1Gaal7P1v5hKy6OJ1rv9k=; b=VAINC1RyXVQci0iHjFj1nkwCQF2Tad1JmOudjap48VlwNC+XPggRos3iPXYhOyYnMv BhlgKm2O6vVIESc1EgbuXOra+ac+NEJDQxC+H+9PZfjjGMhW5ADg/3PPa8UXuH5mgz8n NKDspskEYpLcAMG0hNFhVi/SZLYTKqim/zhI9PH7JtSq9Fuhisu4yVk4J9O8lDatmu9R oXP1yhOWMmcVXPc1xqzLCiQZt4TTJHB/kIGpDJeRt178opFHk2CRw2Y6+gpMfVLlbHm3 CQthnqfQIlQnu8D3Gm4QcfDdATIDsJdWS91uPMj3qHlkQ26a4X3ujWajqn0YztGUH2Nr LGeQ== 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; bh=rH9cgA8h1eI+KecCw1kRKi1Gaal7P1v5hKy6OJ1rv9k=; b=GRDbVECtTf8bGQ12VQr8f9Bb2NxtscOGmTR10cuTr1+vKPHfDhMJfjY9M/1pvDcQAp 0jdS/3Tdy3rDxn14o7zkeh5SJZj/U5oX6WSUYQu85ytpZDvm7qQnzfEfkxtUzQhBqNvJ AxM0fpuitWzZ/yyo8gm7KlqbzbtcR5u4Ukbrsq91DMP6tLN95CLDK5OoRp7deJAk49UD XlNNDSlD8R4OqZ4gY40eReRBYl5555ect0JP3jVhjhvPGlhJSSIrPIrXBuTm8SiMK54b o6aBTL9qIDJOJBsqCivZCp9CGgpPFrqpkJA7KNbbuiWfaPGFLLK9jJTZg3yR83nX2LMp 2XXQ== X-Gm-Message-State: AJIora/QPotAXdJ9aVv2ltxqRqWcpBNCbE5J5h/FtYvbj+ovWlNgqJmL +BU030Cd5gGX/ZBzx+bYZoTSp2P3uoURMw== X-Google-Smtp-Source: AGRyM1sHBfQBFGJRvqOwarCDIUeyh8BGNlHbx9gaDJAfP2jiovu5zVpLbzLQEG5NxQYlGSIxNyhKFQ== X-Received: by 2002:a05:600c:3399:b0:3a3:f1e:ebd8 with SMTP id o25-20020a05600c339900b003a30f1eebd8mr167571wmp.6.1659025819798; Thu, 28 Jul 2022 09:30:19 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:18 -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 v4 03/17] submodule--helper: fix most "struct pathspec" memory leaks Date: Thu, 28 Jul 2022 18:29:57 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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". 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 d452af40d2d..6c53c83a2af 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -367,7 +367,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")), @@ -379,12 +379,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; @@ -392,7 +393,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) @@ -502,7 +506,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[] = { @@ -513,12 +517,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, @@ -533,7 +538,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 { @@ -680,7 +688,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[] = { @@ -693,12 +701,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) @@ -706,7 +715,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 { @@ -1245,7 +1257,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; @@ -1259,12 +1271,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) @@ -1274,7 +1287,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 { @@ -1383,7 +1399,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; @@ -1398,6 +1414,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); @@ -1412,7 +1429,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) @@ -1422,7 +1439,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 { @@ -2516,7 +2536,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; @@ -2593,8 +2613,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) @@ -2605,8 +2625,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, @@ -2623,7 +2645,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; } @@ -2707,7 +2731,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[] = { @@ -2722,17 +2746,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 Thu Jul 28 16:29:58 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: 12931573 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 DA50FC04A68 for ; Thu, 28 Jul 2022 16:30:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232133AbiG1Qac (ORCPT ); Thu, 28 Jul 2022 12:30:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230463AbiG1QaW (ORCPT ); Thu, 28 Jul 2022 12:30:22 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC316BE2F for ; Thu, 28 Jul 2022 09:30:21 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id l22so2891887wrz.7 for ; Thu, 28 Jul 2022 09:30:21 -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; bh=/Gq1NyfgLUzAiQv6ZnsJD1oZyAik6HkPAulV6EOUzHY=; b=ef5PHp1Rze2BlgxDE4HyInZbSwqqmPLvFpKN2FfHEzHjl0ASC48bUj9LwmiSjATHeT lQtqHQwqu5d5VQJ3iLdHj1DYbdt1W4AxChrsgJLa/d/vUE8RZQ/gh31px6m8xHf76/t2 PrT7z2YxTd2fPbgohfIdE/IoU6hAK+iGM620jLng0XnRASupb3IoSwUq6qmjfMvnrg+G uh9u5yE+D4zPBiiGaAnWdcsCI2d6PPoNPYqNaRxZqDt85nR3r5NQyOeuz/XHgM2UuY1R y2LbsPNDro6MyqqWk7xNpu73xOjXH9/Tes0ghs6WoR4vVxNa+u0HE3sVKsXZ0J3Nozwi ftkA== 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; bh=/Gq1NyfgLUzAiQv6ZnsJD1oZyAik6HkPAulV6EOUzHY=; b=yxeTq/TZoP0AEp1WQsxlzqk3ZYZSvekivf00TpyZ+ojgTy3dMenW+8oko1ag+GIdzp imQvQg+z27Jj4NNtMpYayPh1QHEgUcRcMaK1IlbZy49Wfd633gxbytnY5sxicJTcjkud v9GkYqccgm45Z7NsifMrj0fC3g/70uq07mlz2rZ20b7O2P0FzrYjgelHfaaaEzHNiX8S Xat22XCj2phdjqHCN7LsZZupWEUcGwqOmYRC5/7sKI3AeiEHaRgnRm1UT7r/UWH+PLFN MoCYw2OCgnwPDs3qv9ic5xHxbG/TtNOw/gfg/Vw5nyVFU0wjQusvRP/LjE+aZ+DRYapN yq0A== X-Gm-Message-State: AJIora+KKy2oIOlWu7eTlkZSOF7RGeLQyGcWGZh3IMblRKLT/i/9Wfms olfOicpgAmOxYAKkLqbsqBHU3Mdc2+wYHA== X-Google-Smtp-Source: AGRyM1thwjuC5+m7wIvUrbJOeeZrlq9Egn8LWpnwu/LzQ+GW9Q+Oy+cRFotiI5LbR3gf1JLPXDpHJw== X-Received: by 2002:a5d:620b:0:b0:21e:5b97:c826 with SMTP id y11-20020a5d620b000000b0021e5b97c826mr17101407wru.600.1659025820969; Thu, 28 Jul 2022 09:30:20 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:20 -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 v4 04/17] submodule--helper: "struct pathspec" memory leak in module_update() Date: Thu, 28 Jul 2022 18:29:58 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 6c53c83a2af..057cb6c0d42 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2537,6 +2537,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; @@ -2625,7 +2626,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; } @@ -2648,6 +2649,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 Thu Jul 28 16:29:59 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: 12931574 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 81930C19F2B for ; Thu, 28 Jul 2022 16:30:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232261AbiG1Qae (ORCPT ); Thu, 28 Jul 2022 12:30:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229513AbiG1QaY (ORCPT ); Thu, 28 Jul 2022 12:30:24 -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 0A974DD8 for ; Thu, 28 Jul 2022 09:30:24 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id r1-20020a05600c35c100b003a326685e7cso3431577wmq.1 for ; Thu, 28 Jul 2022 09:30:23 -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; bh=+9KLAtS07Gp07j/UQuFkCrlVBUtAPSpbiQ4csnWuMec=; b=o8urLJAjXSTx2K+XajwverZkty8ZGndfxTQ2ppqVVb3B2bes4mKc1x+eQFABa0v6Bx SShZStBYqyIx7YHEYmP5nBbrYbg5IzuACvkhi7lN9TnJsAD0Iw9QC8OUFsHt9Ue0CdCH HQXgyjn+ZVUzFCoatiyHdv/v8n9y/uSv1DNVNyQ1u3Mncz70TLqTuC3SBpV1AcQ4cuE2 8TgH1s7z9oiruaQ/mSZD3X/E2x7Gc3cW6WEX/HUoih4p/tBOSPusWXx1IZy67KzECVAV i9JZeGPOBeudkTFpgeHMkbzYxNkpgQ6fMvlWMsR3ag4PaxSAdNHaF4TiXT9qY95nVG9O fLsg== 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; bh=+9KLAtS07Gp07j/UQuFkCrlVBUtAPSpbiQ4csnWuMec=; b=cSke44F+zxtkAhko5b3puljWlbw36msEsFXkREecv6zrkBl4IaA2l3qtJF7gfK8BSZ SsGvkHDNYjAOKknHwlGN/VVznpEIMB4+OzSIQpTV6uolPOgPScCk+gQU/74YI8NeruD5 7PRzAyTnJxG2Z8FG0xSLqvwyhGuXNKIVcwynZeWBLh77v9jEaT0kP3jmgptEcn9Lyk+j ABrSr7aVvX5nfz2TSkSTEB18pXQY0P4aQh+r1a/y6mxS/gzSWqEjixq5bTR09lk/wFZv LYz5rtbAI53upGcDghbyU13wGJ62/okZ6aaP64ssQqjUktyTNJzXGMeHXXjKczAKfSm0 z10g== X-Gm-Message-State: AJIora+CLx07TLuEMrCZPT6YEo4dV9EvsMYWXndBv9XhWsMwPQABPWmo ESB/1oD1m81dFa1THgDnxhvlYKTwgt5Z4Q== X-Google-Smtp-Source: AGRyM1v8qvlxOEDWpIXtJ1dVLbyQFvYziXDXlVQ08d/XkDDgNNEXx4nTfzF2HEsK+OcYqzCGafbhUw== X-Received: by 2002:a05:600c:4e94:b0:3a3:455b:a277 with SMTP id f20-20020a05600c4e9400b003a3455ba277mr145298wmq.198.1659025822235; Thu, 28 Jul 2022 09:30:22 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:21 -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 v4 05/17] submodule--helper: don't leak {run,capture}_command() cp.dir argument Date: Thu, 28 Jul 2022 18:29:59 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 057cb6c0d42..35a168009b7 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2105,7 +2105,7 @@ static int is_tip_reachable(const char *path, 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); @@ -2123,7 +2123,7 @@ static int fetch_in_submodule(const char *module_path, int depth, int quiet, str 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) @@ -2180,7 +2180,7 @@ static int run_update_command(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 (run_command(&cp)) { switch (ud->update_strategy.type) { From patchwork Thu Jul 28 16:30:00 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: 12931576 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 2752CC04A68 for ; Thu, 28 Jul 2022 16:30:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232141AbiG1Qaq (ORCPT ); Thu, 28 Jul 2022 12:30:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232030AbiG1Qa0 (ORCPT ); Thu, 28 Jul 2022 12:30:26 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F43A64C7 for ; Thu, 28 Jul 2022 09:30:25 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id v17so2878192wrr.10 for ; Thu, 28 Jul 2022 09:30:25 -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; bh=stR+OuorQBwE92QYi1w7bGFdan4hiToutrf2Fo8qupM=; b=KVx9OzW5EeH5JENqJ/pczyoyJG9xcAFaXisXfh7jaSi1GXcDkcxyykTyaoch2jtCmy 1pMVWPtY7kXXi3f2r9XOQ8gZtSljcp4rFzPPR9UNemgYAl7ay/8cyQ2jfjItAM0g9Fdh vTKP3uzLnZEcK135q6aJL6MEBddegB+ehn+dn+bPsQh2dt7+cKgGHt92hsnyZrAItB21 WodYHdj4ztLBSCLfvkeLJE0r0owRDaooYVc5pU91C6vzLHQDXr+d9G35TwXpqbWIR9hh 595i8VVc1fHVhwAKiZ1RK8gu1ux2dBD2NfZ8osd6R31X/+wnO8CwdVgj+g48GvRKDFqR dl5Q== 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; bh=stR+OuorQBwE92QYi1w7bGFdan4hiToutrf2Fo8qupM=; b=oYag9ZuIA50dIbUXqtnHAnX6eXIIPHMY5WE4Ulw9H7cc2d5wihCW488qeKwodtKqYQ C83SyNqBD9Q3+v4+KJVU/PjR8sOZhialEkmPjK1dSauLllcW1OZWMvODk+ExFr9T0aiX jzsb66+JDjFjhupbM9Ca8UpenzNuvBmy90JRiAIqde9/PNJAAOtHz9LTgvhT8+cM3PEF WntilABrkSi6kw292f2Qtj7BKnvgKOf3McIt+XgtFqlk0VNaq0Sk8AmZ1bO3wTtOju/z v8X9QcxeVM/5L4JL9EeCfC6zLWEQKz/47Wl2yN+d2oSNwQhvh9VEGRYKZkXAgNOwk3kX dTHg== X-Gm-Message-State: AJIora/9pCh9WFCY9ftLVgkpdPkt6yLKxnbBjYn4XhV9PXChrYkwXnFr B5JTlMDeAPS1XQN1W1sfcvOU61N3RqFszQ== X-Google-Smtp-Source: AGRyM1vuaq6FqWE/kVWj+U5uhKQ6kxqCriQh6VeHGizjD5+03QFz+8fVRq0kgBSAqjPRHekFuWPMNQ== X-Received: by 2002:a5d:56d1:0:b0:21e:ccc7:c595 with SMTP id m17-20020a5d56d1000000b0021eccc7c595mr5902236wrw.661.1659025823332; Thu, 28 Jul 2022 09:30:23 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:22 -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 v4 06/17] submodule--helper: add and use *_release() functions Date: Thu, 28 Jul 2022 18:30:00 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 35a168009b7..48507a6e0df 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -160,6 +160,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, @@ -222,7 +227,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; } @@ -395,6 +400,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; } @@ -540,6 +546,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; } @@ -717,6 +724,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; } @@ -1289,6 +1297,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; } @@ -1441,6 +1450,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; } @@ -1810,6 +1820,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; @@ -1848,6 +1864,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) { @@ -2530,6 +2551,7 @@ static int update_submodules(struct update_data *update_data) } cleanup: + submodule_update_clone_release(&suc); string_list_clear(&update_data->references, 0); return res; } @@ -2627,6 +2649,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; } @@ -2643,10 +2666,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); @@ -2762,6 +2787,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 Thu Jul 28 16:30:01 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: 12931575 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 753A0C04A68 for ; Thu, 28 Jul 2022 16:30:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232672AbiG1Qao (ORCPT ); Thu, 28 Jul 2022 12:30:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232093AbiG1Qa2 (ORCPT ); Thu, 28 Jul 2022 12:30:28 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56F7F13DE5 for ; Thu, 28 Jul 2022 09:30:26 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id h188-20020a1c21c5000000b003a34ac64bdfso1142175wmh.1 for ; Thu, 28 Jul 2022 09:30:26 -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; bh=3sx7UQy+f0dM276fdgJp58ZCerfns3AYz/F1d1kqems=; b=Mp9ciN6aswOHHm2QNNbvk9gsLcSjwkgozgOi4H4s12hDcOr8A1Reu/20JTaCzdukV3 2DQl8Lc4kMZuPBhTaGUvyiYzAYUoOZlbCwJk0s8+1wH4qaFQ3e4H+klRf4cKg58ixDdc V4XupNyoxtU90tOlTpLGhHWla2tcozqi7jgBgqNVj8lbmzn3VgF5+/dW/g5jt4yEYX8b Q9gWzXce7fPWyjj8ERHr3viSkxD5h1Uus7Y27r6aCTzaSTYPVuMHVSpqBO/+jaSn6zWU Z8My71bm0HC/5Pg/M/Dny7nmddmkMpLxvwM16Sl3BC4lN3rPXhsafFUoY+SbCd7mN+L+ +P+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; bh=3sx7UQy+f0dM276fdgJp58ZCerfns3AYz/F1d1kqems=; b=fGGlYr8HKZ0sWM0QAP0kyaoYzTU1u0sgXSyoo72BTx3X37Yx0NP0SZizmcjqXJ16BN qG/JCJN2/XjMDWgVF7bd0Tz1lEvDb/C+0IOgj1/RSVHEyKEHFXEL2zOQBgve8hjgoQuF MEFo136mTw2M8SUvGiRu4amK4Gm9aRd704mW6XuDaN8vQFqkvgqeKl7dXUZoO63PZPbT /KRxPkOD3Q3Uf5UK7kGRoH/BMo65nmwLThEk3K4kxhJt2VG3jeSnpGIkkpG56EcoyreZ xqcM75wkVXNQFj+GxfMUmXc18GlMKHudGKsTOjlvCOekNaEeX5AMUTEkKI1z76pgWIRQ y6Rw== X-Gm-Message-State: AJIora/81fL0AdWLf7xj2RlFeORDf5gkqbdWt9DamjR1t7vfJW74Yzca a7O+Nay92uFre+K22y+1TZA/0zBRlL6USQ== X-Google-Smtp-Source: AGRyM1u4KI0Wrz5lanIc95Eq3vCfYAowcanyeQ3YnODE6EEo/n/b9dTJ4KZHkTKMQmrTR1q+IEMwkA== X-Received: by 2002:a05:600c:3d0a:b0:3a3:561c:a9c9 with SMTP id bh10-20020a05600c3d0a00b003a3561ca9c9mr144734wmb.1.1659025824413; Thu, 28 Jul 2022 09:30:24 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:23 -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 v4 07/17] submodule--helper: fix "errmsg_str" memory leak Date: Thu, 28 Jul 2022 18:30:01 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 48507a6e0df..5cbe6a41e5f 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -960,6 +960,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 Thu Jul 28 16:30:02 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: 12931577 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 B9882C04A68 for ; Thu, 28 Jul 2022 16:30:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232635AbiG1Qas (ORCPT ); Thu, 28 Jul 2022 12:30:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232266AbiG1Qaj (ORCPT ); Thu, 28 Jul 2022 12:30:39 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A3AF165B7 for ; Thu, 28 Jul 2022 09:30:27 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id h8so2914298wrw.1 for ; Thu, 28 Jul 2022 09:30:27 -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; bh=MZraaqpo5FGcZIRHuC+Tyd8g6Z35YRaVd3RXyNti4m8=; b=IIdXjA/rTMfv+j3U5ZjvvDMyJkhOXRRPbM+5DT9Kad6FW48AlJcaYQw2Zj+XOMxRd9 Y9jqmGxiYC+xIlUB1rjFnjTRO51q5OHnLjNlbuBLg/JGlcrvWFk+Yn5O+3r2y/SYdMqD VF/1HKoNHYbg0KN7FK4MTz8RDz1yf8oDTRrWkLiZavPsnvv3i2oWo7bZFcsdRM2vuDEn 10nTdsHhRXlLu6OjzoMF6DncB2sdASCHD5ghER1CZukENaSsa8Xbm19bQHs/HTDCadAv oGFvmarVcP49vZeg0EE4NLe4hiCd72ExjlyMwM6X2LD8vl4QwWL4Dlxd/TtuWj0HyYlT GuxQ== 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; bh=MZraaqpo5FGcZIRHuC+Tyd8g6Z35YRaVd3RXyNti4m8=; b=doAEjQ9V0vGPpawk2Od9/QLgoQqLbcZJXsS5yD5drf9QTzGq7jf1zMxzKQfIXj9ooV gA9KOrcWuEArD7qCLMKjFojH6HEBfHq0PejkuyxYTZkTOWTQHJ5PQJ9tz7YZyXgehbTx cofolGA9iq6S+oqqjMJIAoPBIDvZ48gWr9Q3bh5l66in9j5wPx7eVOR3OFMOjx/uhjsK Hqas+erLum5Bi/bl5QoUD7o1YOP7qXqdZr9yKMsky586sEWWWC7hwNx2Rlp2BgrCHfcS ANwvIWJxJtD5EoMaVB7t11Z7G4NytNEQx9PUVyPyM9JeDE/+tHFHkMvUbve7kbSVHzBP kyFg== X-Gm-Message-State: AJIora+I/45wGR6PIhjWnXIunmBA6PxUIzM4ciFOjKEtzGNHjXBNJGzx nVyDBfrVKth4wMAcjpwK/v16+z3RcOblxA== X-Google-Smtp-Source: AGRyM1vZ+ylobx+zN+V/sBXv8UeueZqSS8oKsi7gTK1uDr4XtXETVqe4umhELdRWBRutVarQO9Altw== X-Received: by 2002:adf:e310:0:b0:21e:3e46:81f8 with SMTP id b16-20020adfe310000000b0021e3e4681f8mr17124932wrj.188.1659025825639; Thu, 28 Jul 2022 09:30:25 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:24 -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 v4 08/17] submodule--helper: fix "sm_path" and other "module_cb_list" leaks Date: Thu, 28 Jul 2022 18:30:02 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 fixes 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 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 5cbe6a41e5f..becf686e47a 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -735,16 +735,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; @@ -1088,6 +1106,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; } From patchwork Thu Jul 28 16:30:03 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: 12931581 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 87645C04A68 for ; Thu, 28 Jul 2022 16:30:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233190AbiG1Qaz (ORCPT ); Thu, 28 Jul 2022 12:30:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232418AbiG1Qak (ORCPT ); Thu, 28 Jul 2022 12:30:40 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4442319C39 for ; Thu, 28 Jul 2022 09:30:29 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id v5so1283292wmj.0 for ; Thu, 28 Jul 2022 09:30:28 -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; bh=NQe6PkKD628J1ciH6LcRkkH7QD3SC8650VZsGYakTms=; b=VEdpKIrEy7rOI8zg0FUdczLbAaU+zuW4uN23c83yXLK+n6wbBJW2vWn5ejeIX7zQjl L+sYP477Yw5w5e1+zNrfpnnDfna4cq1OdMQ3gBlmHrRCtnBtA2Pd3Kk4xPmDXDCvwGHh 2zZ+c52rsm7ufK2F6poWO9zSLSGhHA3mJqlHKMhVpQWeJSEhLbI9wIicE2rd3468BYR3 bev0OIKsatMzPDOGzWw6evSOzg05KiROSHTrsmcpEpbAWSlYHSR+txUr/U7glO9puZm6 zVnsnM0fx+OOPXYSlr0H5AcU6MHdvQEYlSORIT5LHGem1b1Y0cs9ByHtgc3oKX5qerrG Jzrw== 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; bh=NQe6PkKD628J1ciH6LcRkkH7QD3SC8650VZsGYakTms=; b=JSfsxb2+QFVDdCX6OmrGCOpmXQRoR237K39brRA5/TdM08pIz9VOakpEnfl6ER5G+3 vpMoT7Rhfg4wbiB7P4Va/ilPDcQobkb3F8Qemoj5gsbLLhxDYxhNVDH9Nr0mWcJ/guVb bknKuUx+hf4tlPbHRfZfr/Jkd+m0vT0/pLWWJtt6aA73KIEB/RDcGReQzmSF98846FZ5 VZAhLetm36bvFnZNs+GabuLNuArxIof32H9nB3lLCALyiqGVpBERPULGbrZ3OJgS4AJd Qi8uipejE7ilP6caDvhW//ATHe+51fwZAOFUjP6QxmJp26cjtA/N0WifiJqHtX1ylhAX wstg== X-Gm-Message-State: AJIora84FR9bryYCKwmsQ/1DRwEcSXos714ZH31JRqOZ9MsteFpjsfvz BYA49shmoFM+WWqWTCQkzZiXwex5Eo0Efg== X-Google-Smtp-Source: AGRyM1sUmu8l0FNYsjAjAGUpvKZfv2bYZ6zefEMnpZD1QFFbdo/aKEPGnagVgtlMVJnaata42FVaHw== X-Received: by 2002:a05:600c:1e03:b0:3a3:6cd6:1d38 with SMTP id ay3-20020a05600c1e0300b003a36cd61d38mr142172wmb.25.1659025826703; Thu, 28 Jul 2022 09:30:26 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:25 -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 v4 09/17] submodule--helper: fix a leak with repo_clear() Date: Thu, 28 Jul 2022 18:30:03 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 becf686e47a..dbeafd9787d 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2375,6 +2375,7 @@ static void ensure_core_worktree(const char *path) free(abs_path); strbuf_release(&sb); } + repo_clear(&subrepo); } static const char *submodule_update_type_to_label(enum submodule_update_type type) From patchwork Thu Jul 28 16:30:04 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: 12931578 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 AFDD6C19F2B for ; Thu, 28 Jul 2022 16:30:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232416AbiG1Qau (ORCPT ); Thu, 28 Jul 2022 12:30:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232417AbiG1Qak (ORCPT ); Thu, 28 Jul 2022 12:30:40 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84DFA193E3 for ; Thu, 28 Jul 2022 09:30:28 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id v17so2878549wrr.10 for ; Thu, 28 Jul 2022 09:30:28 -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; bh=b3ToLErw8PA2w00NMfhGeGmunYegTqzi6gghmbJDE84=; b=cttxuWyufPU5ePHY9pB8y4E3Qi0KOAZcoYOpK+MdBMaBrOXrfrNTnIn+v9wNuSsNfA up70fJARFPh8TNiXcpEt+/pK8TkWmCZ2/XqcsnuqfiQDZLviUgQCdzVs+WHv+LJ97ag6 xGKT62cTq4NVO9y3/jc6G6Ejs3GdOwqM9qKq1r2YdLpr+HXBJuDeHsr/fJ0Yu4KiCACN is4WkbBgJ+wTR2c5KgHNTb8RRW2iMwdO6oxn4ESEhSbNSdoTBXUK8zo70og6cqtW0v4D gj8S/2+aCTOy+VCblJ3M6FOIbiCo4qTvav1mSbyx7FXq2ns6mHizbbouvDsD+Bx5PC13 DdvQ== 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; bh=b3ToLErw8PA2w00NMfhGeGmunYegTqzi6gghmbJDE84=; b=Bh+mgkAD/dtKOGNxNxzgBTFiC8N18p4vSEUemJC1wTPYskT33EjtoIPJ2umL6yPDBk wUfHv6E1lfa6sELyslhJPOtPdDlhupPCyFtS1l8K3BznleHwgjGHUag4/Tvs5pfOAbo1 1LBX1vW20w7HCf3HdPRRd71jT2jX8+4AAhAaw183vzFC0KKN9IV1oe9TMfKEhUSe8rHV sdVVftbR0DrEwHIG4iWW6wy7v803Lf2HYQn788+Xs8oa8sAg619rVvEtgYhRkz++NOcy xXMvuG3JtWcar5rPqmjUjNcSvS4ljG6l73zbsozwVJY6+NZoSlU22knshfsL73Oiejj7 xIqg== X-Gm-Message-State: AJIora/4E6Ax/fU4j4nXlUydLkCSDjIAu6t2NbB6Ma1ghF8WWkq44rav w1mf42gk12Xgv53bSi3JzNvzTdbjU4wBFg== X-Google-Smtp-Source: AGRyM1v+6FPMI8Bfy63GWvbwWC1RAzfvMpbxui/MOyR8Rkm7WE1dhpCnI3SuLBdhahK2tUh2/IPN2Q== X-Received: by 2002:a5d:60cc:0:b0:21e:5844:578a with SMTP id x12-20020a5d60cc000000b0021e5844578amr17509581wrt.505.1659025827804; Thu, 28 Jul 2022 09:30:27 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:27 -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 v4 10/17] submodule--helper: fix a memory leak in get_default_remote_submodule() Date: Thu, 28 Jul 2022 18:30:04 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 dbeafd9787d..f8a92ecf86d 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -62,9 +62,13 @@ static char *repo_get_default_remote(struct repository *repo) static char *get_default_remote_submodule(const char *module_path) { struct repository subrepo; + char *ret; repo_submodule_init(&subrepo, the_repository, module_path, null_oid()); - return repo_get_default_remote(&subrepo); + ret = repo_get_default_remote(&subrepo); + repo_clear(&subrepo); + + return ret; } static char *get_default_remote(void) From patchwork Thu Jul 28 16:30:05 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: 12931579 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 29290C04A68 for ; Thu, 28 Jul 2022 16:30:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232568AbiG1Qaw (ORCPT ); Thu, 28 Jul 2022 12:30:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232622AbiG1Qak (ORCPT ); Thu, 28 Jul 2022 12:30:40 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2A1D1CB33 for ; Thu, 28 Jul 2022 09:30:30 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id q18so2884149wrx.8 for ; Thu, 28 Jul 2022 09:30:30 -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; bh=XcneEM0nKdf/scpDpGVDKEW1H0hdqCzZkFEx19CgGw0=; b=EDThHzWSlPtjNS+25zgdalyI4/n5ENx9U8CIlDqRx9YLI4ExKjVG+qy04bSvp8wTsb u7YRh536yXpY3ID+bvCz4dOqX5zkpM7NLzfzvLR5nmnuTkSfNw7+9NVdCN6ARpPLzQbr aKTzJ09JfgvO897QfXPJ3EJhNJ7/h33RFgNScWRT7yW8aTnQup2da5CJ0NWY3/2tM+b1 EZFHkayLW4DFF29q1KhV9pldFLQE2u4AvYXA7rfh/XSVG7lxYBpJd4+XvgIG3Bxjfwy1 3dJJB7Fo81zOCIR55h6KYBEHZtYBM0C89meLRdcXHjAvQOp71CKYUyK7hviDqCWrPnMg /7CQ== 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; bh=XcneEM0nKdf/scpDpGVDKEW1H0hdqCzZkFEx19CgGw0=; b=0VkASI+Vrcglhah6Tsl7orR1DnODcr8/mXLDMUj9OFFnCdhwEegd7XkEHOD1F+aE8e D+Fm0Y10WcNQfy1mnf5baGnmLbMTNmFiC3OBWNMUDEQQTYyJIcUnRc6pJ2RiSo1BwoXR o9B2jW8cStUlIGM2XpHdF7xtHofHZW4JNmiD4y9UwMdbZ5Z9rJBLSmD/gsAe6cQIGW90 z5wP8H2ssayEC116qQzLsFfTfRUSHLb+QNfUvWM0zkSSkn8ujsAvHN1VrVRZ4s06R8Lw 2oSUje3i3MKgsMsKuiEOEunAca3cx2v4s8Vimw2gfz/pwR1B89cVn7o7XLshEAEHRM/J vr8w== X-Gm-Message-State: AJIora8DjabxkSUmxgAHizoKUQFaL5pABsSeezO5frOwxCwbIHmOVg7h 72vXPC9OQMCY3fDotHhxLM6nuuvod4ljLA== X-Google-Smtp-Source: AGRyM1vvDq/Bnm4P9sPUk5BZ/yw2smLGhjjzPbRMAlNqaig7JQYFckMcDCIrIVv0frQIKW/Nlpr0pQ== X-Received: by 2002:a05:6000:168f:b0:21e:92d7:f4bc with SMTP id y15-20020a056000168f00b0021e92d7f4bcmr11462328wrd.101.1659025828945; Thu, 28 Jul 2022 09:30:28 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:28 -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 v4 11/17] submodule--helper: fix "reference" leak Date: Thu, 28 Jul 2022 18:30:05 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 f8a92ecf86d..debe3a4a2f5 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -1548,7 +1548,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) { @@ -1780,6 +1782,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; } @@ -3018,6 +3021,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)) { @@ -3073,15 +3077,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; @@ -3100,7 +3106,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 Thu Jul 28 16:30:06 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: 12931580 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 DAB35C19F29 for ; Thu, 28 Jul 2022 16:30:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232671AbiG1Qax (ORCPT ); Thu, 28 Jul 2022 12:30:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232140AbiG1Qan (ORCPT ); Thu, 28 Jul 2022 12:30:43 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB78E1E3CC for ; Thu, 28 Jul 2022 09:30:30 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id v5so1283391wmj.0 for ; Thu, 28 Jul 2022 09:30:30 -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; bh=YWDE+SNBa5Vb/I2QMAVgevGI6iD5ueyrzxTK+7x8vq8=; b=baIVZmR5D5s4ohxe9bWAX/2VKs76Clsfj12LkAwc5RFHMrI3YP4FcFt3ayxWiNTyxM FIDaadQnSST+hsEg/wuJdSMpWjT0NVfE8bXVQU/8aYFlY2tlXqptag0O2HZ4CPuPuIDT 1BHQ2pMzQcULkN6hFyCpP5bTQXmrjg1JSrUaun0TR8HFD6WiYGWFySilbM2j2BWEj7uq SVcDZE68xDfpD86fAtD6gO7IwyMF9rD6WWj0s7g4ZeQ49m+ZjnVJgMminGvL01S6cRCC OOTMBQ64Y+thTAGOnK6O3gwCXlChTdhWH8VTKMPEtK+XPw8u9zsyj9zlCAs3E53TBTqu +APA== 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; bh=YWDE+SNBa5Vb/I2QMAVgevGI6iD5ueyrzxTK+7x8vq8=; b=RC9ZAFavKM/+1Wx4p2C0q8dhhmHm9ILPjtQKnBukSRnbJXQGN11eutiUxDgelVcvCh aq3z7X5g6dx6L2tsbyYynZBCgvSfxSEy7+/A8M+DBbQIFcwmf2H4Xjp7C6m8L6y9RsPF a2QFKNIiIZcB2+5sYgoR5tQL7z12aBaXED5rSgQVZXiSKAXhnlBBCZIVpREogDU+vtQs Yz+NQCZmvWu5gw70DQXfF8V1CqnGHlJjSYPKaTQ/DxBG6RUekl8W6HD1dTWN1ViZsCY7 QXPiit69zV1uRxGOj4EqKtHx9tYu+0QUFDG4aXKHSDi7fN/pRFuuYc7JhbwZY1w4Oig1 UKuw== X-Gm-Message-State: AJIora/tRlIqygbZrSjMMG1C/4ZKVPptUH4jhWweYSkdDse513/VMFBb /GKwl16Yu0KVNoLcTe2vviMYK5sl/yG6lA== X-Google-Smtp-Source: AGRyM1sSlBE7sAJ8n4Dogm6CSVvZZtt9XoV5iwRcrn0PG34OOGW6sQwDa6A70NLGtVO/z6wYuShwjg== X-Received: by 2002:a05:600c:35ce:b0:3a3:1b7f:bbd8 with SMTP id r14-20020a05600c35ce00b003a31b7fbbd8mr158486wmq.22.1659025830044; Thu, 28 Jul 2022 09:30:30 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:29 -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 v4 12/17] submodule--helper: fix obscure leak in module_add() Date: Thu, 28 Jul 2022 18:30:06 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 debe3a4a2f5..0f6c07e3d1e 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -3269,6 +3269,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); @@ -3318,21 +3320,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) @@ -3347,15 +3345,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 Thu Jul 28 16:30:07 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: 12931582 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 36F48C04A68 for ; Thu, 28 Jul 2022 16:31:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233314AbiG1QbJ (ORCPT ); Thu, 28 Jul 2022 12:31:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232818AbiG1Qap (ORCPT ); Thu, 28 Jul 2022 12:30:45 -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 BDBB43121C for ; Thu, 28 Jul 2022 09:30:33 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id j29-20020a05600c1c1d00b003a2fdafdefbso1312041wms.2 for ; Thu, 28 Jul 2022 09:30:33 -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; bh=RNhvJzwdpxHKH8msFj3ks2FkMR8WWi9q5BufnSrkGKE=; b=dTyFUPzEgUOt8USofSPEqHHvssXleNMIyZDmzUljVziSO8jUk4Hn2A3qTekxYmacdP 60LiCG+X4o5yFskoW7VE3MiyUL+zEFNi4isdeS8ewPyHwVtqRzdhKWQGi459yjlan84t KD+IMy013ID6Bo7dwCFuCbgAiaFQyvwqHUWlSa4x7aGvtxi86Ig+6pETdP2an7PnlzFf OwBz3raGg5YOgBMcTNGbkEBi25i0qf3C6nwS4Y+48P5xiI/Zdp6F5UkPy3p01hSJCynU 7mL94ToK7ZyDQ7ah22CrzVTeG0JwU4Qdv4mC/LsjX1X+dsOjPg6ysMych83sikcigExu f93A== 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; bh=RNhvJzwdpxHKH8msFj3ks2FkMR8WWi9q5BufnSrkGKE=; b=qCXxnwb6bK49q5jJwXBEpgLwd25QlLraGStmpsG/yVxtkaV5kM3qGk7GiGJQjRVbgq rsaobvYF1Zvo4c/wbSxBNhPBiuQOnxVB+REWDzxudNnw4I4vZcBxt/g/4o4SatL6EcF+ 2om/yY74HI7Msxq5+MmJpDUPPqxKCNJyb0gTakAGWDIy+30ezCdfRXDB44KLGebcp5b0 dCgO9rgrL0Vwgl1S61MFL4j+O+W+zm37iaLlDUMnpf7OPVLzEQIhICG0rlYBqvx3kRy8 Ro0Lidef+HLBVgbiNt99xFjfQA++nYs1K+CLuGCVMK+F2GmhIPey85512rTIjlbl27Wp y1vQ== X-Gm-Message-State: AJIora8Y0QTN7GTltyvkXyEivzl7Wei7hyohROc8aFI7NO2gzqAaPDsD xfY/q4WSIPdZBHyC3loEUv4EfNlBUCXIWg== X-Google-Smtp-Source: AGRyM1uteTrzxaPfc2RafdbXiozy41k29UkL6QMY9TsOEX2H+NpnIKO/3bltpF1Ih8Imr+sMWCi7jw== X-Received: by 2002:a05:600c:3549:b0:3a3:16af:d280 with SMTP id i9-20020a05600c354900b003a316afd280mr144766wmq.142.1659025831274; Thu, 28 Jul 2022 09:30:31 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:30 -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 v4 13/17] submodule--helper: fix a leak in module_add() Date: Thu, 28 Jul 2022 18:30:07 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 0f6c07e3d1e..6077aecd202 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -3290,8 +3290,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 Thu Jul 28 16:30: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: 12931583 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 8D954C04A68 for ; Thu, 28 Jul 2022 16:31:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233422AbiG1QbM (ORCPT ); Thu, 28 Jul 2022 12:31:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232392AbiG1Qap (ORCPT ); Thu, 28 Jul 2022 12:30:45 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 918DA39BBE for ; Thu, 28 Jul 2022 09:30:34 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id a18-20020a05600c349200b003a30de68697so3589126wmq.0 for ; Thu, 28 Jul 2022 09:30:34 -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; bh=FSpUAMJ0iKq0Vfc5uXG+vNrC5YsYtmjkk+76h9qdYAE=; b=kiVG3ezpRgKov4eKgAruQ3U3vkLmEEMP4ktXLvLfd//PUcz2xBtvVUDYJ8YA5xPpS1 Q5sfpaetidb2yt63C2qaUXGdd1CX8vjGsG/5LxLDGRZk029HNelytRA/rb/PkE68QjZl d9bYgw4wKajEXFlg1xZmhD6C6H3dbBSlll8ki9+yILxFQ+TIjUeuxRCHRYw7tqJAw6kG RFH5YVwqcpEva9Vk7NVbQJvgUmJ1k6j0Y6+/11Jl7UPaRsCzuCyVZsKcT1yYomJrh7vL 8pOiRXV4cttW5cwC5RHrQJJLsFfFxPe/P+t214MeOu1PQAJ3o5OImo5QtlJIwa9HP2rC HJ4Q== 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; bh=FSpUAMJ0iKq0Vfc5uXG+vNrC5YsYtmjkk+76h9qdYAE=; b=nawG2WnEQSQ6Fozu1BujgkrstHbXZ+UGGpsx5vniBZdaWGxOlIgTc6JCXBgsnzYfze 6JQgAeIwUhzTiPUNPh3neiCjKDYVrxj3UsrDvRE+mbq/6/YywVysqsOmVav2sgTTrgS4 OhwmaKxP6nm5DMIgRnGtHtKy61BCdxe+MEW20Cd8De2vlIxD2W2mBgUxDM9Wzjf+rL2l UIm1J0v2Zq/HFKfksIw3IncsTqtNw53ajmJ4cYZkV6TKAomWNP0xcxXIS9NsUutXMB8o nfowGhOoMpNBUGi+3aYrNmE82cJCqVI5xdMt97RcGXfnKb1nuyXcHkLN1/ox5+JHrpsL BeMA== X-Gm-Message-State: AJIora8yFOVOkpK+6Qqc/hcsV0nOOFU4Dwz20LoxWCPjf8qKxgMST/De ENKXIFY1MEEGdJBInTxpDVJP/OZjxTW0bg== X-Google-Smtp-Source: AGRyM1vIwBmSDhRqV1zIU+l4betfZ0ZOVMbOO5cufrZiT9Zr2CA7I8Lx2o6KgUoESGf+T4K26VZIZA== X-Received: by 2002:a05:600c:ca:b0:3a3:58f6:94e2 with SMTP id u10-20020a05600c00ca00b003a358f694e2mr125594wmm.131.1659025832497; Thu, 28 Jul 2022 09:30:32 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:31 -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 v4 14/17] submodule--helper: fix a memory leak in print_status() Date: Thu, 28 Jul 2022 18:30:08 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 6077aecd202..b51b60d5c77 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -570,10 +570,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 Thu Jul 28 16:30: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: 12931585 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 A1D7EC04A68 for ; Thu, 28 Jul 2022 16:31:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231379AbiG1QbQ (ORCPT ); Thu, 28 Jul 2022 12:31:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232907AbiG1Qap (ORCPT ); Thu, 28 Jul 2022 12:30:45 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A217748EA8 for ; Thu, 28 Jul 2022 09:30:35 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id u14-20020a05600c00ce00b003a323062569so1302283wmm.4 for ; Thu, 28 Jul 2022 09:30:35 -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; bh=VmJMl0+v4ZZLPGOvQA/fP7vWGTJKtcZpXq54VGdb8Lw=; b=BbaWoA7XDoQAl27P3YHKQoSO/8l1PGPp2nlNL5UtU5xZY281G9S6A5Kxf01W2cioIP bNUswE3fmYTNkACGg74NY5ZS7QClmNJu5J3pCnDry/3g0Yxy/ar29G3NQkXTrench5To eixZsC0ML92ziUH/7QsHewvObzUAgn4D1p3750tp5jgNRZ6KMdfUyRJzsf03mgYCmagi zuUesc5TaUmrG6qgoDpNrRGFVTGxdu/28bBX6lNLls794jBnmUSgdVkJtuB6HAJ1CJdN TCpy1rNLLH6ZP3hH9aW+F/tu/tgTrWmmynqnuttnc1DXVKK3b+oISE3RKocOH0cChTlu u4PQ== 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; bh=VmJMl0+v4ZZLPGOvQA/fP7vWGTJKtcZpXq54VGdb8Lw=; b=6BRa0bMeDVQHZTmiZPzqJAfRX7O0TER7y0JQp94IXg9ZjpXJVbbDOda2wazrgYHPv2 Khj3h/C1lN+f9kRwhlzjRnNOFeeqwC8a/jtFwqa+W9h9ShxhoYJmrcO/18OE7kSGb9+2 LjKi9JALsUOEX8TsmbKGivtLaMvi5VGTZwUBBgbsoGRv/1Po++sqBEDhcJ6M5DJ5qxfv uDLs5L4XxZRTwwBjlZKqgD3EVeNj/Mk9xpGDkFYM3arA0YMwp0yThFONkaVYFLDFqB0K 4oTXSzxU3OhLjwB6OZlwIxJ+wElCUocevrKeQh2b6reujYP/c0igq6qhb7yl4SnhZJ1r vkKQ== X-Gm-Message-State: AJIora/SQkjCcwgdrHMIsI7eb7GLc97IaaonINHWvz9TEWAHXdchWmcr jnHM5ygdpx3tzdQujc0hvDTI//kmW+XKAQ== X-Google-Smtp-Source: AGRyM1sQ8c1wyitrGqTxXBBo0knto6kdtouXLCpvPA16rtYD1BdcuLKenUOjl43P3oVycgW0LX47QQ== X-Received: by 2002:a7b:cb4e:0:b0:3a2:f3b0:4233 with SMTP id v14-20020a7bcb4e000000b003a2f3b04233mr156724wmj.50.1659025833403; Thu, 28 Jul 2022 09:30:33 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:32 -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 v4 15/17] submodule--helper: free some "displaypath" in "struct update_data" Date: Thu, 28 Jul 2022 18:30:09 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index b51b60d5c77..2e39750fb06 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -1856,7 +1856,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; @@ -1894,6 +1894,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' ' From patchwork Thu Jul 28 16:30: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: 12931584 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 0025AC19F2B for ; Thu, 28 Jul 2022 16:31:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233465AbiG1QbP (ORCPT ); Thu, 28 Jul 2022 12:31:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232925AbiG1Qap (ORCPT ); Thu, 28 Jul 2022 12:30:45 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C0702316D for ; Thu, 28 Jul 2022 09:30:35 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id v5so1283530wmj.0 for ; Thu, 28 Jul 2022 09:30:35 -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; bh=H6nK1+tk2A5o/4nnlhxT77iNxS2xLnxijTm02b4meOY=; b=PxDuWuiA+/VNODGLHybpYqXDwwRHrti9eMKqoDvoTMWT1QDAL3UisedOr+MNazKBRe KArsCxXmkdd2SsxH7begVlXaLHS1dT0JQuqrQBU+zgZ5kDP11BDsXizqbEwFDLlnKZfv gb/btBhCs+mcFtRPvX3n0OCU9mdlpNR/0jJxBVhEFiRTMEmS2TOFU6p/mK7ZVrff069A HY8qgl2PDth5UB7OHXm+tsa/mHQU5ERJqCCbXqe6WmzuybSB+JXzpwpBA+rjtXMxII+m 4jsz1LL2+ChFrS8J8orvRqBFrkG9NB7ktmB+89buuIwHdVC7U66XiA8jpm3ehUmi0r30 xRSQ== 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; bh=H6nK1+tk2A5o/4nnlhxT77iNxS2xLnxijTm02b4meOY=; b=Jg9NArN1RgyDcpizcDuJDJEs/W9hbor3x4CsdyxG9BUxaQmEyEnOonhPlTdDbEGmVX slxL1QPuefXWr6WP2TtZzWp06j2lFuKSD2bd7saW+0dHDbYjPJHhGyLpwi7FkYSRHnFF AqJaUGO3FB8GFvQJxcKFg/qr4xlW1c94TBrQ66s423TZeT0S1OzhuVYthVM1awHrcO7I gbQ4pQ/nwVE3Um5KwyAK/d0P8NoBZxQQ0070FTCttI0ip4hDoI1XX4rUx8EMFHMHjMaC wMX9tIMaG9CNiCd8mNgEHFO4iQ3jVGyjogV1pAABdh9VU0hoxMJQ9dovs10Dc9M8HkiR OE4w== X-Gm-Message-State: AJIora8fUzZyoNTMsg1jI9ubUJDtRctgkYJbufxQgUyboemNFkje+zOp ifRjDYfETbYz9vnESOLdXKpeNknQCAG6wg== X-Google-Smtp-Source: AGRyM1tVh5yn8gK6VTEbGOLNoaL1A2Dlglo/7bpYTNJ2asn0aDEPmotbdqf0139WxEDX2RScZEtPAA== X-Received: by 2002:a05:600c:4fc9:b0:3a3:3537:1726 with SMTP id o9-20020a05600c4fc900b003a335371726mr164877wmq.26.1659025834642; Thu, 28 Jul 2022 09:30:34 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:33 -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 v4 16/17] submodule--helper: free rest of "displaypath" in "struct update_data" Date: Thu, 28 Jul 2022 18:30:10 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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. 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 2e39750fb06..2a171d25992 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2460,10 +2460,11 @@ static int update_submodule(struct update_data *update_data, int *must_die_on_failure) { int ret = 1; + char *to_free, *restore = update_data->displaypath; ensure_core_worktree(update_data->sm_path); - update_data->displaypath = get_submodule_displaypath( + update_data->displaypath = to_free = get_submodule_displaypath( update_data->sm_path, update_data->prefix); determine_submodule_update_strategy(the_repository, update_data->just_cloned, @@ -2537,6 +2538,9 @@ static int update_submodule(struct update_data *update_data, ret = 0; cleanup: + free(to_free); + update_data->displaypath = restore; + return ret; } From patchwork Thu Jul 28 16:30: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: 12931586 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 D50BFC19F2B for ; Thu, 28 Jul 2022 16:31:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233466AbiG1QbS (ORCPT ); Thu, 28 Jul 2022 12:31:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232295AbiG1Qaq (ORCPT ); Thu, 28 Jul 2022 12:30:46 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA57C51A02 for ; Thu, 28 Jul 2022 09:30:37 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id i10-20020a1c3b0a000000b003a2fa488efdso611118wma.4 for ; Thu, 28 Jul 2022 09:30:37 -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; bh=F0AE1Nr1A1Q4KOqDig4+oZGp4SBwgDu4v/4lq5j1fbI=; b=loVNygnn7rxncsWo3Du40G7tMcWt1TebAHd9dtY0OOecZf692iy4IKT5FpXUkTCgO/ RIy+P/WrVAP1Mq8rGkBPiFG1V/QbQqBzbxl9ToGQQH2PuPt8ecUlfs4yZsm9pO9RjoCY KZv5+PLh13p4OIJ45tR08/jfxhw27rETI/r0Bv2rS2TFVvsoHOrykvoki7mhu1jAP9vs Z+bTpLVmMO01lcy3fejpa/+KLx+HY4IYiNaX9DRBZSWJqQNEC6uFXdNmYuqgHsRWoBAH nx1AlRcOUTd9uMVyGJ3BF4XWLoH2F6Qtwoyo12Cnb25E5mWeSGDc7f3yR467ssu0g8H6 1QxQ== 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; bh=F0AE1Nr1A1Q4KOqDig4+oZGp4SBwgDu4v/4lq5j1fbI=; b=RxhK9d3z4/8SQP+OGHfJ8Iin3DkJvd3iAd6hQmeivPAJCMirBQUUnhC76KADdXuo6A GF8HHUJOmJgyHy1GDCif2Rurn9MG1dVYfiSAF5ID6p4hz2SouU4BC+ogY2q1/Ct3mjwH 8lJEoz5uEfx96AQ/FfFFua/LocH9zSx/9Vtmfr23FnEzKlaQYMhE7Mci6z1kZa2izGuk BejMprQ1aNszRSLGLDs7BBg+axF6Dobw9Fh714rTjGbDEc528ATWOPZF9G+cVhqNUNWj uzAV/VxBLOmyvFbRz0a6P+28sIH1Lqpsy8NvWnVuJKH6rdTOFg3EZj8i0kf1kqMY+HW6 KsYw== X-Gm-Message-State: AJIora9LoxiCrLyTM2nj6ubZApfZv383GqfTtSK5YuibEDVP5GTSOitB HRQxjEdsuwvjUIZavXnZhKH8zZOCIQ1a7w== X-Google-Smtp-Source: AGRyM1t2Jxt9X4BqjDsmk/tvTNmaNx703cAtTt0viodi8v0UedjxdMDLrwwYaO87hYgU6HY/FGmjTA== X-Received: by 2002:a05:600c:3ac8:b0:3a3:63ad:d53b with SMTP id d8-20020a05600c3ac800b003a363add53bmr159101wms.104.1659025835983; Thu, 28 Jul 2022 09:30:35 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c13-20020a5d528d000000b0021ef34124ebsm1492018wrv.11.2022.07.28.09.30.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 09:30:34 -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 v4 17/17] submodule--helper: fix a configure_added_submodule() leak Date: Thu, 28 Jul 2022 18:30:11 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1197.g7ed548b7807 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 2a171d25992..a3e00c9929d 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -3136,7 +3136,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; @@ -3181,7 +3181,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 9ead083371a..a3c824c3e4b 100755 --- a/t/t7413-submodule-is-active.sh +++ b/t/t7413-submodule-is-active.sh @@ -6,6 +6,7 @@ This test verifies that `test-tool submodule is-active` correctly identifies submodules which are "active" and interesting to the user. ' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh test_expect_success 'setup' '