]> git.notmuchmail.org Git - notmuch/commitdiff
lib: Use talloc to simplify cleanup in notmuch_database_open
authorAustin Clements <amdragon@MIT.EDU>
Sun, 29 Jan 2012 05:50:10 +0000 (00:50 -0500)
committerDavid Bremner <bremner@debian.org>
Sat, 4 Feb 2012 01:15:45 +0000 (21:15 -0400)
Previously, we manually "free"d various pointers in
notmuch_database_open.  Use a local talloc context instead to simplify
cleanup and eliminate various NULL pointer initializations and
conditionals.

lib/database.cc

index 94022d7ce3ae9c060ecf18baaca1aa95afa11781..c928d02bb147b039dae529be337dda8055a0b757 100644 (file)
@@ -582,15 +582,15 @@ notmuch_database_t *
 notmuch_database_open (const char *path,
                       notmuch_database_mode_t mode)
 {
 notmuch_database_open (const char *path,
                       notmuch_database_mode_t mode)
 {
+    void *local = talloc_new (NULL);
     notmuch_database_t *notmuch = NULL;
     notmuch_database_t *notmuch = NULL;
-    char *notmuch_path = NULL, *xapian_path = NULL;
+    char *notmuch_path, *xapian_path;
     struct stat st;
     int err;
     unsigned int i, version;
     static int initialized = 0;
 
     struct stat st;
     int err;
     unsigned int i, version;
     static int initialized = 0;
 
-    if (asprintf (&notmuch_path, "%s/%s", path, ".notmuch") == -1) {
-       notmuch_path = NULL;
+    if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {
        fprintf (stderr, "Out of memory\n");
        goto DONE;
     }
        fprintf (stderr, "Out of memory\n");
        goto DONE;
     }
@@ -602,8 +602,7 @@ notmuch_database_open (const char *path,
        goto DONE;
     }
 
        goto DONE;
     }
 
-    if (asprintf (&xapian_path, "%s/%s", notmuch_path, "xapian") == -1) {
-       xapian_path = NULL;
+    if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
        fprintf (stderr, "Out of memory\n");
        goto DONE;
     }
        fprintf (stderr, "Out of memory\n");
        goto DONE;
     }
@@ -708,10 +707,7 @@ notmuch_database_open (const char *path,
     }
 
   DONE:
     }
 
   DONE:
-    if (notmuch_path)
-       free (notmuch_path);
-    if (xapian_path)
-       free (xapian_path);
+    talloc_free (local);
 
     return notmuch;
 }
 
     return notmuch;
 }