g_free() to auto_gfree, introduce auto_guchar

Fix 11 potential mem leaks in theme.c
This commit is contained in:
John Hernandez
2023-07-13 17:04:59 +02:00
parent 3a4cd7da48
commit 8304ac86ff
39 changed files with 145 additions and 233 deletions

View File

@@ -65,7 +65,7 @@ static GHashTable* jid_to_ver;
static GHashTable* jid_to_caps;
static GHashTable* prof_features;
static char* my_sha1;
static gchar* my_sha1;
static void _save_cache(void);
static EntityCapabilities* _caps_by_ver(const char* const ver);
@@ -464,8 +464,7 @@ static void
_save_cache(void)
{
gsize g_data_size;
gchar* g_cache_data = g_key_file_to_data(cache, &g_data_size, NULL);
auto_gchar gchar* g_cache_data = g_key_file_to_data(cache, &g_data_size, NULL);
g_file_set_contents(cache_loc, g_cache_data, g_data_size, NULL);
g_chmod(cache_loc, S_IRUSR | S_IWUSR);
g_free(g_cache_data);
}

View File

@@ -879,7 +879,7 @@ _caps_response_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
// validate sha1
gchar** split = g_strsplit(node, "#", -1);
char* given_sha1 = split[1];
char* generated_sha1 = stanza_create_caps_sha1_from_query(query);
auto_gchar gchar* generated_sha1 = stanza_create_caps_sha1_from_query(query);
if (g_strcmp0(given_sha1, generated_sha1) != 0) {
log_warning("Generated sha-1 does not match given:");
@@ -901,7 +901,6 @@ _caps_response_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
caps_map_jid_to_ver(from, given_sha1);
}
g_free(generated_sha1);
g_strfreev(split);
return 0;
@@ -1079,7 +1078,7 @@ _room_list_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
const char* stanza_name = xmpp_stanza_get_name(child);
if (stanza_name && (g_strcmp0(stanza_name, STANZA_NAME_ITEM) == 0)) {
const char* item_jid = xmpp_stanza_get_attribute(child, STANZA_ATTR_JID);
gchar* item_jid_lower = NULL;
auto_gchar gchar* item_jid_lower = NULL;
if (item_jid) {
Jid* jidp = jid_create(item_jid);
if (jidp && jidp->localpart) {
@@ -1088,7 +1087,7 @@ _room_list_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
jid_destroy(jidp);
}
const char* item_name = xmpp_stanza_get_attribute(child, STANZA_ATTR_NAME);
gchar* item_name_lower = NULL;
auto_gchar gchar* item_name_lower = NULL;
if (item_name) {
item_name_lower = g_utf8_strdown(item_name, -1);
}
@@ -1106,8 +1105,6 @@ _room_list_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
cons_show(" %s", item->str);
g_string_free(item, TRUE);
}
g_free(item_jid_lower);
g_free(item_name_lower);
}
child = xmpp_stanza_get_next(child);
}
@@ -2594,7 +2591,7 @@ iq_mam_request_older(ProfChatWin* win)
ProfMessage* first_msg = log_database_get_limits_info(win->barejid, FALSE);
char* firstid = NULL;
char* enddate = NULL;
auto_gchar gchar* enddate = NULL;
// If first message found
if (first_msg->timestamp) {
@@ -2608,7 +2605,6 @@ iq_mam_request_older(ProfChatWin* win)
xmpp_stanza_t* iq = stanza_create_mam_iq(ctx, win->barejid, NULL, enddate, firstid, NULL);
iq_id_handler_add(xmpp_stanza_get_id(iq), _mam_buffer_commit_handler, NULL, win);
g_free(enddate);
message_free(first_msg);
iq_send_stanza(iq);

View File

@@ -163,9 +163,8 @@ jid_is_valid_room_form(Jid* jid)
char*
create_fulljid(const char* const barejid, const char* const resource)
{
gchar* barejidlower = g_utf8_strdown(barejid, -1);
auto_gchar gchar* barejidlower = g_utf8_strdown(barejid, -1);
GString* full_jid = g_string_new(barejidlower);
g_free(barejidlower);
g_string_append(full_jid, "/");
g_string_append(full_jid, resource);
@@ -209,7 +208,7 @@ jid_fulljid_or_barejid(Jid* jid)
}
}
char*
gchar*
jid_random_resource(void)
{
auto_char char* rand = get_random_string(4);

View File

@@ -64,6 +64,6 @@ char* create_fulljid(const char* const barejid, const char* const resource);
char* get_nick_from_full_jid(const char* const full_room_jid);
char* jid_fulljid_or_barejid(Jid* jid);
char* jid_random_resource(void);
gchar* jid_random_resource(void);
#endif

View File

@@ -647,10 +647,9 @@ message_send_chat_omemo(const char* const jid, uint32_t sid, GList* keys,
xmpp_stanza_set_attribute(key_stanza, "prekey", "true");
}
gchar* key_raw = g_base64_encode(key->data, key->length);
auto_gchar gchar* key_raw = g_base64_encode(key->data, key->length);
xmpp_stanza_t* key_text = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(key_text, key_raw);
g_free(key_raw);
xmpp_stanza_add_child(key_stanza, key_text);
xmpp_stanza_add_child(header, key_stanza);
@@ -661,10 +660,9 @@ message_send_chat_omemo(const char* const jid, uint32_t sid, GList* keys,
xmpp_stanza_t* iv_stanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(iv_stanza, "iv");
gchar* iv_raw = g_base64_encode(iv, iv_len);
auto_gchar gchar* iv_raw = g_base64_encode(iv, iv_len);
xmpp_stanza_t* iv_text = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(iv_text, iv_raw);
g_free(iv_raw);
xmpp_stanza_add_child(iv_stanza, iv_text);
xmpp_stanza_add_child(header, iv_stanza);
@@ -677,10 +675,9 @@ message_send_chat_omemo(const char* const jid, uint32_t sid, GList* keys,
xmpp_stanza_t* payload = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(payload, "payload");
gchar* ciphertext_raw = g_base64_encode(ciphertext, ciphertext_len);
auto_gchar gchar* ciphertext_raw = g_base64_encode(ciphertext, ciphertext_len);
xmpp_stanza_t* payload_text = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(payload_text, ciphertext_raw);
g_free(ciphertext_raw);
xmpp_stanza_add_child(payload, payload_text);
xmpp_stanza_add_child(encrypted, payload);

View File

@@ -737,7 +737,7 @@ muc_autocomplete(ProfWin* window, const char* const input, gboolean previous)
}
}
char* result = autocomplete_complete(chat_room->nick_ac, search_str, FALSE, previous);
auto_gchar gchar* result = autocomplete_complete(chat_room->nick_ac, search_str, FALSE, previous);
if (result == NULL) {
return NULL;
}
@@ -748,7 +748,6 @@ muc_autocomplete(ProfWin* window, const char* const input, gboolean previous)
if (strlen(chat_room->autocomplete_prefix) == 0) {
g_string_append(replace_with, ": ");
}
g_free(result);
return g_string_free(replace_with, FALSE);
}

View File

@@ -651,7 +651,7 @@ _available_handler(xmpp_stanza_t* const stanza)
const char* account_name = session_get_account_name();
int max_sessions = accounts_get_max_sessions(account_name);
if (max_sessions > 0) {
const char* cur_resource = accounts_get_resource(account_name);
const gchar* cur_resource = accounts_get_resource(account_name);
int res_count = connection_count_available_resources();
if (res_count > max_sessions && g_strcmp0(cur_resource, resource->name)) {
ProfWin* console = wins_get_console();

View File

@@ -268,7 +268,7 @@ roster_result_handler(xmpp_stanza_t* const stanza)
while (item) {
const char* barejid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID);
gchar* barejid_lower = g_utf8_strdown(barejid, -1);
auto_gchar gchar* barejid_lower = g_utf8_strdown(barejid, -1);
const char* name = xmpp_stanza_get_attribute(item, STANZA_ATTR_NAME);
const char* sub = xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION);
@@ -289,7 +289,6 @@ roster_result_handler(xmpp_stanza_t* const stanza)
log_warning("Attempt to add contact twice: %s", barejid_lower);
}
g_free(barejid_lower);
item = xmpp_stanza_get_next(item);
}

View File

@@ -163,9 +163,8 @@ roster_get_contact(const char* const barejid)
{
assert(roster != NULL);
gchar* barejidlower = g_utf8_strdown(barejid, -1);
auto_gchar gchar* barejidlower = g_utf8_strdown(barejid, -1);
PContact contact = g_hash_table_lookup(roster->contacts, barejidlower);
g_free(barejidlower);
return contact;
}

View File

@@ -178,7 +178,7 @@ session_connect_with_details(const char* const jid, const char* const passwd, co
Jid* jidp = jid_create(jid);
if (jidp->resourcepart == NULL) {
jid_destroy(jidp);
auto_char char* resource = jid_random_resource();
auto_gchar gchar* resource = jid_random_resource();
jidp = jid_create_from_bare_and_resource(jid, resource);
saved_details.jid = strdup(jidp->fulljid);
} else {

View File

@@ -61,7 +61,7 @@
#include "database.h"
static void _stanza_add_unique_id(xmpp_stanza_t* stanza);
static char* _stanza_create_sha1_hash(char* str);
static gchar* _stanza_create_sha1_hash(char* str);
#if 0
xmpp_stanza_t*
@@ -233,10 +233,9 @@ stanza_create_http_upload_request(xmpp_ctx_t* ctx, const char* const id,
}
xmpp_stanza_set_attribute(request, STANZA_ATTR_FILENAME, basename(filename_cpy));
gchar* filesize = g_strdup_printf("%jd", (intmax_t)(upload->filesize));
auto_gchar gchar* filesize = g_strdup_printf("%jd", (intmax_t)(upload->filesize));
if (filesize) {
xmpp_stanza_set_attribute(request, STANZA_ATTR_SIZE, filesize);
g_free(filesize);
}
xmpp_stanza_set_attribute(request, STANZA_ATTR_CONTENTTYPE, upload->mime_type);
@@ -1026,7 +1025,7 @@ stanza_create_ping_iq(xmpp_ctx_t* ctx, const char* const target)
return iq;
}
char*
gchar*
stanza_create_caps_sha1_from_query(xmpp_stanza_t* const query)
{
GSList* identities = NULL;
@@ -1116,7 +1115,7 @@ stanza_create_caps_sha1_from_query(xmpp_stanza_t* const query)
curr = g_slist_next(curr);
}
char* result = _stanza_create_sha1_hash(s->str);
gchar* result = _stanza_create_sha1_hash(s->str);
g_string_free(s, TRUE);
g_slist_free_full(identities, g_free);
@@ -2353,9 +2352,8 @@ stanza_create_omemo_bundle_publish(xmpp_ctx_t* ctx, const char* const id,
xmpp_stanza_set_attribute(signed_prekey_public_stanza, "signedPreKeyId", "1");
xmpp_stanza_t* signed_prekey_public_stanza_text = xmpp_stanza_new(ctx);
char* signed_prekey_b64 = g_base64_encode(signed_prekey, signed_prekey_length);
auto_gchar gchar* signed_prekey_b64 = g_base64_encode(signed_prekey, signed_prekey_length);
xmpp_stanza_set_text(signed_prekey_public_stanza_text, signed_prekey_b64);
g_free(signed_prekey_b64);
xmpp_stanza_add_child(signed_prekey_public_stanza, signed_prekey_public_stanza_text);
xmpp_stanza_release(signed_prekey_public_stanza_text);
@@ -2363,9 +2361,8 @@ stanza_create_omemo_bundle_publish(xmpp_ctx_t* ctx, const char* const id,
xmpp_stanza_set_name(signed_prekey_signature_stanza, "signedPreKeySignature");
xmpp_stanza_t* signed_prekey_signature_stanza_text = xmpp_stanza_new(ctx);
char* signed_prekey_signature_b64 = g_base64_encode(signed_prekey_signature, signed_prekey_signature_length);
auto_gchar gchar* signed_prekey_signature_b64 = g_base64_encode(signed_prekey_signature, signed_prekey_signature_length);
xmpp_stanza_set_text(signed_prekey_signature_stanza_text, signed_prekey_signature_b64);
g_free(signed_prekey_signature_b64);
xmpp_stanza_add_child(signed_prekey_signature_stanza, signed_prekey_signature_stanza_text);
xmpp_stanza_release(signed_prekey_signature_stanza_text);
@@ -2373,9 +2370,8 @@ stanza_create_omemo_bundle_publish(xmpp_ctx_t* ctx, const char* const id,
xmpp_stanza_set_name(identity_key_stanza, "identityKey");
xmpp_stanza_t* identity_key_stanza_text = xmpp_stanza_new(ctx);
char* identity_key_b64 = g_base64_encode(identity_key, identity_key_length);
auto_gchar gchar* identity_key_b64 = g_base64_encode(identity_key, identity_key_length);
xmpp_stanza_set_text(identity_key_stanza_text, identity_key_b64);
g_free(identity_key_b64);
xmpp_stanza_add_child(identity_key_stanza, identity_key_stanza_text);
xmpp_stanza_release(identity_key_stanza_text);
@@ -2390,9 +2386,8 @@ stanza_create_omemo_bundle_publish(xmpp_ctx_t* ctx, const char* const id,
xmpp_stanza_set_attribute(prekey, "preKeyId", id);
xmpp_stanza_t* prekey_text = xmpp_stanza_new(ctx);
char* prekey_b64 = g_base64_encode(p->data, GPOINTER_TO_INT(l->data));
auto_gchar gchar* prekey_b64 = g_base64_encode(p->data, GPOINTER_TO_INT(l->data));
xmpp_stanza_set_text(prekey_text, prekey_b64);
g_free(prekey_b64);
xmpp_stanza_add_child(prekey, prekey_text);
xmpp_stanza_add_child(prekeys_stanza, prekey);
@@ -2517,7 +2512,7 @@ _stanza_add_unique_id(xmpp_stanza_t* stanza)
xmpp_stanza_set_id(stanza, id);
}
static char*
static gchar*
_stanza_create_sha1_hash(char* str)
{
unsigned char* digest = (unsigned char*)malloc(XMPP_SHA1_DIGEST_SIZE);
@@ -2525,7 +2520,7 @@ _stanza_create_sha1_hash(char* str)
xmpp_sha1_digest((unsigned char*)str, strlen(str), digest);
char* b64 = g_base64_encode(digest, XMPP_SHA1_DIGEST_SIZE);
gchar* b64 = g_base64_encode(digest, XMPP_SHA1_DIGEST_SIZE);
assert(b64 != NULL);
free(digest);

View File

@@ -380,7 +380,7 @@ void stanza_attach_show(xmpp_ctx_t* const ctx, xmpp_stanza_t* const presence, co
void stanza_attach_status(xmpp_ctx_t* const ctx, xmpp_stanza_t* const presence, const char* const status);
xmpp_stanza_t* stanza_create_caps_query_element(xmpp_ctx_t* ctx);
char* stanza_create_caps_sha1_from_query(xmpp_stanza_t* const query);
gchar* stanza_create_caps_sha1_from_query(xmpp_stanza_t* const query);
EntityCapabilities* stanza_create_caps_from_query_element(xmpp_stanza_t* query);
const char* stanza_get_presence_string_from_type(resource_presence_t presence_type);

View File

@@ -757,10 +757,9 @@ vcard_to_xml(xmpp_ctx_t* const ctx, vCard* vcard)
xmpp_stanza_t* binval = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(binval, "BINVAL");
gchar* base64 = g_base64_encode(element->photo.data, element->photo.length);
auto_gchar gchar* base64 = g_base64_encode(element->photo.data, element->photo.length);
xmpp_stanza_t* binval_text = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(binval_text, base64);
g_free(base64);
xmpp_stanza_add_child(binval, binval_text);
xmpp_stanza_release(binval_text);
@@ -789,10 +788,9 @@ vcard_to_xml(xmpp_ctx_t* const ctx, vCard* vcard)
xmpp_stanza_t* birthday = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(birthday, "BDAY");
gchar* bday_text = g_date_time_format(element->birthday, "%Y-%m-%d");
auto_gchar gchar* bday_text = g_date_time_format(element->birthday, "%Y-%m-%d");
xmpp_stanza_t* birthday_text = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(birthday_text, bday_text);
g_free(bday_text);
xmpp_stanza_add_child(birthday, birthday_text);
xmpp_stanza_release(birthday_text);