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

Fix memleaks & more auto-free

`data_dir` would have been leaked if directory creation failed.

`editor_argv` was leaked at some point, no idea why.

```
==1244734== 118 (32 direct, 86 indirect) bytes in 1 blocks are definitely lost in loss record 6,299 of 7,824
==1244734==    at 0x4846CC3: realloc (vg_replace_malloc.c:1451)
==1244734==    by 0x5E85AD0: g_realloc (in /usr/lib/libglib-2.0.so.0.7600.1)
==1244734==    by 0x5E4A004: ??? (in /usr/lib/libglib-2.0.so.0.7600.1)
==1244734==    by 0x5E4A7B1: g_ptr_array_add (in /usr/lib/libglib-2.0.so.0.7600.1)
==1244734==    by 0x5EA4235: g_strsplit (in /usr/lib/libglib-2.0.so.0.7600.1)
==1244734==    by 0x1F143C: get_message_from_editor (editor.c:92)
==1244734==    by 0x193F6B: _inp_rl_send_to_editor (inputwin.c:950)
==1244734==    by 0x614642F: _rl_dispatch_subseq (readline.c:916)
==1244734==    by 0x6146C85: _rl_dispatch_callback (readline.c:823)
==1244734==    by 0x616739F: rl_callback_read_char (callback.c:241)
==1244734==    by 0x1923DB: inp_readline (inputwin.c:188)
==1244734==    by 0x149860: prof_run (profanity.c:117)
==1244734==    by 0x2283E8: main (main.c:186)
```

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
This commit is contained in:
Steffen Jaeckel 2023-05-11 11:11:44 +02:00 committed by Michael Vetter
parent 76a8de891e
commit 12d76e4a21

View File

@ -54,19 +54,18 @@ get_message_from_editor(gchar* message, gchar** returned_message)
/* Make sure that there's no junk in the return-pointer in error cases */ /* Make sure that there's no junk in the return-pointer in error cases */
*returned_message = NULL; *returned_message = NULL;
gchar* filename = NULL; auto_gchar gchar* filename = NULL;
GError* glib_error = NULL; GError* glib_error = NULL;
auto_char char* jid = connection_get_barejid(); auto_char char* jid = connection_get_barejid();
if (jid) { if (jid) {
filename = files_file_in_account_data_path(DIR_EDITOR, jid, "compose.md"); filename = files_file_in_account_data_path(DIR_EDITOR, jid, "compose.md");
} else { } else {
log_debug("[Editor] could not get JID"); log_debug("[Editor] could not get JID");
gchar* data_dir = files_get_data_path(DIR_EDITOR); auto_gchar gchar* data_dir = files_get_data_path(DIR_EDITOR);
if (!create_dir(data_dir)) { if (!create_dir(data_dir)) {
return TRUE; return TRUE;
} }
filename = g_strdup_printf("%s/compose.md", data_dir); filename = g_strdup_printf("%s/compose.md", data_dir);
g_free(data_dir);
} }
if (!filename) { if (!filename) {
log_error("[Editor] something went wrong while creating compose file"); log_error("[Editor] something went wrong while creating compose file");
@ -83,21 +82,17 @@ get_message_from_editor(gchar* message, gchar** returned_message)
if (glib_error) { if (glib_error) {
g_error_free(glib_error); g_error_free(glib_error);
} }
g_free(filename);
return TRUE; return TRUE;
} }
char* editor = prefs_get_string(PREF_COMPOSE_EDITOR); auto_gchar gchar* editor = prefs_get_string(PREF_COMPOSE_EDITOR);
gchar* editor_with_filename = g_strdup_printf("%s %s", editor, filename); auto_gchar gchar* editor_with_filename = g_strdup_printf("%s %s", editor, filename);
gchar** editor_argv = g_strsplit(editor_with_filename, " ", 0); auto_gcharv gchar** editor_argv = g_strsplit(editor_with_filename, " ", 0);
g_free(editor_with_filename);
// Fork / exec // Fork / exec
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid == 0) {
int x = execvp(editor_argv[0], editor_argv); int x = execvp(editor_argv[0], editor_argv);
g_strfreev(editor_argv);
if (x == -1) { if (x == -1) {
log_error("[Editor] Failed to exec %s", editor); log_error("[Editor] Failed to exec %s", editor);
} }
@ -115,8 +110,6 @@ get_message_from_editor(gchar* message, gchar** returned_message)
if (glib_error) { if (glib_error) {
g_error_free(glib_error); g_error_free(glib_error);
} }
g_free(filename);
g_free(editor);
return TRUE; return TRUE;
} }
/* Remove all trailing new-line characters */ /* Remove all trailing new-line characters */
@ -127,10 +120,7 @@ get_message_from_editor(gchar* message, gchar** returned_message)
} else { } else {
log_debug("[Editor] deleted file: %s", filename); log_debug("[Editor] deleted file: %s", filename);
} }
g_free(filename);
} }
g_free(editor);
return FALSE; return FALSE;
} }