Skip to content

Commit

Permalink
get_comm(): Handle short reads. See rfjakob#189
Browse files Browse the repository at this point in the history
  • Loading branch information
nh2 committed Apr 25, 2020
1 parent 3823fb3 commit addfffc
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions meminfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,28 @@ int get_comm(int pid, char* out, size_t outlen)
if (f == NULL) {
return -errno;
}
size_t n = fread(out, 1, outlen - 1, f);
size_t total_bytes_read = 0;
while (1) {
size_t n = fread(out + total_bytes_read, 1, outlen - 1 - total_bytes_read, f);
if (ferror(f)) {
int fread_errno = errno;
perror("get_comm: fread() failed");
fclose(f);
return -fread_errno;
}
total_bytes_read += n;
if (feof(f)) {
break;
}
}
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'
if (n < 1) {
if (total_bytes_read < 1) {
return -ENODATA;
}
// Strip trailing newline
out[n - 1] = 0;
out[total_bytes_read - 1] = 0;
fix_truncated_utf8(out);
return 0;
}
Expand Down

0 comments on commit addfffc

Please sign in to comment.