From 2557104494c9592c86b545cdf6de8e40f5702da8 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sat, 5 Jul 2014 12:51:13 +0000 Subject: [PATCH] util/proc: theoretical underflow --- util/proc.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/util/proc.c b/util/proc.c index 84c2a94..d041d5d 100644 --- a/util/proc.c +++ b/util/proc.c @@ -43,6 +43,7 @@ parsestat(pid_t pid, struct procstat *ps) { char path[PATH_MAX]; FILE *fp; + size_t len; snprintf(path, sizeof(path), "/proc/%d/stat", pid); if (!(fp = fopen(path, "r"))) @@ -58,8 +59,10 @@ parsestat(pid_t pid, struct procstat *ps) &ps->num_threads, &ps->itrealvalue, &ps->starttime, &ps->vsize, &ps->rss, &ps->rsslim); /* Filter out '(' and ')' from comm */ - ps->comm[strlen(ps->comm) - 1] = '\0'; - memmove(ps->comm, ps->comm + 1, strlen(ps->comm)); + if((len = strlen(ps->comm)) > 0) + len--; + ps->comm[len] = '\0'; + memmove(ps->comm, ps->comm + 1, len); fclose(fp); return 0; }