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

@@ -127,7 +127,7 @@ accounts_add(const char* account_name, const char* altdomain, const int port, co
{
// set account name and resource
const char* barejid = account_name;
char* resource = jid_random_resource();
auto_gchar gchar* resource = jid_random_resource();
Jid* jid = jid_create(account_name);
if (jid) {
barejid = jid->barejid;
@@ -137,7 +137,6 @@ accounts_add(const char* account_name, const char* altdomain, const int port, co
}
if (g_key_file_has_group(accounts, account_name)) {
g_free(resource);
jid_destroy(jid);
return;
}
@@ -181,7 +180,6 @@ accounts_add(const char* account_name, const char* altdomain, const int port, co
autocomplete_add(enabled_ac, account_name);
jid_destroy(jid);
g_free(resource);
}
int
@@ -433,10 +431,9 @@ accounts_rename(const char* const account_name, const char* const new_name)
};
for (int i = 0; i < ARRAY_SIZE(string_keys); i++) {
char* value = g_key_file_get_string(accounts, account_name, string_keys[i], NULL);
auto_gchar gchar* value = g_key_file_get_string(accounts, account_name, string_keys[i], NULL);
if (value) {
g_key_file_set_string(accounts, new_name, string_keys[i], value);
g_free(value);
}
}
@@ -868,7 +865,7 @@ accounts_set_last_activity(const char* const account_name)
}
}
char*
gchar*
accounts_get_last_activity(const char* const account_name)
{
if (accounts_account_exists(account_name)) {
@@ -878,7 +875,7 @@ accounts_get_last_activity(const char* const account_name)
}
}
char*
gchar*
accounts_get_resource(const char* const account_name)
{
if (!accounts_account_exists(account_name)) {
@@ -909,7 +906,7 @@ resource_presence_t
accounts_get_last_presence(const char* const account_name)
{
resource_presence_t result;
gchar* setting = g_key_file_get_string(accounts, account_name, "presence.last", NULL);
auto_gchar gchar* setting = g_key_file_get_string(accounts, account_name, "presence.last", NULL);
if (setting == NULL || (strcmp(setting, "online") == 0)) {
result = RESOURCE_ONLINE;
@@ -927,7 +924,6 @@ accounts_get_last_presence(const char* const account_name)
result = RESOURCE_ONLINE;
}
g_free(setting);
return result;
}

View File

@@ -61,7 +61,7 @@ void accounts_set_jid(const char* const account_name, const char* const value);
void accounts_set_server(const char* const account_name, const char* const value);
void accounts_set_port(const char* const account_name, const int value);
void accounts_set_resource(const char* const account_name, const char* const value);
char* accounts_get_resource(const char* const account_name);
gchar* accounts_get_resource(const char* const account_name);
void accounts_set_password(const char* const account_name, const char* const value);
void accounts_set_eval_password(const char* const account_name, const char* const value);
void accounts_set_muc_service(const char* const account_name, const char* const value);
@@ -72,7 +72,7 @@ void accounts_set_auth_policy(const char* const account_name, const char* const
void accounts_set_last_presence(const char* const account_name, const char* const value);
void accounts_set_last_status(const char* const account_name, const char* const value);
void accounts_set_last_activity(const char* const account_name);
char* accounts_get_last_activity(const char* const account_name);
gchar* accounts_get_last_activity(const char* const account_name);
void accounts_set_login_presence(const char* const account_name, const char* const value);
resource_presence_t accounts_get_login_presence(const char* const account_name);
char* accounts_get_last_status(const char* const account_name);

View File

@@ -64,30 +64,27 @@ cafile_add(const TLSCertificate* cert)
log_error("[CAfile] can't store cert with fingerprint %s: PEM is empty", cert->fingerprint);
return;
}
gchar* cafile = _cafile_name();
auto_gchar gchar* cafile = _cafile_name();
if (!cafile)
return;
gchar *contents = NULL, *new_contents = NULL;
auto_gchar gchar* contents = NULL;
auto_gchar gchar* new_contents = NULL;
gsize length;
GError* glib_error = NULL;
if (g_file_test(cafile, G_FILE_TEST_EXISTS)) {
if (!g_file_get_contents(cafile, &contents, &length, &glib_error)) {
log_error("[CAfile] could not read from %s: %s", cafile, glib_error ? glib_error->message : "No GLib error given");
goto out;
return;
}
if (strstr(contents, cert->fingerprint)) {
log_debug("[CAfile] fingerprint %s already stored", cert->fingerprint);
goto out;
return;
}
}
const char* header = "# Profanity CAfile\n# DO NOT EDIT - this file is automatically generated";
new_contents = g_strdup_printf("%s\n\n# %s\n%s", contents ? contents : header, cert->fingerprint, cert->pem);
if (!g_file_set_contents(cafile, new_contents, -1, &glib_error))
log_error("[CAfile] could not write to %s: %s", cafile, glib_error ? glib_error->message : "No GLib error given");
out:
g_free(new_contents);
g_free(contents);
g_free(cafile);
}
gchar*

View File

@@ -53,8 +53,8 @@ static char* _files_get_xdg_data_home(void);
void
files_create_directories(void)
{
gchar* xdg_config = _files_get_xdg_config_home();
gchar* xdg_data = _files_get_xdg_data_home();
auto_gchar gchar* xdg_config = _files_get_xdg_config_home();
auto_gchar gchar* xdg_data = _files_get_xdg_data_home();
GString* themes_dir = g_string_new(xdg_config);
g_string_append(themes_dir, "/profanity/themes");
@@ -88,17 +88,13 @@ files_create_directories(void)
g_string_free(chatlogs_dir, TRUE);
g_string_free(logs_dir, TRUE);
g_string_free(plugins_dir, TRUE);
g_free(xdg_config);
g_free(xdg_data);
}
gchar*
files_get_inputrc_file(void)
{
gchar* xdg_config = _files_get_xdg_config_home();
auto_gchar gchar* xdg_config = _files_get_xdg_config_home();
GString* inputrc_file = g_string_new(xdg_config);
g_free(xdg_config);
g_string_append(inputrc_file, "/profanity/inputrc");
@@ -121,11 +117,10 @@ files_get_log_file(const char* const log_file)
GString* logfile;
if (log_file) {
gchar* log_path = g_path_get_dirname(log_file);
auto_gchar gchar* log_path = g_path_get_dirname(log_file);
if (!create_dir(log_path)) {
log_error("Error while creating directory %s", log_path);
}
g_free(log_path);
logfile = g_string_new(log_file);
} else {
@@ -149,9 +144,8 @@ files_get_log_file(const char* const log_file)
gchar*
files_get_config_path(const char* const config_base)
{
gchar* xdg_config = _files_get_xdg_config_home();
auto_gchar gchar* xdg_config = _files_get_xdg_config_home();
gchar* result = g_strdup_printf("%s/profanity/%s", xdg_config, config_base);
g_free(xdg_config);
return result;
}

View File

@@ -237,10 +237,9 @@ GSList*
theme_list(void)
{
GSList* result = NULL;
gchar* themes_dir = files_get_config_path(DIR_THEMES);
auto_gchar gchar* themes_dir = files_get_config_path(DIR_THEMES);
_theme_list_dir(themes_dir, &result);
g_free(themes_dir);
#ifdef THEMES_PATH
_theme_list_dir(THEMES_PATH, &result);
@@ -281,9 +280,8 @@ static void
_set_string_preference(char* prefstr, preference_t pref)
{
if (g_key_file_has_key(theme, "ui", prefstr, NULL)) {
gchar* val = g_key_file_get_string(theme, "ui", prefstr, NULL);
auto_gchar gchar* val = g_key_file_get_string(theme, "ui", prefstr, NULL);
prefs_set_string(pref, val);
g_free(val);
}
}
@@ -408,100 +406,88 @@ _load_preferences(void)
// load chars from theme and set them to prefs
// with custom set functions
if (g_key_file_has_key(theme, "ui", "occupants.char", NULL)) {
gchar* ch = g_key_file_get_string(theme, "ui", "occupants.char", NULL);
auto_gchar gchar* ch = g_key_file_get_string(theme, "ui", "occupants.char", NULL);
if (ch && g_utf8_strlen(ch, 4) == 1) {
prefs_set_occupants_char(ch);
g_free(ch);
}
}
if (g_key_file_has_key(theme, "ui", "occupants.header.char", NULL)) {
gchar* ch = g_key_file_get_string(theme, "ui", "occupants.header.char", NULL);
auto_gchar gchar* ch = g_key_file_get_string(theme, "ui", "occupants.header.char", NULL);
if (ch && g_utf8_strlen(ch, 4) == 1) {
prefs_set_occupants_header_char(ch);
g_free(ch);
}
}
if (g_key_file_has_key(theme, "ui", "roster.header.char", NULL)) {
gchar* ch = g_key_file_get_string(theme, "ui", "roster.header.char", NULL);
auto_gchar gchar* ch = g_key_file_get_string(theme, "ui", "roster.header.char", NULL);
if (ch && g_utf8_strlen(ch, 4) == 1) {
prefs_set_roster_header_char(ch);
g_free(ch);
}
}
if (g_key_file_has_key(theme, "ui", "roster.contact.char", NULL)) {
gchar* ch = g_key_file_get_string(theme, "ui", "roster.contact.char", NULL);
auto_gchar gchar* ch = g_key_file_get_string(theme, "ui", "roster.contact.char", NULL);
if (ch && g_utf8_strlen(ch, 4) == 1) {
prefs_set_roster_contact_char(ch);
g_free(ch);
}
}
if (g_key_file_has_key(theme, "ui", "roster.resource.char", NULL)) {
gchar* ch = g_key_file_get_string(theme, "ui", "roster.resource.char", NULL);
auto_gchar gchar* ch = g_key_file_get_string(theme, "ui", "roster.resource.char", NULL);
if (ch && g_utf8_strlen(ch, 4) == 1) {
prefs_set_roster_resource_char(ch);
g_free(ch);
}
} else {
prefs_clear_roster_resource_char();
}
if (g_key_file_has_key(theme, "ui", "roster.rooms.char", NULL)) {
gchar* ch = g_key_file_get_string(theme, "ui", "roster.rooms.char", NULL);
auto_gchar gchar* ch = g_key_file_get_string(theme, "ui", "roster.rooms.char", NULL);
if (ch && g_utf8_strlen(ch, 4) == 1) {
prefs_set_roster_room_char(ch);
g_free(ch);
}
}
if (g_key_file_has_key(theme, "ui", "roster.rooms.private.char", NULL)) {
gchar* ch = g_key_file_get_string(theme, "ui", "roster.rooms.private.char", NULL);
auto_gchar gchar* ch = g_key_file_get_string(theme, "ui", "roster.rooms.private.char", NULL);
if (ch && g_utf8_strlen(ch, 4) == 1) {
prefs_set_roster_room_private_char(ch);
g_free(ch);
}
}
if (g_key_file_has_key(theme, "ui", "roster.private.char", NULL)) {
gchar* ch = g_key_file_get_string(theme, "ui", "roster.private.char", NULL);
auto_gchar gchar* ch = g_key_file_get_string(theme, "ui", "roster.private.char", NULL);
if (ch && g_utf8_strlen(ch, 4) == 1) {
prefs_set_roster_private_char(ch);
g_free(ch);
}
}
if (g_key_file_has_key(theme, "ui", "otr.char", NULL)) {
gchar* ch = g_key_file_get_string(theme, "ui", "otr.char", NULL);
auto_gchar gchar* ch = g_key_file_get_string(theme, "ui", "otr.char", NULL);
if (ch && g_utf8_strlen(ch, 4) == 1) {
prefs_set_otr_char(ch);
g_free(ch);
}
}
if (g_key_file_has_key(theme, "ui", "pgp.char", NULL)) {
gchar* ch = g_key_file_get_string(theme, "ui", "pgp.char", NULL);
auto_gchar gchar* ch = g_key_file_get_string(theme, "ui", "pgp.char", NULL);
if (ch && g_utf8_strlen(ch, 4) == 1) {
prefs_set_pgp_char(ch);
g_free(ch);
}
}
if (g_key_file_has_key(theme, "ui", "omemo.char", NULL)) {
gchar* ch = g_key_file_get_string(theme, "ui", "omemo.char", NULL);
auto_gchar gchar* ch = g_key_file_get_string(theme, "ui", "omemo.char", NULL);
if (ch && g_utf8_strlen(ch, 4) == 1) {
prefs_set_omemo_char(ch);
g_free(ch);
}
}
if (g_key_file_has_key(theme, "ui", "correction.char", NULL)) {
gchar* ch = g_key_file_get_string(theme, "ui", "correction.char", NULL);
auto_gchar gchar* ch = g_key_file_get_string(theme, "ui", "correction.char", NULL);
if (ch && strlen(ch) > 0) {
prefs_set_correction_char(ch[0]);
g_free(ch);
}
}
@@ -538,11 +524,10 @@ static GString*
_theme_find(const char* const theme_name)
{
GString* path = NULL;
gchar* themes_dir = files_get_config_path(DIR_THEMES);
auto_gchar gchar* themes_dir = files_get_config_path(DIR_THEMES);
if (themes_dir) {
path = g_string_new(themes_dir);
g_free(themes_dir);
g_string_append(path, "/");
g_string_append(path, theme_name);
if (!g_file_test(path->str, G_FILE_TEST_EXISTS)) {
@@ -641,7 +626,7 @@ theme_main_presence_attrs(const char* const presence)
static void
_theme_prep_bgnd(char* setting, char* def, GString* lookup_str)
{
gchar* val = g_key_file_get_string(theme, "colours", setting, NULL);
auto_gchar gchar* val = g_key_file_get_string(theme, "colours", setting, NULL);
if (!val) {
g_string_append(lookup_str, def);
} else {
@@ -651,7 +636,6 @@ _theme_prep_bgnd(char* setting, char* def, GString* lookup_str)
g_string_append(lookup_str, val);
}
}
g_free(val);
}
/* return value needs to be freed */
@@ -666,7 +650,7 @@ theme_get_bkgnd(void)
static void
_theme_prep_fgnd(char* setting, GString* lookup_str, gboolean* bold)
{
gchar* conf_str = g_key_file_get_string(theme, "colours", setting, NULL);
auto_gchar gchar* conf_str = g_key_file_get_string(theme, "colours", setting, NULL);
gchar* val = conf_str;
if (!val)
@@ -679,8 +663,6 @@ _theme_prep_fgnd(char* setting, GString* lookup_str, gboolean* bold)
g_string_append(lookup_str, val);
*bold = FALSE;
}
g_free(conf_str);
}
char*

View File

@@ -387,8 +387,7 @@ static void
_save_tlscerts(void)
{
gsize g_data_size;
gchar* g_tlscerts_data = g_key_file_to_data(tlscerts, &g_data_size, NULL);
auto_gchar gchar* g_tlscerts_data = g_key_file_to_data(tlscerts, &g_data_size, NULL);
g_file_set_contents(tlscerts_loc, g_tlscerts_data, g_data_size, NULL);
g_chmod(tlscerts_loc, S_IRUSR | S_IWUSR);
g_free(g_tlscerts_data);
}