From patchwork Mon Sep 7 15:04:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Blain via GitGitGadget X-Patchwork-Id: 11761215 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8266759D for ; Mon, 7 Sep 2020 15:08:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 65B60218AC for ; Mon, 7 Sep 2020 15:08:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nUEB+kXN" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730093AbgIGPH0 (ORCPT ); Mon, 7 Sep 2020 11:07:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730056AbgIGPEG (ORCPT ); Mon, 7 Sep 2020 11:04:06 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CDEDC061573 for ; Mon, 7 Sep 2020 08:04:04 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id t10so16183436wrv.1 for ; Mon, 07 Sep 2020 08:04:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:from:date:subject:fcc:content-transfer-encoding :mime-version:to:cc; bh=e+vs/mZWaSzb7y+phDgQ8niC6aslfNUt86upjnV1LOw=; b=nUEB+kXNEv6K6seMiXx6djX6mvBA7pqIwdiyxUBza3mD6EF3CwyE869OpiEHljOQsl wKWT457DRoePhQ90TKtxjljWy1mE8shGx9t93ehWvqkrChND9uWieN63q2owA9YxeWtJ bJn8NsTNpkbfFu1mMPuLc1kMDAAuoTAMbTzBYynoXbfKQDm6KVe9IMfHu7Qh35M3H3ep eVHZsX/Wul/ZxZ+nK4Homx3Rn0X3HfBFuOrq/0tsUR8Y0RYjuGz2H5B+qJhH2A3suVI7 szKS4PYy+3ulKFzQeObgauoAoah4f/yJ4piYfMYHI2ubPa+1qOvuhaa5U/SiK+JSGFWA 9u0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=e+vs/mZWaSzb7y+phDgQ8niC6aslfNUt86upjnV1LOw=; b=EmvxG9oynLdvfOUFy2Ed1x/td2bq9lXTS+b1u/M6YqANgp2kfRXxRoHQEwzKNoHCux QR+WMZ34WGBQOFBmkY+Pn4pqdap9INl4/sEJ37lwuY5CdzwQoUBgYwfuPoO0NoJSzoE6 fRR39eF3GwrsQ9bWzzEK6KosNAa/xS8fES3DkgZy3ULbOcxgFxX8QH6CrI/5qfmKqZ5f g6LEy2YCYGMfPFOMS4rRo2pT1tphfi3GB80+72LqApbeDsTgtRuEdi3aJ7Utp5yJ1xa8 OwYecGCTzTYdEb+vmnyvOg4YfwD+P1HRowVgCJZXoxZnrX7MlRI70Vvb6AMEB096vh0J BRoQ== X-Gm-Message-State: AOAM533X/2eVuo00bVbdii7fCubrGTguGjyaWL5BfzkNVya+JtK+23IL hJOB2bAHZSWb5a2hYMret1125Wm2OC8= X-Google-Smtp-Source: ABdhPJwRVnzmmmNJmWxWbIRfWva4Ief2fFOPnXs9bygRTsd3RAQyb02/H3e6isOxMWHbwDG9cg5gEw== X-Received: by 2002:a5d:5111:: with SMTP id s17mr21888146wrt.70.1599491042573; Mon, 07 Sep 2020 08:04:02 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c4sm29094631wrp.85.2020.09.07.08.04.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Sep 2020 08:04:02 -0700 (PDT) Message-Id: From: "Phillip Wood via GitGitGadget" Date: Mon, 07 Sep 2020 15:04:00 +0000 Subject: [PATCH] add -p: fix memory leak Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Johannes Schindelin , Phillip Wood , Phillip Wood Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood asan reports that the C version of `add -p` is not freeing all the memory it allocates. Fix this by introducing a function to clear `struct add_p_state` and use it instead of freeing individual members. Signed-off-by: Phillip Wood --- add -p: fix memory leak It seems to be the season for add -p fixes. This patch fixes a memory leak in the C version found with asan. Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-729%2Fphillipwood%2Fwip%2Fadd-p-fix-memory-leak-v1 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-729/phillipwood/wip/add-p-fix-memory-leak-v1 Pull-Request: https://github.com/gitgitgadget/git/pull/729 add-patch.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) base-commit: 3a238e539bcdfe3f9eb5010fd218640c1b499f7a diff --git a/add-patch.c b/add-patch.c index 457b8c550e..2fcab983a6 100644 --- a/add-patch.c +++ b/add-patch.c @@ -266,6 +266,20 @@ struct add_p_state { const char *revision; }; +static void add_p_state_clear(struct add_p_state *s) +{ + size_t i; + + strbuf_release(&s->answer); + strbuf_release(&s->buf); + strbuf_release(&s->plain); + strbuf_release(&s->colored); + for (i = 0; i < s->file_diff_nr; i++) + free(s->file_diff[i].hunk); + free(s->file_diff); + clear_add_i_state(&s->s); +} + static void err(struct add_p_state *s, const char *fmt, ...) { va_list args; @@ -1690,9 +1704,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode, repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1, NULL, NULL, NULL) < 0) || parse_diff(&s, ps) < 0) { - strbuf_release(&s.plain); - strbuf_release(&s.colored); - clear_add_i_state(&s.s); + add_p_state_clear(&s); return -1; } @@ -1707,10 +1719,6 @@ int run_add_p(struct repository *r, enum add_p_mode mode, else if (binary_count == s.file_diff_nr) fprintf(stderr, _("Only binary files changed.\n")); - strbuf_release(&s.answer); - strbuf_release(&s.buf); - strbuf_release(&s.plain); - strbuf_release(&s.colored); - clear_add_i_state(&s.s); + add_p_state_clear(&s); return 0; }