提交 353dd147 编写于 作者: J Jim Meyering

openvzGetProcessInfo: address clang-detected low-probability flaw

* src/openvz/openvz_driver.c (openvzGetProcessInfo): Reorganize
so that unexpected /proc/vz/vestat content cannot make us use
uninitialized variables.  Without this change, an input line with
a matching "readvps", but fewer than 4 numbers would result in our
using at least "systime" uninitialized.
上级 cd7ee20a
...@@ -1384,14 +1384,15 @@ static int openvzGetProcessInfo(unsigned long long *cpuTime, int vpsid) { ...@@ -1384,14 +1384,15 @@ static int openvzGetProcessInfo(unsigned long long *cpuTime, int vpsid) {
int fd; int fd;
char line[1024] ; char line[1024] ;
unsigned long long usertime, systime, nicetime; unsigned long long usertime, systime, nicetime;
int readvps = 0, ret; int readvps = vpsid + 1; /* ensure readvps is initially different */
int ret;
/* read statistic from /proc/vz/vestat. /* read statistic from /proc/vz/vestat.
sample: sample:
Version: 2.2 Version: 2.2
VEID user nice system uptime idle other.. VEID user nice system uptime idle other..
33 78 0 1330 59454597 142650441835148 other.. 33 78 0 1330 59454597 142650441835148 other..
55 178 0 5340 59424597 542650441835148 other.. 55 178 0 5340 59424597 542650441835148 other..
*/ */
if ((fd = open("/proc/vz/vestat", O_RDONLY)) == -1) if ((fd = open("/proc/vz/vestat", O_RDONLY)) == -1)
...@@ -1400,15 +1401,18 @@ Version: 2.2 ...@@ -1400,15 +1401,18 @@ Version: 2.2
/*search line with VEID=vpsid*/ /*search line with VEID=vpsid*/
while(1) { while(1) {
ret = openvz_readline(fd, line, sizeof(line)); ret = openvz_readline(fd, line, sizeof(line));
if(ret <= 0) if (ret <= 0)
break; break;
if (sscanf(line, "%d %llu %llu %llu", if (sscanf (line, "%d %llu %llu %llu",
&readvps, &usertime, &nicetime, &systime) != 4) &readvps, &usertime, &nicetime, &systime) == 4
continue; && readvps == vpsid) { /*found vpsid*/
/* convert jiffies to nanoseconds */
if (readvps == vpsid) *cpuTime = (1000ull * 1000ull * 1000ull
break; /*found vpsid*/ * (usertime + nicetime + systime)
/ (unsigned long long)sysconf(_SC_CLK_TCK));
break;
}
} }
close(fd); close(fd);
...@@ -1418,10 +1422,6 @@ Version: 2.2 ...@@ -1418,10 +1422,6 @@ Version: 2.2
if (readvps != vpsid) /*not found*/ if (readvps != vpsid) /*not found*/
return -1; return -1;
/* convert jiffies to nanoseconds */
*cpuTime = 1000ull * 1000ull * 1000ull * (usertime + nicetime + systime)
/ (unsigned long long)sysconf(_SC_CLK_TCK);
return 0; return 0;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册