sticker: initialize hash table in sticker_new()
Moved the hash table initialization from sticker_list_values() to the new function sticker_new(). This fixes a memory leak in sticker_list_values().
This commit is contained in:
@@ -194,17 +194,15 @@ sticker_load_value(const char *type, const char *uri, const char *name)
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static GHashTable *
|
static bool
|
||||||
sticker_list_values(const char *type, const char *uri)
|
sticker_list_values(GHashTable *hash, const char *type, const char *uri)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
char *name, *value;
|
char *name, *value;
|
||||||
GHashTable *hash;
|
|
||||||
|
|
||||||
hash = NULL;
|
assert(hash != NULL);
|
||||||
assert(type != NULL);
|
assert(type != NULL);
|
||||||
assert(uri != NULL);
|
assert(uri != NULL);
|
||||||
|
|
||||||
assert(sticker_enabled());
|
assert(sticker_enabled());
|
||||||
|
|
||||||
sqlite3_reset(sticker_stmt_list);
|
sqlite3_reset(sticker_stmt_list);
|
||||||
@@ -213,25 +211,20 @@ sticker_list_values(const char *type, const char *uri)
|
|||||||
if (ret != SQLITE_OK) {
|
if (ret != SQLITE_OK) {
|
||||||
g_warning("sqlite3_bind_text() failed: %s",
|
g_warning("sqlite3_bind_text() failed: %s",
|
||||||
sqlite3_errmsg(sticker_db));
|
sqlite3_errmsg(sticker_db));
|
||||||
return NULL;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = sqlite3_bind_text(sticker_stmt_list, 2, uri, -1, NULL);
|
ret = sqlite3_bind_text(sticker_stmt_list, 2, uri, -1, NULL);
|
||||||
if (ret != SQLITE_OK) {
|
if (ret != SQLITE_OK) {
|
||||||
g_warning("sqlite3_bind_text() failed: %s",
|
g_warning("sqlite3_bind_text() failed: %s",
|
||||||
sqlite3_errmsg(sticker_db));
|
sqlite3_errmsg(sticker_db));
|
||||||
return NULL;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
ret = sqlite3_step(sticker_stmt_list);
|
ret = sqlite3_step(sticker_stmt_list);
|
||||||
switch (ret) {
|
switch (ret) {
|
||||||
case SQLITE_ROW:
|
case SQLITE_ROW:
|
||||||
if (!hash)
|
|
||||||
hash = g_hash_table_new_full(g_str_hash,
|
|
||||||
g_str_equal,
|
|
||||||
(GDestroyNotify)g_free,
|
|
||||||
(GDestroyNotify)g_free);
|
|
||||||
name = g_strdup((const char*)sqlite3_column_text(sticker_stmt_list, 0));
|
name = g_strdup((const char*)sqlite3_column_text(sticker_stmt_list, 0));
|
||||||
value = g_strdup((const char*)sqlite3_column_text(sticker_stmt_list, 1));
|
value = g_strdup((const char*)sqlite3_column_text(sticker_stmt_list, 1));
|
||||||
g_hash_table_insert(hash, name, value);
|
g_hash_table_insert(hash, name, value);
|
||||||
@@ -244,14 +237,14 @@ sticker_list_values(const char *type, const char *uri)
|
|||||||
default:
|
default:
|
||||||
g_warning("sqlite3_step() failed: %s",
|
g_warning("sqlite3_step() failed: %s",
|
||||||
sqlite3_errmsg(sticker_db));
|
sqlite3_errmsg(sticker_db));
|
||||||
return NULL;
|
return false;
|
||||||
}
|
}
|
||||||
} while (ret != SQLITE_DONE);
|
} while (ret != SQLITE_DONE);
|
||||||
|
|
||||||
sqlite3_reset(sticker_stmt_list);
|
sqlite3_reset(sticker_stmt_list);
|
||||||
sqlite3_clear_bindings(sticker_stmt_list);
|
sqlite3_clear_bindings(sticker_stmt_list);
|
||||||
|
|
||||||
return hash;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
@@ -438,6 +431,16 @@ sticker_delete(const char *type, const char *uri)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct sticker *
|
||||||
|
sticker_new(void)
|
||||||
|
{
|
||||||
|
struct sticker *sticker = g_new(struct sticker, 1);
|
||||||
|
|
||||||
|
sticker->table = g_hash_table_new_full(g_str_hash, g_str_equal,
|
||||||
|
g_free, g_free);
|
||||||
|
return sticker;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
sticker_free(struct sticker *sticker)
|
sticker_free(struct sticker *sticker)
|
||||||
{
|
{
|
||||||
@@ -485,11 +488,16 @@ sticker_foreach(struct sticker *sticker,
|
|||||||
struct sticker *
|
struct sticker *
|
||||||
sticker_load(const char *type, const char *uri)
|
sticker_load(const char *type, const char *uri)
|
||||||
{
|
{
|
||||||
struct sticker *sticker = g_new(struct sticker, 1);
|
struct sticker *sticker = sticker_new();
|
||||||
|
bool success;
|
||||||
|
|
||||||
sticker->table = sticker_list_values(type, uri);
|
success = sticker_list_values(sticker->table, type, uri);
|
||||||
if (sticker->table == NULL) {
|
if (!success)
|
||||||
g_free(sticker);
|
return NULL;
|
||||||
|
|
||||||
|
if (g_hash_table_size(sticker->table) == 0) {
|
||||||
|
/* don't return empty sticker objects */
|
||||||
|
sticker_free(sticker);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user