From 9a475bf2d377c253a58b35db2966aac03fb953fc Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 24 May 2019 17:22:04 +0200 Subject: [PATCH] speller: don't crash when the spell-checked tempfile cannot be opened This addresses https://savannah.gnu.org/bugs/?56361. --- src/files.c | 6 ++++-- src/proto.h | 2 +- src/text.c | 8 +++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/files.c b/src/files.c index 361991f4..71ece42c 100644 --- a/src/files.c +++ b/src/files.c @@ -498,7 +498,7 @@ bool open_buffer(const char *filename, bool new_buffer) #ifdef ENABLE_SPELLER /* Open the specified file, and if that succeeds, remove the text of the marked * region or of the entire buffer and read the file contents into its place. */ -void replace_buffer(const char *filename, undo_type action, bool marked) +bool replace_buffer(const char *filename, undo_type action, bool marked) { linestruct *was_cutbuffer = cutbuffer; int descriptor; @@ -507,7 +507,7 @@ void replace_buffer(const char *filename, undo_type action, bool marked) descriptor = open_file(filename, FALSE, &f); if (descriptor < 0) - return; + return FALSE; #ifndef NANO_TINY add_undo(COUPLE_BEGIN); @@ -539,6 +539,7 @@ void replace_buffer(const char *filename, undo_type action, bool marked) add_undo(COUPLE_END); openfile->undotop->strdata = mallocstrcpy(NULL, _("spelling correction")); #endif + return TRUE; } #endif /* ENABLE_SPELLER */ @@ -959,6 +960,7 @@ int open_file(const char *filename, bool newfie, FILE **f) if (*f == NULL) { statusline(ALERT, _("Error reading %s: %s"), filename, strerror(errno)); close(fd); + fd = -1; } else if (!inhelp) statusbar(_("Reading...")); } diff --git a/src/proto.h b/src/proto.h index 7e60a036..d8a622f8 100644 --- a/src/proto.h +++ b/src/proto.h @@ -267,7 +267,7 @@ void make_new_buffer(void); void set_modified(void); bool open_buffer(const char *filename, bool new_buffer); #ifdef ENABLE_SPELLER -void replace_buffer(const char *filename, undo_type action, bool marked); +bool replace_buffer(const char *filename, undo_type action, bool marked); #endif void prepare_for_display(void); #ifdef ENABLE_MULTIBUFFER diff --git a/src/text.c b/src/text.c index 077cbc27..a4cac7c0 100644 --- a/src/text.c +++ b/src/text.c @@ -2531,6 +2531,7 @@ const char *do_alt_speller(char *tempfile_name) size_t pww_save = openfile->placewewant; ssize_t lineno_save = openfile->current->lineno; bool was_at_eol = (openfile->current->data[openfile->current_x] == '\0'); + bool replaced = FALSE; struct stat spellfileinfo; time_t timestamp; pid_t pid_spell; @@ -2586,7 +2587,7 @@ const char *do_alt_speller(char *tempfile_name) openfile->mark_x < openfile->current_x)); ssize_t was_mark_lineno = openfile->mark->lineno; - replace_buffer(tempfile_name, CUT, TRUE); + replaced = replace_buffer(tempfile_name, CUT, TRUE); /* Adjust the end point of the marked region for any change in * length of the region's last line. */ @@ -2599,14 +2600,15 @@ const char *do_alt_speller(char *tempfile_name) openfile->mark = fsfromline(was_mark_lineno); } else #endif - replace_buffer(tempfile_name, CUT_TO_EOF, FALSE); + replaced = replace_buffer(tempfile_name, CUT_TO_EOF, FALSE); /* Go back to the old position. */ goto_line_posx(lineno_save, current_x_save); if (was_at_eol || openfile->current_x > strlen(openfile->current->data)) openfile->current_x = strlen(openfile->current->data); #ifndef NANO_TINY - update_undo(COUPLE_END); + if (replaced) + update_undo(COUPLE_END); #endif openfile->placewewant = pww_save; adjust_viewport(STATIONARY);