Skip to content

Commit

Permalink
Add checking of return status of sqlite3_open_v2 to ensure we are not…
Browse files Browse the repository at this point in the history
… trying to use the database when it failed to open

Signed-off-by: DL6ER <dl6er@dl6er.de>
  • Loading branch information
DL6ER committed Jun 6, 2024
1 parent 61826a4 commit ca838b4
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/database/gravity-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
#include "datastructure.h"
// reset_aliasclient()
#include "aliasclients.h"

// Definition of struct regexData
#include "regex_r.h"
// file_readable()
#include "files.h"

// Prefix of interface names in the client table
#define INTERFACE_SEP ":"
Expand Down Expand Up @@ -102,6 +103,13 @@ static void gravity_check_ABP_format(void)
// Check if we have a valid ABP format
// We do this by checking the "abp_domains" property in the "info" table

// Return early if the database is not open
if(gravity_db == NULL)
{
gravity_abp_format = false;
return;
}

// Prepare statement
sqlite3_stmt *stmt = NULL;
int rc = sqlite3_prepare_v2(gravity_db,
Expand Down Expand Up @@ -2715,9 +2723,17 @@ bool gravity_updated(void)
sqlite3 *db = NULL;
sqlite3_stmt *query_stmt = NULL;

// Check if database is a readable file
if(file_readable(config.files.gravity.v.s) == false)
{
log_err("Cannot read gravity database at %s - file does not exist or is not readable",
config.files.gravity.v.s);
return false;
}

// Open database
int rc = sqlite3_open_v2(config.files.gravity.v.s, &db, SQLITE_OPEN_READONLY, NULL);
if(db == NULL)
if(db == NULL || rc != SQLITE_OK)
{
log_err("gravity_updated(): %s - SQL error open: %s", config.files.gravity.v.s, sqlite3_errstr(rc));
return false;
Expand Down

0 comments on commit ca838b4

Please sign in to comment.