Fix get_from_pipe potentially reading more bytes than its buffer size

This commit is contained in:
ToMe25
2023-08-04 11:09:12 +02:00
committed by Robert Roth
parent 306f5d0c8f
commit 079b9e17c5
5 changed files with 8 additions and 9 deletions

View File

@@ -235,7 +235,7 @@ glibtop_get_disk_s (glibtop *server, glibtop_disk *buf)
file_to_buffer (server, buffer, sizeof buffer, FILENAME);
get_from_pipe (map_buffer, CMD_PIPE);
get_from_pipe (map_buffer, STAT_BUFSIZ, CMD_PIPE);
/*
* GLOBAL

View File

@@ -58,17 +58,15 @@ skip_token (const char *p)
void
get_from_pipe (char *buffer, const char *cmd)
get_from_pipe (char *buffer, const size_t bufsiz, const char *cmd)
{
FILE* fp;
long psize;
fp = popen (cmd, "r");
fseek (fp, 0, SEEK_END);
psize = ftell (fp);
fseek (fp, 0, SEEK_SET);
fread(buffer,1,psize,fp);
size_t psize = fread(buffer,1,bufsiz,fp);
if (psize == bufsiz)
g_warning("Read bufsiz bytes, there may be more");
pclose (fp);
}

View File

@@ -62,7 +62,7 @@ skip_line (const char *p)
}
void
get_from_pipe (char *buffer, const char *cmd);
get_from_pipe (char *buffer, const size_t bufsiz, const char *cmd);
/*
* Smart strtoul which handles binary suffixes