Skip to content

Commit

Permalink
[db] Fix memleak if db backup is enabled but fails
Browse files Browse the repository at this point in the history
Closes issue #1741
  • Loading branch information
ejurgensen committed Apr 3, 2024
1 parent 1c26681 commit 945bde7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -7093,8 +7093,9 @@ db_statements_prepare(void)
return 0;
}

// Returns -2 if backup not enabled in config
int
db_backup()
db_backup(void)
{
int ret;
sqlite3 *backup_hdl;
Expand All @@ -7114,7 +7115,7 @@ db_backup()
if (realpath(db_path, resolved_dbp) == NULL || realpath(backup_path, resolved_bp) == NULL)
{
DPRINTF(E_LOG, L_DB, "Failed to resolve real path of db/backup path: %s\n", strerror(errno));
goto error;
return -1;
}

if (strcmp(resolved_bp, resolved_dbp) == 0)
Expand All @@ -7129,14 +7130,15 @@ db_backup()
if (ret != SQLITE_OK)
{
DPRINTF(E_WARN, L_DB, "Failed to create backup '%s': %s\n", backup_path, sqlite3_errmsg(backup_hdl));
goto error;
return -1;
}

backup = sqlite3_backup_init(backup_hdl, "main", hdl, "main");
if (!backup)
{
DPRINTF(E_WARN, L_DB, "Failed to initiate backup '%s': %s\n", backup_path, sqlite3_errmsg(backup_hdl));
goto error;
sqlite3_close(backup_hdl);
return -1;
}

ret = sqlite3_backup_step(backup, -1);
Expand All @@ -7148,10 +7150,7 @@ db_backup()
else
DPRINTF(E_WARN, L_DB, "Failed to complete backup '%s': %s (%d)\n", backup_path, sqlite3_errstr(ret), ret);

return ret;

error:
return -1;
return 0;
}

int
Expand Down
2 changes: 1 addition & 1 deletion src/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ int
db_watch_enum_fetchwd(struct watch_enum *we, uint32_t *wd);

int
db_backup();
db_backup(void);

int
db_perthread_init(void);
Expand Down

0 comments on commit 945bde7

Please sign in to comment.