From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id sEy5JgXVc2H+AAAAgWs5BA (envelope-from ) for ; Sat, 23 Oct 2021 11:25:25 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id ICheIgXVc2EqVwAAB5/wlQ (envelope-from ) for ; Sat, 23 Oct 2021 09:25:25 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [144.217.243.247]) (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 aspmx1.migadu.com (Postfix) with ESMTPS id EED4115B0D for ; Sat, 23 Oct 2021 11:25:24 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 633C62C1AA; Sat, 23 Oct 2021 05:25:06 -0400 (EDT) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id 785552906D for ; Sat, 23 Oct 2021 05:24:59 -0400 (EDT) Received: by fethera.tethera.net (Postfix, from userid 1001) id 4C52B5FC48; Sat, 23 Oct 2021 05:24:59 -0400 (EDT) Received: (nullmailer pid 1403471 invoked by uid 1000); Sat, 23 Oct 2021 09:24:54 -0000 From: David Bremner To: Austin Ray , notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 4/5] lib/create: fix memory leak, ensure *database=NULL on error Date: Sat, 23 Oct 2021 06:24:50 -0300 Message-Id: <20211023092451.1403141-5-david@tethera.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211023092451.1403141-1-david@tethera.net> References: <20211023042225.xt36nzpsukqs6kid@athena> <20211023092451.1403141-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: H4P5GQ56K74572ZZQRY4QCMOCOHCJ27Q X-Message-ID-Hash: H4P5GQ56K74572ZZQRY4QCMOCOHCJ27Q X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1634981125; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=4aZfd6rvbs3d7sdWd4i3QX1Cvm82tn2fRT5uWwYDJcA=; b=cgKCwueZ9RcrbQeMpauyYnezqaK32QayCei9gDzxdpkpNdpedR0G880cO7IB/h+eb9RURE nKaYyAuEJ0Xzx+EQwYaNSB8x+3GKMHzYmPZnMARFkiPg5+gxP+xflU1BhH86o/v4RXHv6X wb7JlIM0xr8vLTsp2KTq8+n/x+DFumNwY2oBFv2B3vaW+9z5WjaF0uj/vLlb9i2q0plHLG KX4LtrpEoFnuBua+UKJQFI8Zq/Z5pNz1WaYOiDvLLhGRegf7MFXUwF2Qk5ZGAtmrGZ5exj p2IhH3c8Z8q3UasgE4cvBVkEdc7+hYoYJQoyIga+G3JUNEO7kHWSz4DauIS+fQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1634981125; a=rsa-sha256; cv=none; b=Hip6mANEe/cMPd9LMcKnq4bEn5MeybliabfRdt+emmkc1dXK/0G9J3rLHUg+n+weEQjzZv PKxdfFlMKsnW9jrL9p/ZEEyGxP/6bFAXuFLWiVzjzWztKDPG+V8X5jGTnl4traFRcy8k0e H6NHUCnti7/frgKRnOWnf455h/kDHxaWZAcEYwrLV2LTQQhc9Y+N5aflFmoQZLkt/R5vaD e2fU4E7PwbiNPJUFe9pc9we4CcEZ9IY/VdGns/lldh4RqUPvPULMLI51nSiBJWcVp8Yzxa nIRWrhURxFEPWVoyFN83TKTCcnAfwM+4U/NkxeQYQ/DGxktihqMAvzL0xSztlg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -1.15 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: EED4115B0D X-Spam-Score: -1.15 X-Migadu-Scanner: scn0.migadu.com X-TUID: I5vGPrHFuMiD This code previously relied on _finish_open to free the notmuch struct on errors (except for the case of database == NULL, which was a potential double free). When we removed those frees from _finish_open, we introduced a (small) memory leak. In this commit, fix the memory leak, and harmonize the on-error behaviour with n_d_open_with_config. --- lib/open.cc | 10 ++++++++-- test/T590-libconfig.sh | 2 -- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/open.cc b/lib/open.cc index 77f01f72..6fa00a84 100644 --- a/lib/open.cc +++ b/lib/open.cc @@ -714,10 +714,16 @@ notmuch_database_create_with_config (const char *database_path, else free (message); } + if (status && notmuch) { + notmuch_database_destroy (notmuch); + notmuch = NULL; + } + if (database) *database = notmuch; - else - talloc_free (notmuch); + + if (notmuch) + notmuch->open = true; return status; } diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh index b36653bf..cd7d6e24 100755 --- a/test/T590-libconfig.sh +++ b/test/T590-libconfig.sh @@ -893,7 +893,6 @@ EOF test_expect_equal_file EXPECTED OUTPUT test_begin_subtest "create: database set to null on missing config" -test_subtest_known_broken test_C ${MAIL_DIR} "/nonexistent" < int main (int argc, char **argv) { @@ -912,7 +911,6 @@ EOF test_expect_equal_file EXPECTED OUTPUT test_begin_subtest "create: database set to null on missing config (env)" -test_subtest_known_broken old_NOTMUCH_CONFIG=${NOTMUCH_CONFIG} NOTMUCH_CONFIG="/nonexistent" test_C ${MAIL_DIR} <