Skip to content

Commit

Permalink
Fix regression causing access to already closed sqlite handle
Browse files Browse the repository at this point in the history
Commit fb58884 introduced a regression
where on database open failure we end up accessing the already freed
handle when trying to report the error.

Now that we're no longer implicitly creating non-existent databases,
just simplify the whole logic to get rid of it. 4.16.x branch will need
a different fix, maybe use sqlite3_errstr() instead of _errmsg().

Reported and initial patch by Demi M. Obenour.
  • Loading branch information
pmatilai committed Mar 1, 2021
1 parent f9b9017 commit d601a7b
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions lib/backend/sqlite.c
Expand Up @@ -134,24 +134,17 @@ static int sqlite_init(rpmdb rdb, const char * dbhome)
if (rdb->db_dbenv == NULL) {
dbfile = rpmGenPath(dbhome, rdb->db_ops->path, NULL);
sqlite3 *sdb = NULL;
int xx, flags = 0;
int retry_open = 1;
int flags = 0;
if ((rdb->db_mode & O_ACCMODE) == O_RDONLY)
flags |= SQLITE_OPEN_READONLY;
else
flags |= (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);

while (retry_open--) {
xx = sqlite3_open_v2(dbfile, &sdb, flags, NULL);
if (xx == SQLITE_CANTOPEN) {
/* Sqlite allocates resources even on failure to open (!) */
sqlite3_close(sdb);
}
}

if (xx != SQLITE_OK) {
if (sqlite3_open_v2(dbfile, &sdb, flags, NULL) != SQLITE_OK ) {
rpmlog(RPMLOG_ERR, _("Unable to open sqlite database %s: %s\n"),
dbfile, sqlite3_errmsg(sdb));
/* Sqlite allocates resources even on failure to open (!) */
sqlite3_close(sdb);
rc = 1;
goto exit;
}
Expand Down

0 comments on commit d601a7b

Please sign in to comment.