Skip to content

Commit

Permalink
parse_meminfo()/get_comm(): Error on short reads. Fixes rfjakob#189
Browse files Browse the repository at this point in the history
  • Loading branch information
nh2 committed Apr 27, 2020
1 parent 8d545b0 commit 93693ea
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions meminfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ meminfo_t parse_meminfo()
rewind(fd);

size_t len = fread(buf, 1, sizeof(buf) - 1, fd);
if (ferror(fd)) {
fatal(103, "could not read /proc/meminfo: %s\n", strerror(errno));
}
if (len == 0) {
fatal(103, "could not read /proc/meminfo: 0 bytes returned\n");
}
Expand Down Expand Up @@ -200,6 +203,12 @@ int get_comm(int pid, char* out, size_t outlen)
return -errno;
}
size_t n = fread(out, 1, outlen - 1, f);
if (ferror(f)) {
int fread_errno = errno;
perror("get_comm: fread() failed");
fclose(f);
return -fread_errno;
}
fclose(f);
// Process name may be empty, but we should get at least a newline
// Example for empty process name: perl -MPOSIX -e '$0=""; pause'
Expand Down

0 comments on commit 93693ea

Please sign in to comment.