From f230c46e5ef3d091ef9a3a304819378bd6748b69 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Sun, 27 Oct 2019 10:25:37 +0100 Subject: [PATCH] tweaks: drop the unneeded closing of descriptors when exiting anyway Also, when something goes wrong in the later stages of the pipeline, the error code would try to close descriptors that are already closed, which is not right. So, just let exit() handle any file descriptors that are still open. --- src/text.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/text.c b/src/text.c index 19b75ea0..f1c7b288 100644 --- a/src/text.c +++ b/src/text.c @@ -2361,19 +2361,17 @@ const char *do_int_speller(const char *tempfile_name) /* Child: open the temporary file that holds the text to be checked. */ if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1) - goto close_pipes_and_exit; + exit(6); /* Connect standard input to the temporary file. */ - if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO) { - close(tempfile_fd); - goto close_pipes_and_exit; - } + if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO) + exit(7); close(tempfile_fd); /* Connect standard output to the write end of the first pipe. */ if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO) - goto close_pipes_and_exit; + exit(8); close(spell_fd[1]); @@ -2391,13 +2389,13 @@ const char *do_int_speller(const char *tempfile_name) if ((pid_sort = fork()) == 0) { /* Connect standard input to the read end of the first pipe. */ if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO) - goto close_pipes_and_exit; + exit(7); close(spell_fd[0]); /* Connect standard output to the write end of the second pipe. */ if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO) - goto close_pipes_and_exit; + exit(8); close(sort_fd[1]); @@ -2413,12 +2411,12 @@ const char *do_int_speller(const char *tempfile_name) /* Fork a process to run uniq in. */ if ((pid_uniq = fork()) == 0) { if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO) - goto close_pipes_and_exit; + exit(7); close(sort_fd[0]); if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO) - goto close_pipes_and_exit; + exit(8); close(uniq_fd[1]); @@ -2511,16 +2509,6 @@ const char *do_int_speller(const char *tempfile_name) /* When all went okay. */ statusbar(_("Finished checking spelling")); return NULL; - - close_pipes_and_exit: - /* Don't leak any handles. */ - close(spell_fd[0]); - close(spell_fd[1]); - close(sort_fd[0]); - close(sort_fd[1]); - close(uniq_fd[0]); - close(uniq_fd[1]); - exit(1); } /* Execute the given program, with the given temp file as last argument. */