Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf data: Fix error return code in perf_data__create_dir()

Although 'ret' has been initialized to -1, but it will be reassigned by
the "ret = open(...)" statement in the for loop. So that, the value of
'ret' is unknown when asprintf() failed.

Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/20210415083417.3740-1-thunder.leizhen@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Zhen Lei and committed by
Arnaldo Carvalho de Melo
59a1a843 b96da02b

+3 -2
+3 -2
tools/perf/util/data.c
··· 35 35 int perf_data__create_dir(struct perf_data *data, int nr) 36 36 { 37 37 struct perf_data_file *files = NULL; 38 - int i, ret = -1; 38 + int i, ret; 39 39 40 40 if (WARN_ON(!data->is_dir)) 41 41 return -EINVAL; ··· 51 51 for (i = 0; i < nr; i++) { 52 52 struct perf_data_file *file = &files[i]; 53 53 54 - if (asprintf(&file->path, "%s/data.%d", data->path, i) < 0) 54 + ret = asprintf(&file->path, "%s/data.%d", data->path, i); 55 + if (ret < 0) 55 56 goto out_err; 56 57 57 58 ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);