1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Cleanup and mem leak fix

Improve usage of `gchar` and `char` by setting them correctly
Increase usage of `auto_gchar` and `auto_char`
Fix 2 mem leaks (rosterwin.c, avatar.c)
This commit is contained in:
John Hernandez 2023-07-12 17:43:33 +02:00
parent 9451ea7c4c
commit 3a4cd7da48
9 changed files with 31 additions and 43 deletions

View File

@ -406,7 +406,7 @@ _get_log_filename(const char* const other, const char* const login, GDateTime* d
{
auto_gchar gchar* chatlogs_dir = files_file_in_account_data_path(DIR_CHATLOGS, login, is_room ? "rooms" : NULL);
auto_gchar gchar* logfile_name = g_date_time_format(dt, "%Y_%m_%d.log");
auto_gchar gchar* other_ = str_replace(other, "@", "_at_");
auto_char char* other_ = str_replace(other, "@", "_at_");
auto_gchar gchar* logs_path = g_strdup_printf("%s/%s", chatlogs_dir, other_);
gchar* logfile_path = NULL;

View File

@ -9270,15 +9270,15 @@ gboolean
cmd_stamp(ProfWin* window, const char* const command, gchar** args)
{
if (g_strv_length(args) == 0) {
auto_gchar gchar* def = prefs_get_string(PREF_OUTGOING_STAMP);
if (def) {
cons_show("The outgoing stamp is: %s", def);
auto_gchar gchar* def_incoming = prefs_get_string(PREF_OUTGOING_STAMP);
if (def_incoming) {
cons_show("The outgoing stamp is: %s", def_incoming);
} else {
cons_show("The default outgoing stamp is used.");
}
def = prefs_get_string(PREF_INCOMING_STAMP);
if (def) {
cons_show("The incoming stamp is: %s", def);
auto_gchar gchar* def_outgoing = prefs_get_string(PREF_INCOMING_STAMP);
if (def_outgoing) {
cons_show("The incoming stamp is: %s", def_outgoing);
} else {
cons_show("The default incoming stamp is used.");
}
@ -9687,12 +9687,11 @@ _cmd_executable_template(const preference_t setting, const char* command, gchar*
} else if (g_strcmp0(args[1], "default") == 0) {
prefs_set_string(setting, NULL);
gchar* def = prefs_get_string(setting);
auto_gchar gchar* def = prefs_get_string(setting);
if (def == NULL) {
def = g_strdup("built-in method");
}
cons_show("`%s` command set to invoke %s (default)", command, def);
g_free(def);
} else {
cons_bad_cmd_usage(command);
}

View File

@ -176,7 +176,7 @@ gchar*
files_get_account_data_path(const char* const specific_dir, const char* const jid)
{
auto_gchar gchar* data_dir = files_get_data_path(specific_dir);
auto_gchar gchar* account_dir = str_replace(jid, "@", "_at_");
auto_char char* account_dir = str_replace(jid, "@", "_at_");
return g_strdup_printf("%s/%s", data_dir, account_dir);
}

View File

@ -541,13 +541,13 @@ prefs_get_string(preference_t pref)
}
gchar*
prefs_get_string_with_option(preference_t pref, gchar* option)
prefs_get_string_with_locale(preference_t pref, gchar* locale)
{
const char* group = _get_group(pref);
const char* key = _get_key(pref);
char* def = _get_default_string(pref);
gchar* result = g_key_file_get_locale_string(prefs, group, key, option, NULL);
gchar* result = g_key_file_get_locale_string(prefs, group, key, locale, NULL);
if (result == NULL) {
// check for user set default
@ -566,14 +566,14 @@ prefs_get_string_with_option(preference_t pref, gchar* option)
}
void
prefs_set_string(preference_t pref, char* value)
prefs_set_string(preference_t pref, gchar* new_value)
{
const char* group = _get_group(pref);
const char* key = _get_key(pref);
if (value == NULL) {
if (new_value == NULL) {
g_key_file_remove_key(prefs, group, key, NULL);
} else {
g_key_file_set_string(prefs, group, key, value);
g_key_file_set_string(prefs, group, key, new_value);
}
}

View File

@ -336,8 +336,8 @@ void prefs_save_win_placement(ProfWinPlacement* placement);
gboolean prefs_get_boolean(preference_t pref);
void prefs_set_boolean(preference_t pref, gboolean value);
gchar* prefs_get_string(preference_t pref);
gchar* prefs_get_string_with_option(preference_t pref, gchar* option);
void prefs_set_string(preference_t pref, char* value);
gchar* prefs_get_string_with_locale(preference_t pref, gchar* locale);
void prefs_set_string(preference_t pref, gchar* new_value);
void prefs_set_string_with_option(preference_t pref, char* option, char* value);
void prefs_set_string_list_with_option(preference_t pref, char* option, const gchar* const* values);

View File

@ -2198,30 +2198,25 @@ cons_correction_setting(void)
void
cons_executable_setting(void)
{
gchar* avatar = prefs_get_string(PREF_AVATAR_CMD);
auto_gchar gchar* avatar = prefs_get_string(PREF_AVATAR_CMD);
cons_show("Default '/avatar open' command (/executable avatar) : %s", avatar);
g_free(avatar);
// TODO: there needs to be a way to get all the "locales"/schemes so we can
// display the default openers for all filetypes
gchar* urlopen = prefs_get_string(PREF_URL_OPEN_CMD);
auto_gchar gchar* urlopen = prefs_get_string(PREF_URL_OPEN_CMD);
cons_show("Default '/url open' command (/executable urlopen) : %s", urlopen);
g_free(urlopen);
gchar* urlsave = prefs_get_string(PREF_URL_SAVE_CMD);
auto_gchar gchar* urlsave = prefs_get_string(PREF_URL_SAVE_CMD);
if (urlsave == NULL) {
urlsave = g_strdup("(built-in)");
}
cons_show("Default '/url save' command (/executable urlsave) : %s", urlsave);
g_free(urlsave);
gchar* editor = prefs_get_string(PREF_COMPOSE_EDITOR);
auto_gchar gchar* editor = prefs_get_string(PREF_COMPOSE_EDITOR);
cons_show("Default '/editor' command (/executable editor) : %s", editor);
g_free(editor);
gchar* vcard_cmd = prefs_get_string(PREF_VCARD_PHOTO_CMD);
auto_gchar gchar* vcard_cmd = prefs_get_string(PREF_VCARD_PHOTO_CMD);
cons_show("Default '/vcard photo open' command (/executable vcard_photo) : %s", vcard_cmd);
g_free(vcard_cmd);
}
void

View File

@ -830,8 +830,8 @@ _rosterwin_room(ProfLayoutSplit* layout, ProfMucWin* mucwin)
}
}
unreadpos = prefs_get_string(PREF_ROSTER_ROOMS_UNREAD);
if ((g_strcmp0(unreadpos, "before") == 0) && privwin->unread > 0) {
auto_gchar gchar* unreadpos2 = prefs_get_string(PREF_ROSTER_ROOMS_UNREAD);
if ((g_strcmp0(unreadpos2, "before") == 0) && privwin->unread > 0) {
g_string_append_printf(privmsg, "(%d) ", privwin->unread);
}
@ -843,7 +843,7 @@ _rosterwin_room(ProfLayoutSplit* layout, ProfMucWin* mucwin)
char* nick = privwin->fulljid + strlen(mucwin->roomjid) + 1;
g_string_append(privmsg, nick);
if ((g_strcmp0(unreadpos, "after") == 0) && privwin->unread > 0) {
if ((g_strcmp0(unreadpos2, "after") == 0) && privwin->unread > 0) {
g_string_append_printf(privmsg, " (%d)", privwin->unread);
}

View File

@ -317,7 +317,7 @@ _avatar_request_item_result_handler(xmpp_stanza_t* const stanza, void* const use
}
}
auto_gchar gchar* from = str_replace(from_attr, "@", "_at_");
auto_char char* from = str_replace(from_attr, "@", "_at_");
g_string_append(filename, from);
avatar_metadata* data = (avatar_metadata*)userdata;
@ -345,18 +345,16 @@ _avatar_request_item_result_handler(xmpp_stanza_t* const stanza, void* const use
// if we shall open it
if (g_hash_table_contains(shall_open, from_attr)) {
gchar* cmdtemplate = prefs_get_string(PREF_AVATAR_CMD);
auto_gchar gchar* cmdtemplate = prefs_get_string(PREF_AVATAR_CMD);
if (cmdtemplate == NULL) {
cons_show_error("No default `avatar open` command found in executables preferences.");
} else {
gchar** argv = format_call_external_argv(cmdtemplate, NULL, filename->str);
auto_gcharv gchar** argv = format_call_external_argv(cmdtemplate, NULL, filename->str);
if (!call_external(argv)) {
cons_show_error("Unable to display avatar: check the logs for more information.");
}
g_strfreev(argv);
}
g_hash_table_remove(shall_open, from_attr);

View File

@ -1322,12 +1322,10 @@ _vcard_photo_result(xmpp_stanza_t* const stanza, void* userdata)
g_string_free(filename, TRUE);
}
}
gchar* from1 = str_replace(from, "@", "_at_");
gchar* from2 = str_replace(from1, "/", "_slash_");
g_free(from1);
auto_char char* from1 = str_replace(from, "@", "_at_");
auto_char char* from2 = str_replace(from1, "/", "_slash_");
g_string_append(filename, from2);
g_free(from2);
} else {
filename = g_string_new(data->filename);
}
@ -1357,17 +1355,15 @@ _vcard_photo_result(xmpp_stanza_t* const stanza, void* userdata)
gchar** argv;
gint argc;
gchar* cmdtemplate = prefs_get_string(PREF_VCARD_PHOTO_CMD);
auto_gchar gchar* cmdtemplate = prefs_get_string(PREF_VCARD_PHOTO_CMD);
// this makes it work with filenames that contain spaces
g_string_prepend(filename, "\"");
g_string_append(filename, "\"");
gchar* cmd = str_replace(cmdtemplate, "%p", filename->str);
g_free(cmdtemplate);
auto_char char* cmd = str_replace(cmdtemplate, "%p", filename->str);
if (g_shell_parse_argv(cmd, &argc, &argv, &err) == FALSE) {
cons_show_error("Failed to parse command template");
g_free(cmd);
} else {
if (!call_external(argv)) {
cons_show_error("Unable to execute command");