diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index 3c520baa198cfcc49276ca77a8f8c12d058bec4d..28a9541c48359cad6a52ea4e8609cfba3e2be023 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);