From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-2.5 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by dcvr.yhbt.net (Postfix) with ESMTP id 279361F729 for ; Fri, 17 Jun 2022 21:48:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236107AbiFQVrr (ORCPT ); Fri, 17 Jun 2022 17:47:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230238AbiFQVro (ORCPT ); Fri, 17 Jun 2022 17:47:44 -0400 X-Greylist: delayed 1143 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Fri, 17 Jun 2022 14:47:43 PDT Received: from localhost (ec2-44-200-137-115.compute-1.amazonaws.com [44.200.137.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3E9725CF for ; Fri, 17 Jun 2022 14:47:42 -0700 (PDT) From: Tom Levy To: git@vger.kernel.org Cc: Tom Levy , Johannes Schindelin Subject: [PATCH 08/11] read_index_from(): avoid memory leak Date: Fri, 17 Jun 2022 21:27:36 +0000 Message-Id: <20220617212736.1951-1-tomlevy93@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Hi, I think there is another problem with this code: later on it refers to the 'base_path' variable, but I think it should refer to 'base_path2' if the first path did not exist. How about the following patch instead (or in addition)? Regards, Tom Levy -- >8 -- Subject: [PATCH] read_index_from(): use second path consistently, avoid memory leak In 998330ac2e7c (read-cache: look for shared index files next to the index, too, 2021-08-26), we added code that allocates memory to store the path of a second shared base index, but we never released that memory. Also, later code (e.g. the call to freshen_shared_index()) continued to use the first path even when the second path was selected. Modify the code to store the second path in the original variable (after freeing the first path), so that the later code will use the second path (and also free it). The memory leak was reported by Coverity. Co-authored-by: Johannes Schindelin Signed-off-by: Tom Levy --- read-cache.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/read-cache.c b/read-cache.c index 4df97e185e..48f2b9aa9d 100644 --- a/read-cache.c +++ b/read-cache.c @@ -2459,15 +2459,15 @@ int read_index_from(struct index_state *istate, const char *path, the_repository, "%s", base_path); if (!ret) { char *path_copy = xstrdup(path); - const char *base_path2 = xstrfmt("%s/sharedindex.%s", - dirname(path_copy), - base_oid_hex); + free(base_path); + base_path = xstrfmt("%s/sharedindex.%s", dirname(path_copy), + base_oid_hex); free(path_copy); trace2_region_enter_printf("index", "shared/do_read_index", - the_repository, "%s", base_path2); - ret = do_read_index(split_index->base, base_path2, 1); + the_repository, "%s", base_path); + ret = do_read_index(split_index->base, base_path, 1); trace2_region_leave_printf("index", "shared/do_read_index", - the_repository, "%s", base_path2); + the_repository, "%s", base_path); } if (!oideq(&split_index->base_oid, &split_index->base->oid)) die(_("broken index, expect %s in %s, got %s"), -- 2.30.2