[PATCH] spelling: properly check for a negative return value from read()

Benno Schulenberg <[email protected]> Mon, 25 May 2026 15:54:38 +0200
Newsgroups gmane.editors.nano.devel
Message-ID <[email protected]>
This addresses the first part of
  https://lists.gnu.org/archive/html/nano-devel/2026-05/msg00040.html.
Reported-by: Arjun Basnet <[email protected]>

Problem existed since version 1.1.8, commit f21f3fcc.


[This leaks some memory when the pipe-reading error occurs,
but that is addressed in a subsequent local commit.]

---
 src/text.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/text.c b/src/text.c
index 137f5786..566a352a 100644
--- a/src/text.c
+++ b/src/text.c
@@ -2349,7 +2349,9 @@ void do_int_speller(const char *tempfile_name)
 #if defined(HAVE_FORK) && defined(HAVE_WAITPID)
 	char *misspellings, *pointer, *oneword;
 	long pipesize;
-	size_t buffersize, bytesread, totalread;
+	ssize_t bytesread;
+	size_t buffersize, totalread;
+	int errornumber;
 	int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1;
 	pid_t pid_spell, pid_sort, pid_uniq;
 	int spell_status, sort_status, uniq_status;
@@ -2470,6 +2472,7 @@ void do_int_speller(const char *tempfile_name)
 	}
 
 	*pointer = '\0';
+	errornumber = errno;
 	close(uniq_fd[0]);
 
 	block_sigwinch(FALSE);
@@ -2478,6 +2481,10 @@ void do_int_speller(const char *tempfile_name)
 	terminal_init();
 	doupdate();
 
+	/* When reading from the pipe went wrong, skip the spell fixes. */
+	if (bytesread < 0)
+		goto finale;
+
 	/* Save the settings of the global flags. */
 	memcpy(stash, flags, sizeof(flags));
 
@@ -2514,6 +2521,7 @@ void do_int_speller(const char *tempfile_name)
 	/* Restore the settings of the global flags. */
 	memcpy(flags, stash, sizeof(flags));
 
+  finale:
 	/* Process the end of the three processes. */
 	waitpid(pid_spell, &spell_status, 0);
 	waitpid(pid_sort, &sort_status, 0);
@@ -2525,6 +2533,8 @@ void do_int_speller(const char *tempfile_name)
 		statusline(ALERT, _("Error invoking \"sort\""));
 	else if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status))
 		statusline(ALERT, _("Error invoking \"spell\""));
+	else if (bytesread < 0)
+		statusline(ALERT, _("Error reading pipe: %s"), strerror(errornumber));
 	else
 		statusline(REMARK, _("Finished checking spelling"));
 #endif
-- 
2.54.0