From de2b3f139e7c51748e701f7449587c1c10faeaec Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Tue, 10 Nov 2020 12:52:13 +0200 Subject: Fix a minor memory leak on failed sqlite database open (RhBug:1896301) Curiously, sqlite allocates resources that need freeing even in case of failure to open. Quoting from https://www.sqlite.org/c3ref/open.html: > Whether or not an error occurs when it is opened, resources associated > with the database connection handle should be released by passing it to > sqlite3_close() when it is no longer required. I disagree, but as it's documented behavior there's no point filing a bug. So lets close the non-open connection and chug on. (cherry picked from commit fb5888417727edbd8ce1b389b1b6ee9ac899d8e4) --- lib/backend/sqlite.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/backend/sqlite.c b/lib/backend/sqlite.c index ee9c62706..c86697a97 100644 --- a/lib/backend/sqlite.c +++ b/lib/backend/sqlite.c @@ -145,6 +145,8 @@ static int sqlite_init(rpmdb rdb, const char * dbhome) xx = sqlite3_open_v2(dbfile, &sdb, flags, NULL); /* Attempt to create if missing, discarding OPEN_READONLY (!) */ if (xx == SQLITE_CANTOPEN && (flags & SQLITE_OPEN_READONLY)) { + /* Sqlite allocates resources even on failure to open (!) */ + sqlite3_close(sdb); flags &= ~SQLITE_OPEN_READONLY; flags |= (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); retry_open++; -- cgit v1.2.1