]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/open.c
tdb2: make tdb_name() valid early in tdb_open()
[ccan] / ccan / tdb2 / open.c
index c6fd9a090ecf3078bb4135e6b0651403d8379978..37e20907fedfc1a9f512c449dea8b8e727ebcf9a 100644 (file)
@@ -174,7 +174,7 @@ static enum TDB_ERROR tdb_new_file(struct tdb_context *tdb)
        tdb->file = malloc(sizeof(*tdb->file));
        if (!tdb->file)
                return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
-                                 "tdb_open: could alloc tdb_file structure");
+                                 "tdb_open: cannot alloc tdb_file structure");
        tdb->file->num_lockrecs = 0;
        tdb->file->lockrecs = NULL;
        tdb->file->allrecord_lock.count = 0;
@@ -321,13 +321,18 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        tdb_bool_err berr;
        enum TDB_ERROR ecode;
 
-       tdb = malloc(sizeof(*tdb));
+       tdb = malloc(sizeof(*tdb) + (name ? strlen(name) + 1 : 0));
        if (!tdb) {
                /* Can't log this */
                errno = ENOMEM;
                return NULL;
        }
-       tdb->name = NULL;
+       /* Set name immediately for logging functions. */
+       if (name) {
+               tdb->name = strcpy((char *)(tdb + 1), name);
+       } else {
+               tdb->name = NULL;
+       }
        tdb->direct_access = 0;
        tdb->flags = tdb_flags;
        tdb->log_fn = NULL;
@@ -590,7 +595,6 @@ fail_errno:
 #ifdef TDB_TRACE
        close(tdb->tracefd);
 #endif
-       free(cast_const(char *, tdb->name));
        if (tdb->file) {
                tdb_lock_cleanup(tdb);
                if (--tdb->file->refcnt == 0) {
@@ -631,7 +635,6 @@ int tdb_close(struct tdb_context *tdb)
                else
                        tdb_munmap(tdb->file);
        }
-       free(cast_const(char *, tdb->name));
        if (tdb->file) {
                struct tdb_file **i;