Re: [PATCH V1]Perf: Return error code for perf_session__new function on failure

From: Mamatha Inamdar
Date: Thu Aug 22 2019 - 09:44:20 EST



On 21/08/19 12:35 PM, Jiri Olsa wrote:
On Tue, Aug 20, 2019 at 04:51:21PM +0530, Mamatha Inamdar wrote:

SNIP

#ifdef HAVE_ZSTD_SUPPORT
static int perf_session__process_compressed_event(struct perf_session *session,
@@ -183,6 +184,7 @@ static int ordered_events__deliver_event(struct ordered_events *oe,
struct perf_session *perf_session__new(struct perf_data *data,
bool repipe, struct perf_tool *tool)
{
+ int ret = -ENOMEM;
struct perf_session *session = zalloc(sizeof(*session));
if (!session)
@@ -197,13 +199,15 @@ struct perf_session *perf_session__new(struct perf_data *data,
perf_env__init(&session->header.env);
if (data) {
- if (perf_data__open(data))
+ ret = perf_data__open(data);
+ if (ret < 0)
goto out_delete;
session->data = data;
if (perf_data__is_read(data)) {
- if (perf_session__open(session) < 0)
+ ret = perf_session__open(session);
+ if (ret < 0)
goto out_delete;
/*
@@ -218,7 +222,8 @@ struct perf_session *perf_session__new(struct perf_data *data,
perf_evlist__init_trace_event_sample_raw(session->evlist);
/* Open the directory data. */
- if (data->is_dir && perf_data__open_dir(data))
+ ret = data->is_dir && perf_data__open_dir(data);
+ if (ret)
goto out_delete;
will this return 1 in case of error?

jirka

ok, I think your point is correct,

"ret" contains value of conditional statement(&&), which is always either 0 or 1

will do following changes and send new version of patch

if (data->is_dir) {
ÂÂÂÂÂÂ ret = perf_data__open_dir(data);
ÂÂÂÂÂÂ if (ret)
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto out_delete;
}

}
} else {
@@ -252,7 +257,7 @@ struct perf_session *perf_session__new(struct perf_data *data,
out_delete:
perf_session__delete(session);
out:
- return NULL;
+ return ERR_PTR(ret);
}
static void perf_session__delete_threads(struct perf_session *session)