[db] Fix memleak if db backup is enabled but fails

Closes issue #1741
This commit is contained in:
ejurgensen 2024-04-03 22:04:39 +02:00
parent 1c26681a65
commit 945bde7c66
2 changed files with 8 additions and 9 deletions

View File

@ -7093,8 +7093,9 @@ db_statements_prepare(void)
return 0; return 0;
} }
// Returns -2 if backup not enabled in config
int int
db_backup() db_backup(void)
{ {
int ret; int ret;
sqlite3 *backup_hdl; sqlite3 *backup_hdl;
@ -7114,7 +7115,7 @@ db_backup()
if (realpath(db_path, resolved_dbp) == NULL || realpath(backup_path, resolved_bp) == NULL) 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)); 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) if (strcmp(resolved_bp, resolved_dbp) == 0)
@ -7129,14 +7130,15 @@ db_backup()
if (ret != SQLITE_OK) if (ret != SQLITE_OK)
{ {
DPRINTF(E_WARN, L_DB, "Failed to create backup '%s': %s\n", backup_path, sqlite3_errmsg(backup_hdl)); 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"); backup = sqlite3_backup_init(backup_hdl, "main", hdl, "main");
if (!backup) if (!backup)
{ {
DPRINTF(E_WARN, L_DB, "Failed to initiate backup '%s': %s\n", backup_path, sqlite3_errmsg(backup_hdl)); 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); ret = sqlite3_backup_step(backup, -1);
@ -7148,10 +7150,7 @@ db_backup()
else else
DPRINTF(E_WARN, L_DB, "Failed to complete backup '%s': %s (%d)\n", backup_path, sqlite3_errstr(ret), ret); DPRINTF(E_WARN, L_DB, "Failed to complete backup '%s': %s (%d)\n", backup_path, sqlite3_errstr(ret), ret);
return ret; return 0;
error:
return -1;
} }
int int

View File

@ -1017,7 +1017,7 @@ int
db_watch_enum_fetchwd(struct watch_enum *we, uint32_t *wd); db_watch_enum_fetchwd(struct watch_enum *we, uint32_t *wd);
int int
db_backup(); db_backup(void);
int int
db_perthread_init(void); db_perthread_init(void);