From: Donald Yandt Date: Tue, 14 May 2019 11:01:00 +0000 (-0400) Subject: perf machine: Null-terminate version char array upon fgets(/proc/version) error X-Git-Tag: v5.2-rc1~7^2^2~53 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=30ba5b0e66c872faa416a458d32cc3956ecb548a;p=linux-2.6-block.git perf machine: Null-terminate version char array upon fgets(/proc/version) error If fgets() fails due to any other error besides end-of-file, the version char array may not even be null-terminated. Signed-off-by: Donald Yandt Cc: Alexander Shishkin Cc: Avi Kivity Cc: Jiri Olsa Cc: Peter Zijlstra Cc: Yanmin Zhang Fixes: a1645ce12adb ("perf: 'perf kvm' tool for monitoring guest performance from host") Link: http://lkml.kernel.org/r/20190514110100.22019-1-donald.yandt@gmail.com Signed-off-by: Arnaldo Carvalho de Melo --- diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 3c520baa198c..28a9541c4835 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1234,8 +1234,9 @@ static char *get_kernel_version(const char *root_dir) if (!file) return NULL; - version[0] = '\0'; tmp = fgets(version, sizeof(version), file); + if (!tmp) + *version = '\0'; fclose(file); name = strstr(version, prefix);