From cf70c514b622546d4809fd69ff0fb4ddc5541675 Mon Sep 17 00:00:00 2001 From: chme Date: Sat, 26 Sep 2015 09:28:20 +0200 Subject: [PATCH] Fix memory leak if index clause is used --- src/db.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/db.c b/src/db.c index d9626328..04426d9d 100644 --- a/src/db.c +++ b/src/db.c @@ -951,6 +951,7 @@ db_build_query_items(struct query_params *qp, char **q) return -1; /* Get index clause */ + idx = NULL; ret = db_build_query_index_clause(qp, &idx); if (ret < 0) return -1; @@ -993,6 +994,7 @@ db_build_query_pls(struct query_params *qp, char **q) return -1; /* Get index clause */ + idx = NULL; ret = db_build_query_index_clause(qp, &idx); if (ret < 0) return -1; @@ -1008,6 +1010,9 @@ db_build_query_pls(struct query_params *qp, char **q) else query = sqlite3_mprintf("SELECT f.* FROM playlists f WHERE f.disabled = 0 %s;", sort); + if (idx) + sqlite3_free(idx); + if (!query) { DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); @@ -1048,6 +1053,7 @@ db_build_query_plitems_plain(struct query_params *qp, char **q) return -1; /* Get index clause */ + idx = NULL; ret = db_build_query_index_clause(qp, &idx); if (ret < 0) return -1; @@ -1069,6 +1075,9 @@ db_build_query_plitems_plain(struct query_params *qp, char **q) " WHERE pi.playlistid = %d AND f.disabled = 0 ORDER BY pi.id ASC;", qp->id); + if (idx) + sqlite3_free(idx); + if (!query) { DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); @@ -1110,16 +1119,21 @@ db_build_query_plitems_smart(struct query_params *qp, char *smartpl_query, char return -1; /* Get index clause */ + idx = NULL; ret = db_build_query_index_clause(qp, &idx); if (ret < 0) return -1; - if (!idx) - idx = ""; - sort = sort_clause[qp->sort]; - query = sqlite3_mprintf("SELECT f.* FROM files f WHERE f.disabled = 0 AND %s AND %s %s %s;", smartpl_query, filter, sort, idx); + if (idx) + query = sqlite3_mprintf("SELECT f.* FROM files f WHERE f.disabled = 0 AND %s AND %s %s %s;", smartpl_query, filter, sort, idx); + else + query = sqlite3_mprintf("SELECT f.* FROM files f WHERE f.disabled = 0 AND %s AND %s %s;", smartpl_query, filter, sort); + + if (idx) + sqlite3_free(idx); + if (!query) { DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); @@ -1183,6 +1197,7 @@ db_build_query_group_albums(struct query_params *qp, char **q) return -1; /* Get index clause */ + idx = NULL; ret = db_build_query_index_clause(qp, &idx); if (ret < 0) return -1; @@ -1198,6 +1213,9 @@ db_build_query_group_albums(struct query_params *qp, char **q) else query = sqlite3_mprintf("SELECT g.id, g.persistentid, f.album, f.album_sort, COUNT(f.id), 1, f.album_artist, f.songartistid FROM files f JOIN groups g ON f.songalbumid = g.persistentid WHERE f.disabled = 0 GROUP BY f.songalbumid %s;", sort); + if (idx) + sqlite3_free(idx); + if (!query) { DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); @@ -1222,6 +1240,7 @@ db_build_query_group_artists(struct query_params *qp, char **q) return -1; /* Get index clause */ + idx = NULL; ret = db_build_query_index_clause(qp, &idx); if (ret < 0) return -1; @@ -1237,6 +1256,9 @@ db_build_query_group_artists(struct query_params *qp, char **q) else query = sqlite3_mprintf("SELECT g.id, g.persistentid, f.album_artist, f.album_artist_sort, COUNT(f.id), COUNT(DISTINCT f.songalbumid), f.album_artist, f.songartistid FROM files f JOIN groups g ON f.songartistid = g.persistentid WHERE f.disabled = 0 GROUP BY f.songartistid %s;", sort); + if (idx) + sqlite3_free(idx); + if (!query) { DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); @@ -1414,6 +1436,7 @@ db_build_query_browse(struct query_params *qp, const char *field, const char *gr return -1; /* Get index clause */ + idx = NULL; ret = db_build_query_index_clause(qp, &idx); if (ret < 0) return -1; @@ -1433,6 +1456,9 @@ db_build_query_browse(struct query_params *qp, const char *field, const char *gr query = sqlite3_mprintf("SELECT f.%s, f.%s FROM files f WHERE f.disabled = 0 AND f.%s != ''" " GROUP BY f.%s %s", field, group_field, field, group_field, sort); + if (idx) + sqlite3_free(idx); + if (!query) { DPRINTF(E_LOG, L_DB, "Out of memory for query string\n");