perf machine: Null-terminate version char array upon fgets(/proc/version) error
authorDonald Yandt <donald.yandt@gmail.com>
Tue, 14 May 2019 11:01:00 +0000 (07:01 -0400)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 15 May 2019 19:36:47 +0000 (16:36 -0300)
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 <donald.yandt@gmail.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Avi Kivity <avi@scylladb.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Yanmin Zhang <yanmin_zhang@linux.intel.com>
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 <acme@redhat.com>
tools/perf/util/machine.c

index 3c520baa198cfcc49276ca77a8f8c12d058bec4d..28a9541c48359cad6a52ea4e8609cfba3e2be023 100644 (file)
@@ -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);