From 9a9122c148a8462eb612c7a28ab0f2e3f2f0464e Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Mon, 1 Nov 2021 12:24:28 +0100 Subject: [PATCH] Cleanup _get_message_from_editor a bit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix `src/command/cmd_funcs.c:9463:9: error: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Werror=unused-result]` * Free memory earlier and on less places * Check for succesful open() and write() --- src/command/cmd_funcs.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 9256a933..74bbc5e0 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9443,29 +9443,36 @@ _get_message_from_editor(gchar* message, gchar** returned_message) // create editor dir if not present char* jid = connection_get_barejid(); gchar* path = files_get_account_data_path(DIR_EDITOR, jid); + free(jid); if (g_mkdir_with_parents(path, S_IRWXU) != 0) { cons_show_error("Failed to create directory at '%s' with error '%s'", path, strerror(errno)); - free(jid); g_free(path); return TRUE; } + // build temp file name. Example: /home/user/.local/share/profanity/editor/jid/compose.md char* filename = g_strdup_printf("%s/compose.md", path); - free(jid); g_free(path); GError* creation_error = NULL; GFile* file = g_file_new_for_path(filename); GFileOutputStream* fos = g_file_create(file, G_FILE_CREATE_PRIVATE, NULL, &creation_error); + free(filename); + if (message != NULL && strlen(message) > 0) { int fd_output_file = open(g_file_get_path(file), O_WRONLY); - write(fd_output_file, message, strlen(message)); + if (fd_output_file < 0) { + cons_show_error("Editor: Could not open file '%s': %s", file, strerror(errno)); + return TRUE; + } + if (-1 == write(fd_output_file, message, strlen(message))) { + cons_show_error("Editor: failed to write '%s' to file: %s", message, strerror(errno)); + return TRUE; + } close(fd_output_file); } - free(filename); - if (creation_error) { cons_show_error("Editor: could not create temp file"); return TRUE;