From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id KKCNJ9IMdGEhkAAAgWs5BA (envelope-from ) for ; Sat, 23 Oct 2021 15:23:30 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id wAQiI9IMdGErMQAAbx9fmQ (envelope-from ) for ; Sat, 23 Oct 2021 13:23:30 +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 4994714225 for ; Sat, 23 Oct 2021 15:23:30 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id AB8622C209; Sat, 23 Oct 2021 09:23:09 -0400 (EDT) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id F13A82C1F6 for ; Sat, 23 Oct 2021 09:22:59 -0400 (EDT) Received: by fethera.tethera.net (Postfix, from userid 1001) id D8FB45FC48; Sat, 23 Oct 2021 09:22:59 -0400 (EDT) Received: (nullmailer pid 1864484 invoked by uid 1000); Sat, 23 Oct 2021 13:22:52 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 4/6] lib/create: fix memory leak, ensure *database=NULL on error Date: Sat, 23 Oct 2021 10:22:36 -0300 Message-Id: <20211023132238.1864400-5-david@tethera.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211023132238.1864400-1-david@tethera.net> References: <20211023132238.1864400-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: FSWXWKGJ4F5C4CU4RXSELK62Q7C7SHLX X-Message-ID-Hash: FSWXWKGJ4F5C4CU4RXSELK62Q7C7SHLX 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=1634995410; 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=61FGzJWJtDGEOoMN9brsQ5ymAECNkvor2fywhzk/QJ0=; b=P0Ig2Cnjk83Sv5sDQE/shnxx8P6vKDjOuyi3MXho5ufpcoot2Nsc+9M2dDCT93WKO9qYvR fBczkl0tm0E177Osvm4tYR7Xxb/dHEGwz++R66haFUquvK/g2/d8xVDWPyW3TQbi1KLV/l 3dmJca3aOQt5rYdS9YhjEgrcNNVExeQttkavWQp6a4aq5T7jm6wLuUoY2/fd/xPIttndKR qf7yR4dLWwx9yv0AZV3ZT9hjWAEcUS2srJo3fe6KzwlmD3C346L8pRYecQ362AgWICoksv yHAgJxES9sN8TDOV1bPz9ANMuBj3Zw2fC79iGIvdNlHUaiTZ/hpMYAb7254gDg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1634995410; a=rsa-sha256; cv=none; b=LKIMcC48GhbPXeeZ0YWIZjFV0T5tCsYnTMfqoRln+wGfpHqfPyStkTCOjTGIG9IN7cSAii 9W17nzzX6Je5IJPx1IgU6eAroRSUoWSuZB5A369zYFBCMz76JOprMjgL7ZxpH2n7sr+GZ1 9RKWIifDbINx7Hx2KhH+br+LRjoKsoTglT+wh1whCWEdaAIp9aRkno8wyj7H0MlznxD7o8 aKxJrc97RODN25c13LVafOP6eTivqbihqe623zl3l/1C0CP8s1APwp8m3ZDSoPOAe0jvtI LvBo6KWVrCEqGhCvq/qm/GjLl2jJ5BCXM49NZ3k8aUxyxmvvCK5ftuk1EdnlCQ== 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: 4994714225 X-Spam-Score: -1.15 X-Migadu-Scanner: scn1.migadu.com X-TUID: YjAepbfZy9nc 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 becb2821..77328cd7 100755 --- a/test/T590-libconfig.sh +++ b/test/T590-libconfig.sh @@ -891,7 +891,6 @@ EOF test_expect_equal_file EXPECTED OUTPUT test_begin_subtest "create: database set to null on missing config" -test_subtest_known_broken cat c_head3 - c_tail3 <<'EOF' | test_C ${MAIL_DIR} "/nonexistent" notmuch_status_t st = notmuch_database_create_with_config(argv[1],argv[2], NULL, &db, NULL); EOF @@ -903,7 +902,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" cat c_head3 - c_tail3 <<'EOF' | test_C ${MAIL_DIR} -- 2.33.0