From: Rusty Russell Date: Thu, 7 Apr 2011 11:18:33 +0000 (+0930) Subject: tdb2: make tdb_name() valid early in tdb_open() X-Git-Url: https://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=66ead2bcb732aa53bde5092b34c16d780df779ea tdb2: make tdb_name() valid early in tdb_open() Otherwise tdb_name() can be NULL in log functions. And we might as well allocate it with the tdb, as well. --- diff --git a/ccan/tdb2/open.c b/ccan/tdb2/open.c index 7cbac0af..37e20907 100644 --- a/ccan/tdb2/open.c +++ b/ccan/tdb2/open.c @@ -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;