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: AS53758 23.128.96.0/24 X-Spam-Status: No, score=-4.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by dcvr.yhbt.net (Postfix) with ESMTP id A62CA1F934 for ; Mon, 10 May 2021 20:01:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232255AbhEJUCw (ORCPT ); Mon, 10 May 2021 16:02:52 -0400 Received: from 82-64-198-250.subs.proxad.net ([82.64.198.250]:54562 "EHLO mail.lhuard.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231989AbhEJUCv (ORCPT ); Mon, 10 May 2021 16:02:51 -0400 Received: from coruscant.lhuard.fr (unknown [IPv6:2a0d:e487:15f:f3a5:290d:714a:246e:6fba]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.lhuard.fr (Postfix) with ESMTPSA id A6E46461159; Mon, 10 May 2021 22:01:12 +0200 (CEST) Authentication-Results: mail.lhuard.fr; dmarc=fail (p=quarantine dis=none) header.from=lhuard.fr DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=lhuard.fr; s=rpi3; t=1620676874; bh=E9+001qqUuNE30ZwkyynC/kEj9jmz5uyw7AfxyrHLMM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=jaHMP/UWfbMV/NNj/9Qdb6Jv6e3vrozyPIXRxtgLKStiadAxuJCSdS/Ovs1pLoI/Y JSzhAKKFdRg92ooc0C23MB7vPyWMFTxXUHoglpO5WOgqlwh4PTshsDmZPhJZUw8flq FXZToRPwcrjsLhktUMaJGRyN3bRz5UFmVtlqFkKI= From: =?UTF-8?q?L=C3=A9na=C3=AFc=20Huard?= To: git@vger.kernel.org Cc: Junio C Hamano , Derrick Stolee , Eric Sunshine , lilinchao@oschina.cn, =?UTF-8?q?L=C3=A9na=C3=AFc=20Huard?= Subject: [PATCH v2 1/1] maintenance: fix two memory leaks Date: Mon, 10 May 2021 21:59:09 +0200 Message-Id: <20210510195909.621534-2-lenaic@lhuard.fr> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510195909.621534-1-lenaic@lhuard.fr> References: <20210509221613.474887-1-lenaic@lhuard.fr> <20210510195909.621534-1-lenaic@lhuard.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fixes two memory leaks when running `git maintenance start` or `git maintenance stop` in `update_background_schedule`: $ valgrind --leak-check=full ~/git/bin/git maintenance start ==76584== Memcheck, a memory error detector ==76584== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==76584== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info ==76584== Command: /home/lenaic/git/bin/git maintenance start ==76584== ==76584== ==76584== HEAP SUMMARY: ==76584== in use at exit: 34,880 bytes in 252 blocks ==76584== total heap usage: 820 allocs, 568 frees, 146,414 bytes allocated ==76584== ==76584== 65 bytes in 1 blocks are definitely lost in loss record 17 of 39 ==76584== at 0x483E6AF: malloc (vg_replace_malloc.c:306) ==76584== by 0x3DC39C: xrealloc (wrapper.c:126) ==76584== by 0x3992CC: strbuf_grow (strbuf.c:98) ==76584== by 0x39A473: strbuf_vaddf (strbuf.c:392) ==76584== by 0x39BC54: xstrvfmt (strbuf.c:979) ==76584== by 0x39BD2C: xstrfmt (strbuf.c:989) ==76584== by 0x18451B: update_background_schedule (gc.c:1977) ==76584== by 0x1846F6: maintenance_start (gc.c:2011) ==76584== by 0x1847B4: cmd_maintenance (gc.c:2030) ==76584== by 0x127A2E: run_builtin (git.c:453) ==76584== by 0x127E81: handle_builtin (git.c:704) ==76584== by 0x128142: run_argv (git.c:771) ==76584== ==76584== 240 bytes in 1 blocks are definitely lost in loss record 29 of 39 ==76584== at 0x4840D7B: realloc (vg_replace_malloc.c:834) ==76584== by 0x491CE5D: getdelim (in /usr/lib/libc-2.33.so) ==76584== by 0x39ADD7: strbuf_getwholeline (strbuf.c:635) ==76584== by 0x39AF31: strbuf_getdelim (strbuf.c:706) ==76584== by 0x39B064: strbuf_getline_lf (strbuf.c:727) ==76584== by 0x184273: crontab_update_schedule (gc.c:1919) ==76584== by 0x184678: update_background_schedule (gc.c:1997) ==76584== by 0x1846F6: maintenance_start (gc.c:2011) ==76584== by 0x1847B4: cmd_maintenance (gc.c:2030) ==76584== by 0x127A2E: run_builtin (git.c:453) ==76584== by 0x127E81: handle_builtin (git.c:704) ==76584== by 0x128142: run_argv (git.c:771) ==76584== ==76584== LEAK SUMMARY: ==76584== definitely lost: 305 bytes in 2 blocks ==76584== indirectly lost: 0 bytes in 0 blocks ==76584== possibly lost: 0 bytes in 0 blocks ==76584== still reachable: 34,575 bytes in 250 blocks ==76584== suppressed: 0 bytes in 0 blocks ==76584== Reachable blocks (those to which a pointer was found) are not shown. ==76584== To see them, rerun with: --leak-check=full --show-leak-kinds=all ==76584== ==76584== For lists of detected and suppressed errors, rerun with: -s ==76584== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0) Signed-off-by: Lénaïc Huard --- builtin/gc.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/builtin/gc.c b/builtin/gc.c index ef7226d7bc..484fe983d3 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -1924,6 +1924,7 @@ static int crontab_update_schedule(int run_maintenance, int fd, const char *cmd) else if (!in_old_region) fprintf(cron_in, "%s\n", line.buf); } + strbuf_release(&line); if (run_maintenance) { struct strbuf line_format = STRBUF_INIT; @@ -1986,8 +1987,10 @@ static int update_background_schedule(int enable) cmd = sep + 1; } - if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) - return error(_("another process is scheduling background maintenance")); + if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) { + result = error(_("another process is scheduling background maintenance")); + goto cleanup; + } if (!strcmp(scheduler, "launchctl")) result = launchctl_update_schedule(enable, get_lock_file_fd(&lk), cmd); @@ -1999,6 +2002,9 @@ static int update_background_schedule(int enable) die("unknown background scheduler: %s", scheduler); rollback_lock_file(&lk); + +cleanup: + free(lock_path); free(testing); return result; } -- 2.31.1