From bd796058197420dc9eec63ef701206f9ce408d3f Mon Sep 17 00:00:00 2001
From: Panu Matilainen <pmatilai@redhat.com>
Date: Wed, 23 Oct 2019 12:44:47 +0300
Subject: [PATCH] Handle setting db_descr centrally from the backend name

Now that we can, set db_descr centrally on database open instead of
relying on backends to do it (and forget, or leak memory, as has been
the case). Also don't bother mallocing, the name of the backend is
quite enough.

With backends knowing their own names we could probably eliminate db_descr
entirely but leaving that for another rainy day, it's possible there
are code paths that assume it being set to something.
---
 lib/backend/db3.c    | 4 ----
 lib/backend/dbi.c    | 2 ++
 lib/backend/dbi.h    | 2 +-
 lib/backend/lmdb.c   | 4 ----
 lib/rpmdb.c          | 5 ++---
 5 files changed, 5 insertions(+), 12 deletions(-)

diff --git a/lib/backend/db3.c b/lib/backend/db3.c
index ab2f11f61..ff0fe4305 100644
--- a/lib/backend/db3.c
+++ b/lib/backend/db3.c
@@ -415,10 +415,6 @@ static int db_init(rpmdb rdb, const char * dbhome)
     if (rdb->db_dbenv != NULL) {
 	rdb->db_opens++;
 	return 0;
-    } else {
-	/* On first call, set backend description to something... */
-	free(rdb->db_descr);
-	rasprintf(&rdb->db_descr, "db%u", DB_VERSION_MAJOR);
     }
 
     /*
diff --git a/lib/backend/dbi.c b/lib/backend/dbi.c
index 9e8d667..5443325 100644
--- a/lib/backend/dbi.c
+++ b/lib/backend/dbi.c
@@ -112,6 +112,8 @@ dbDetectBackend(rpmdb rdb)
 	rpmlog(RPMLOG_WARNING, "using dummy database, installs not possible\n");
     }
 
+    rdb->db_descr = rdb->db_ops->name;
+
     if (db_backend)
 	free(db_backend);
 }
diff --git a/lib/backend/dbi.h b/lib/backend/dbi.h
index 3fc9345c9..b2b9717c4 100644
--- a/lib/backend/dbi.h
+++ b/lib/backend/dbi.h
@@ -49,7 +49,7 @@ struct rpmdb_s {
     int		db_flags;
     int		db_mode;	/*!< open mode */
     int		db_perms;	/*!< open permissions */
-    char	* db_descr;	/*!< db backend description (for error msgs) */
+    const char	* db_descr;	/*!< db backend description (for error msgs) */
     struct dbChk_s * db_checked;/*!< headerCheck()'ed package instances */
     rpmdb	db_next;
     int		db_opens;
diff --git a/lib/backend/lmdb.c b/lib/backend/lmdb.c
index 801f50e54..badd317c9 100644
--- a/lib/backend/lmdb.c
+++ b/lib/backend/lmdb.c
@@ -137,10 +137,6 @@ static int db_init(rpmdb rdb, const char * dbhome)
     if (rdb->db_dbenv != NULL) {
 	rdb->db_opens++;
 	return 0;
-    } else {
-	/* On first call, set backend description to something... */
-	free(rdb->db_descr);
-	rdb->db_descr = xstrdup("lmdb");
     }
 
     MDB_dbi maxdbs = 32;
diff --git a/lib/rpmdb.c b/lib/rpmdb.c
index b97274e7b..9cd50e7d9 100644
--- a/lib/rpmdb.c
+++ b/lib/rpmdb.c
@@ -408,7 +408,6 @@ int rpmdbClose(rpmdb db)
     db->db_fullpath = _free(db->db_fullpath);
     db->db_checked = dbChkFree(db->db_checked);
     db->db_indexes = _free(db->db_indexes);
-    db->db_descr = _free(db->db_descr);
 
     if (next) {
         *prev = next->db_next;
@@ -473,7 +472,6 @@ static rpmdb newRpmdb(const char * root, const char * home,
     db->db_tags = dbiTags;
     db->db_ndbi = sizeof(dbiTags) / sizeof(rpmDbiTag);
     db->db_indexes = xcalloc(db->db_ndbi, sizeof(*db->db_indexes));
-    db->db_descr = xstrdup("unknown db");
     db->nrefs = 0;
     return rpmdbLink(db);
 }
@@ -522,7 +522,8 @@ static int openDatabase(const char * prefix,
 	}
 
 	rc = doOpen(db, justPkgs);
-
+        if (!db->db_descr)
+	    db->db_descr = "unknown db";
     }
 
     if (rc || justCheck || dbp == NULL)
-- 
2.27.0

