diff --git a/proc/readproc.c b/proc/readproc.c index bda8d86b..a38850ac 100644 --- a/proc/readproc.c +++ b/proc/readproc.c @@ -658,7 +658,7 @@ static void statm2proc(const char* s, proc_t *restrict P) { static int file2str(const char *directory, const char *what, struct utlbuf_s *ub) { #define buffGRW 1024 char path[PROCPATHLEN]; - int fd, num, tot_read = 0; + int fd, num, tot_read = 0, len; /* on first use we preallocate a buffer of minimum size to emulate former 'local static' behavior -- even if this read fails, that @@ -666,11 +666,16 @@ static int file2str(const char *directory, const char *what, struct utlbuf_s *ub ( besides, with this xcalloc we will never need to use memcpy ) */ if (ub->buf) ub->buf[0] = '\0'; else ub->buf = xcalloc((ub->siz = buffGRW)); - sprintf(path, "%s/%s", directory, what); + len = snprintf(path, sizeof path, "%s/%s", directory, what); + if (len <= 0 || (size_t)len >= sizeof path) return -1; if (-1 == (fd = open(path, O_RDONLY, 0))) return -1; while (0 < (num = read(fd, ub->buf + tot_read, ub->siz - tot_read))) { tot_read += num; if (tot_read < ub->siz) break; + if (ub->siz >= INT_MAX - buffGRW) { + tot_read--; + break; + } ub->buf = xrealloc(ub->buf, (ub->siz += buffGRW)); }; ub->buf[tot_read] = '\0';