Commit 66ead2bc authored by Rusty Russell's avatar Rusty Russell

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.
parent 142e3d31
...@@ -321,13 +321,18 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -321,13 +321,18 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
tdb_bool_err berr; tdb_bool_err berr;
enum TDB_ERROR ecode; enum TDB_ERROR ecode;
tdb = malloc(sizeof(*tdb)); tdb = malloc(sizeof(*tdb) + (name ? strlen(name) + 1 : 0));
if (!tdb) { if (!tdb) {
/* Can't log this */ /* Can't log this */
errno = ENOMEM; errno = ENOMEM;
return NULL; 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->direct_access = 0;
tdb->flags = tdb_flags; tdb->flags = tdb_flags;
tdb->log_fn = NULL; tdb->log_fn = NULL;
...@@ -590,7 +595,6 @@ fail_errno: ...@@ -590,7 +595,6 @@ fail_errno:
#ifdef TDB_TRACE #ifdef TDB_TRACE
close(tdb->tracefd); close(tdb->tracefd);
#endif #endif
free(cast_const(char *, tdb->name));
if (tdb->file) { if (tdb->file) {
tdb_lock_cleanup(tdb); tdb_lock_cleanup(tdb);
if (--tdb->file->refcnt == 0) { if (--tdb->file->refcnt == 0) {
...@@ -631,7 +635,6 @@ int tdb_close(struct tdb_context *tdb) ...@@ -631,7 +635,6 @@ int tdb_close(struct tdb_context *tdb)
else else
tdb_munmap(tdb->file); tdb_munmap(tdb->file);
} }
free(cast_const(char *, tdb->name));
if (tdb->file) { if (tdb->file) {
struct tdb_file **i; struct tdb_file **i;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment