From b6a1583e8ebbdf65c6b80e773736c994e1639aa1 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Sun, 2 Feb 2020 11:36:01 +0100 Subject: [PATCH] tweaks: take just one shot at reading the lock file, and correct a type Don't bother looping until the 1024 bytes are read. Writing the lock file takes just one shot too, and that is more important to get right. Also, correct the type for the result of read(), so that -1 doesn't get turned into a positive number, which would mean that any error would get ignored. --- src/files.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/files.c b/src/files.c index 9711ab49..9a81bb60 100644 --- a/src/files.c +++ b/src/files.c @@ -293,7 +293,7 @@ int do_lockfile(const char *filename, bool ask_the_user) char *lockbuf, *question, *pidstring, *postedname, *promptstr; static char lockprog[11], lockuser[17]; int lockfd, lockpid, room, choice; - size_t readamt = 0, readtot = 0; + ssize_t readamt; if ((lockfd = open(lockfilename, O_RDONLY)) < 0) { statusline(ALERT, _("Error opening lock file %s: %s"), @@ -303,14 +303,11 @@ int do_lockfile(const char *filename, bool ask_the_user) lockbuf = charalloc(LOCKSIZE); - do { - readamt = read(lockfd, &lockbuf[readtot], LOCKSIZE - readtot); - readtot += readamt; - } while (readamt > 0 && readtot < LOCKSIZE); + readamt = read(lockfd, lockbuf, LOCKSIZE); close(lockfd); - if (readtot < LOCKSIZE || lockbuf[0] != 0x62 || lockbuf[1] != 0x30) { + if (readamt < LOCKSIZE || lockbuf[0] != 0x62 || lockbuf[1] != 0x30) { statusline(ALERT, _("Bad lock file is ignored: %s"), lockfilename); free(lockbuf); goto free_the_name;