Commit 2faad123 authored by Benno Schulenberg's avatar Benno Schulenberg
Browse files

locking: don't try to read more bytes than the buffer can hold

A normal lock file is apparently 1024 bytes in size, so the second
attempt at reading bytes from the file would try to read 8192 more
bytes into a buffer that has room for only 7168 left.  According to
valgrind, the read() function doesn't like that -- and true: if for
some reason the lock file had suddenly expanded, the buffer would
overflow.

This fixes https://savannah.gnu.org/bugs/?47156.
No related merge requests found
Showing with 5 additions and 3 deletions
+5 -3
...@@ -32,6 +32,8 @@ ...@@ -32,6 +32,8 @@
#include <pwd.h> #include <pwd.h>
#include <libgen.h> #include <libgen.h>
#define LOCKBUFSIZE 8192
/* Verify that the containing directory of the given filename exists. */ /* Verify that the containing directory of the given filename exists. */
bool has_valid_path(const char *filename) bool has_valid_path(const char *filename)
{ {
...@@ -337,11 +339,11 @@ int do_lockfile(const char *filename) ...@@ -337,11 +339,11 @@ int do_lockfile(const char *filename)
goto free_the_name; goto free_the_name;
} }
lockbuf = charalloc(8192); lockbuf = charalloc(LOCKBUFSIZE);
do { do {
readamt = read(lockfd, &lockbuf[readtot], BUFSIZ); readamt = read(lockfd, &lockbuf[readtot], LOCKBUFSIZE - readtot);
readtot += readamt; readtot += readamt;
} while (readtot < 8192 && readamt > 0); } while (readamt > 0 && readtot < LOCKBUFSIZE);
if (readtot < 48) { if (readtot < 48) {
statusbar(_("Error reading lock file %s: Not enough data read"), statusbar(_("Error reading lock file %s: Not enough data read"),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment