Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id B8302431FBF for ; Tue, 24 Nov 2009 08:16:48 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id vyvxzL9IvHr5 for ; Tue, 24 Nov 2009 08:16:46 -0800 (PST) Received: from gw03.mail.saunalahti.fi (gw03.mail.saunalahti.fi [195.197.172.111]) by olra.theworths.org (Postfix) with ESMTP id B422E431FAE for ; Tue, 24 Nov 2009 08:16:45 -0800 (PST) Received: from djcbsoftware.nl (a88-112-254-208.elisa-laajakaista.fi [88.112.254.208]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by gw03.mail.saunalahti.fi (Postfix) with ESMTP id 5443A216682 for ; Tue, 24 Nov 2009 18:16:39 +0200 (EET) Received: from cthulhu.mindcrime.djcbsoftware.nl (localhost [127.0.0.1]) by djcbsoftware.nl (Postfix) with ESMTP id 73FF039C53B for ; Tue, 24 Nov 2009 16:22:02 +0200 (EET) Date: Tue, 24 Nov 2009 16:22:02 +0200 Message-ID: <873a4480l1.wl%djcb@djcbsoftware.nl> From: Dirk-Jan C. Binnema To: "notmuch@notmuchmail org" Mail-Reply-To: djcb@djcbsoftware.nl User-Agent: Wanderlust/2.15.6 (Almost Unreal) Emacs/23.1 Mule/6.0 (HANACHIRUSATO) Organization: DJCBSoftware MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Subject: [notmuch] [PATCH] notmuch-count: make sure all created items are freed, even in error paths X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: djcb@djcbsoftware.nl List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 24 Nov 2009 16:16:48 -0000 Another minor patch, fixing a couple of resource leaks in error paths. --- notmuch-count.c | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 36 insertions(+), 16 deletions(-) diff --git a/notmuch-count.c b/notmuch-count.c index 77aa433..b5808f5 100644 --- a/notmuch-count.c +++ b/notmuch-count.c @@ -28,7 +28,8 @@ notmuch_count_command (void *ctx, int argc, char *argv[]) notmuch_database_t *notmuch; notmuch_query_t *query; char *query_str; - int i; + int i, retval; + #if 0 char *opt, *end; int i, first = 0, max_threads = -1; @@ -76,35 +77,54 @@ notmuch_count_command (void *ctx, int argc, char *argv[]) argc -= i; argv += i; + config = NULL; + query_str = NULL; + notmuch = NULL; + query = NULL; + config = notmuch_config_open (ctx, NULL, NULL); - if (config == NULL) - return 1; - - notmuch = notmuch_database_open (notmuch_config_get_database_path (config), - NOTMUCH_DATABASE_MODE_READ_ONLY); - if (notmuch == NULL) - return 1; - + if (config == NULL) { + retval = 1; + goto out; + } + query_str = query_string_from_args (ctx, argc, argv); if (query_str == NULL) { fprintf (stderr, "Out of memory.\n"); - return 1; + retval = 1; + goto out; } if (*query_str == '\0') { fprintf (stderr, "Error: notmuch count requires at least one count term.\n"); - return 1; + retval = 1; + goto out; } - + + notmuch = notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_ONLY); + if (notmuch == NULL) { + fprintf (stderr, "Failed to open database at %s\n", + notmuch_config_get_database_path (config)); + retval = 1; + goto out; + } + query = notmuch_query_create (notmuch, query_str); if (query == NULL) { fprintf (stderr, "Out of memory\n"); - return 1; + retval = 1; + goto out; } printf ("%u\n", notmuch_query_count_messages(query)); + retval = 0; +out: + talloc_free (query_str); + notmuch_config_close (config); notmuch_query_destroy (query); - notmuch_database_close (notmuch); - - return 0; + if (notmuch) /* _close does not allow NULL */ + notmuch_database_close (notmuch); + + return retval; } -- 1.6.3.3