locking: do not open an empty buffer when respecting the first lock file

When opening multiple files, and the first of them has a lock file,
and the user chooses to not open the corresponding file, then nano
should NOT create an empty buffer in its stead.

This fixes https://savannah.gnu.org/bugs/?57777.

Bug existed since lock files were introduced, in version 2.4.0,
specifically since commit 6948d2e7.
This commit is contained in:
Benno Schulenberg 2020-02-09 17:35:00 +01:00
parent fa26889d03
commit b63c90bf6b

View File

@ -373,6 +373,9 @@ bool open_buffer(const char *filename, bool new_buffer)
{ {
char *realname; char *realname;
/* The filename after tilde expansion. */ /* The filename after tilde expansion. */
#ifndef NANO_TINY
char *thelocksname = NULL;
#endif
struct stat fileinfo; struct stat fileinfo;
FILE *f; FILE *f;
int rc; int rc;
@ -415,28 +418,24 @@ bool open_buffer(const char *filename, bool new_buffer)
/* If we're going to load into a new buffer, first create the new /* If we're going to load into a new buffer, first create the new
* buffer and (if possible) lock the corresponding file. */ * buffer and (if possible) lock the corresponding file. */
if (new_buffer) { if (new_buffer) {
make_new_buffer();
if (has_valid_path(realname)) { if (has_valid_path(realname)) {
#ifndef NANO_TINY #ifndef NANO_TINY
if (ISSET(LOCKING) && !ISSET(VIEW_MODE) && filename[0] != '\0') { if (ISSET(LOCKING) && !ISSET(VIEW_MODE) && filename[0] != '\0') {
char *thelocksname = do_lockfile(realname, TRUE); thelocksname = do_lockfile(realname, TRUE);
/* When not overriding an existing lock, discard the buffer. */ /* When not overriding an existing lock, don't open a buffer. */
if (thelocksname == SKIPTHISFILE) { if (thelocksname == SKIPTHISFILE) {
#ifdef ENABLE_MULTIBUFFER
if (openfile != openfile->next)
close_buffer();
#endif
free(realname); free(realname);
return FALSE; return FALSE;
} else }
openfile->lock_filename = thelocksname;
} }
#endif /* !NANO_TINY */ #endif /* !NANO_TINY */
} }
} }
if (new_buffer)
make_new_buffer();
/* If the filename isn't blank, and we are not in NOREAD_MODE, /* If the filename isn't blank, and we are not in NOREAD_MODE,
* open the file. Otherwise, treat it as a new file. */ * open the file. Otherwise, treat it as a new file. */
rc = (filename[0] != '\0' && !ISSET(NOREAD_MODE)) ? rc = (filename[0] != '\0' && !ISSET(NOREAD_MODE)) ?
@ -461,6 +460,9 @@ bool open_buffer(const char *filename, bool new_buffer)
* the filename and put the cursor at the start of the buffer. */ * the filename and put the cursor at the start of the buffer. */
if (rc != -1 && new_buffer) { if (rc != -1 && new_buffer) {
openfile->filename = mallocstrcpy(openfile->filename, realname); openfile->filename = mallocstrcpy(openfile->filename, realname);
#ifndef NANO_TINY
openfile->lock_filename = thelocksname;
#endif
openfile->current = openfile->filetop; openfile->current = openfile->filetop;
openfile->current_x = 0; openfile->current_x = 0;
openfile->placewewant = 0; openfile->placewewant = 0;