Skip to content

Commit 5d00b52

Browse files
committed
read files up to stat.st_size
prior to this commit, reading non-regular files like `/dev/zero` or `/dev/urandom` for example would read in a forever loop, filling up the mmap tmp file. other character device files like `/dev/rfkill` would hang on read. after this commit, we only read up to `stat.st_size`, which for these files will mostly be 0.
1 parent bd24d2f commit 5d00b52

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

buffer.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,7 @@ static int _buffer_open_mmap(buffer_t *self, int fd, size_t size) {
11981198
char readbuf[1024];
11991199
ssize_t nread;
12001200
char *mmap_buf;
1201+
size_t nread_total = 0;
12011202

12021203
// Copy fd to tmp file
12031204
sprintf(tmppath, "%s", "/tmp/mle-XXXXXX");
@@ -1207,7 +1208,7 @@ static int _buffer_open_mmap(buffer_t *self, int fd, size_t size) {
12071208
return MLBUF_ERR;
12081209
}
12091210
unlink(tmppath);
1210-
while (1) {
1211+
while (nread_total < size) {
12111212
nread = read(fd, &readbuf, 1024);
12121213
if (nread == 0) {
12131214
break;
@@ -1221,6 +1222,7 @@ static int _buffer_open_mmap(buffer_t *self, int fd, size_t size) {
12211222
close(tmpfd);
12221223
return MLBUF_ERR;
12231224
}
1225+
nread_total += nread;
12241226
}
12251227

12261228
// Now mmap tmp file
@@ -1242,7 +1244,9 @@ static int _buffer_open_mmap(buffer_t *self, int fd, size_t size) {
12421244
static int _buffer_open_read(buffer_t *self, int fd, size_t size) {
12431245
int rc;
12441246
char *buf;
1245-
if (size <= PTRDIFF_MAX) {
1247+
if (size <= 0) {
1248+
return buffer_set(self, "", 0);
1249+
} else if (size <= PTRDIFF_MAX) {
12461250
buf = malloc(size);
12471251
} else {
12481252
return MLBUF_ERR;

0 commit comments

Comments
 (0)