[PATCH v1 05/11] perf trace: Smatch: Fix potential NULL pointer dereference

From: Leo Yan
Date: Tue Jul 02 2019 - 06:36:07 EST


Based on the following report from Smatch, fix the potential
NULL pointer dereference check.

tools/perf/builtin-trace.c:1044
thread_trace__new() error: we previously assumed 'ttrace' could be
null (see line 1041).

tools/perf/builtin-trace.c
1037 static struct thread_trace *thread_trace__new(void)
1038 {
1039 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
1040
1041 if (ttrace)
1042 ttrace->files.max = -1;
1043
1044 ttrace->syscall_stats = intlist__new(NULL);
^^^^^^^^
1045
1046 return ttrace;
1047 }

This patch directly returns NULL when fail to allocate memory for
ttrace; this can avoid potential NULL pointer dereference.

Signed-off-by: Leo Yan <leo.yan@xxxxxxxxxx>
---
tools/perf/builtin-trace.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index f3532b081b31..874d78890c60 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -1038,9 +1038,10 @@ static struct thread_trace *thread_trace__new(void)
{
struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));

- if (ttrace)
- ttrace->files.max = -1;
+ if (ttrace == NULL)
+ return NULL;

+ ttrace->files.max = -1;
ttrace->syscall_stats = intlist__new(NULL);

return ttrace;
--
2.17.1