locking: check two magic bytes, to verify that it is a lock file

Also, when the check fails, then nano should continue and simply
open the file, just like Vim.

This fixes https://savannah.gnu.org/bugs/?57698.
This commit is contained in:
Benno Schulenberg 2020-01-30 17:29:30 +01:00
parent faa96ead44
commit b856fc4664

View File

@ -333,13 +333,20 @@ int do_lockfile(const char *filename, bool ask_the_user)
close(lockfd);
if (readtot < 48) {
if (readtot < 1024) {
statusline(MILD, _("Error reading lock file %s: "
"Not enough data read"), lockfilename);
free(lockbuf);
goto free_the_name;
}
if (lockbuf[0] != 0x62 || lockbuf[1] != 0x30) {
statusline(ALERT, _("Bad lock file is ignored: %s"), lockfilename);
free(lockbuf);
retval = 0;
goto free_the_name;
}
strncpy(lockprog, &lockbuf[2], 10);
lockpid = (((unsigned char)lockbuf[27] * 256 + (unsigned char)lockbuf[26]) * 256 +
(unsigned char)lockbuf[25]) * 256 + (unsigned char)lockbuf[24];