linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf script python: Fail check on dynamic allocation.
@ 2023-11-12  7:14 Paran Lee
  2023-11-13  7:44 ` Adrian Hunter
  0 siblings, 1 reply; 3+ messages in thread
From: Paran Lee @ 2023-11-12  7:14 UTC (permalink / raw)
  To: Namhyung Kim, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Ian Rogers, Adrian Hunter, Sean Christopherson,
	Li Dong
  Cc: linux-perf-users, linux-kernel, shjy180909, austindh.kim,
	honggyu.kp, Paran Lee

There is Ommitted PyList_New() Fail check
and set_regs_in_dict(), python_start_script()
dynamic allocation checking.

Signed-off-by: Paran Lee <p4ranlee@gmail.com>
---
 .../scripting-engines/trace-event-python.c     | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index 94312741443a..0847c50d4601 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -353,6 +353,8 @@ static PyObject *get_field_numeric_entry(struct tep_event *event,
 
 	if (is_array) {
 		list = PyList_New(field->arraylen);
+		if (!list)
+			Py_FatalError("couldn't create Python list");
 		item_size = field->size / field->arraylen;
 		n_items = field->arraylen;
 	} else {
@@ -754,7 +756,7 @@ static void regs_map(struct regs_dump *regs, uint64_t mask, const char *arch, ch
 	}
 }
 
-static void set_regs_in_dict(PyObject *dict,
+static int set_regs_in_dict(PyObject *dict,
 			     struct perf_sample *sample,
 			     struct evsel *evsel)
 {
@@ -770,6 +772,9 @@ static void set_regs_in_dict(PyObject *dict,
 	 */
 	int size = __sw_hweight64(attr->sample_regs_intr) * 28;
 	char *bf = malloc(size);
+	if (!bf)
+		return -1;
+	}
 
 	regs_map(&sample->intr_regs, attr->sample_regs_intr, arch, bf, size);
 
@@ -781,6 +786,8 @@ static void set_regs_in_dict(PyObject *dict,
 	pydict_set_item_string_decref(dict, "uregs",
 			_PyUnicode_FromString(bf));
 	free(bf);
+
+	return 0;
 }
 
 static void set_sym_in_dict(PyObject *dict, struct addr_location *al,
@@ -920,7 +927,8 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
 			PyLong_FromUnsignedLongLong(sample->cyc_cnt));
 	}
 
-	set_regs_in_dict(dict, sample, evsel);
+	if (!set_regs_in_dict(dict, sample, evsel))
+		Py_FatalError("Failed to setting regs in dict");
 
 	return dict;
 }
@@ -1918,12 +1926,18 @@ static int python_start_script(const char *script, int argc, const char **argv,
 	scripting_context->session = session;
 #if PY_MAJOR_VERSION < 3
 	command_line = malloc((argc + 1) * sizeof(const char *));
+	if(!command_line) {
+		return -1;
+	}
 	command_line[0] = script;
 	for (i = 1; i < argc + 1; i++)
 		command_line[i] = argv[i - 1];
 	PyImport_AppendInittab(name, initperf_trace_context);
 #else
 	command_line = malloc((argc + 1) * sizeof(wchar_t *));
+	if(!command_line) {
+		return -1;
+	}
 	command_line[0] = Py_DecodeLocale(script, NULL);
 	for (i = 1; i < argc + 1; i++)
 		command_line[i] = Py_DecodeLocale(argv[i - 1], NULL);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] perf script python: Fail check on dynamic allocation.
  2023-11-12  7:14 [PATCH] perf script python: Fail check on dynamic allocation Paran Lee
@ 2023-11-13  7:44 ` Adrian Hunter
  2023-11-14 13:36   ` Paran Lee
  0 siblings, 1 reply; 3+ messages in thread
From: Adrian Hunter @ 2023-11-13  7:44 UTC (permalink / raw)
  To: Paran Lee, Namhyung Kim, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Ian Rogers, Sean Christopherson, Li Dong
  Cc: linux-perf-users, linux-kernel, shjy180909, austindh.kim,
	honggyu.kp

On 12/11/23 09:14, Paran Lee wrote:
> There is Ommitted PyList_New() Fail check
> and set_regs_in_dict(), python_start_script()
> dynamic allocation checking.
> 
> Signed-off-by: Paran Lee <p4ranlee@gmail.com>

Please ensure patches compile before submitting.  Also run checkpatch
and fix any errors, and consider fixing warnings and checks.  Have a look
at checkpatch --strict also.

> ---
>  .../scripting-engines/trace-event-python.c     | 18 ++++++++++++++++--
>  1 file changed, 16 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
> index 94312741443a..0847c50d4601 100644
> --- a/tools/perf/util/scripting-engines/trace-event-python.c
> +++ b/tools/perf/util/scripting-engines/trace-event-python.c
> @@ -353,6 +353,8 @@ static PyObject *get_field_numeric_entry(struct tep_event *event,
>  
>  	if (is_array) {
>  		list = PyList_New(field->arraylen);
> +		if (!list)
> +			Py_FatalError("couldn't create Python list");
>  		item_size = field->size / field->arraylen;
>  		n_items = field->arraylen;
>  	} else {
> @@ -754,7 +756,7 @@ static void regs_map(struct regs_dump *regs, uint64_t mask, const char *arch, ch
>  	}
>  }
>  
> -static void set_regs_in_dict(PyObject *dict,
> +static int set_regs_in_dict(PyObject *dict,
>  			     struct perf_sample *sample,
>  			     struct evsel *evsel)
>  {
> @@ -770,6 +772,9 @@ static void set_regs_in_dict(PyObject *dict,
>  	 */
>  	int size = __sw_hweight64(attr->sample_regs_intr) * 28;
>  	char *bf = malloc(size);
> +	if (!bf)
> +		return -1;
> +	}
>  
>  	regs_map(&sample->intr_regs, attr->sample_regs_intr, arch, bf, size);
>  
> @@ -781,6 +786,8 @@ static void set_regs_in_dict(PyObject *dict,
>  	pydict_set_item_string_decref(dict, "uregs",
>  			_PyUnicode_FromString(bf));
>  	free(bf);
> +
> +	return 0;
>  }
>  
>  static void set_sym_in_dict(PyObject *dict, struct addr_location *al,
> @@ -920,7 +927,8 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
>  			PyLong_FromUnsignedLongLong(sample->cyc_cnt));
>  	}
>  
> -	set_regs_in_dict(dict, sample, evsel);
> +	if (!set_regs_in_dict(dict, sample, evsel))
> +		Py_FatalError("Failed to setting regs in dict");
>  
>  	return dict;
>  }
> @@ -1918,12 +1926,18 @@ static int python_start_script(const char *script, int argc, const char **argv,
>  	scripting_context->session = session;
>  #if PY_MAJOR_VERSION < 3
>  	command_line = malloc((argc + 1) * sizeof(const char *));
> +	if(!command_line) {
> +		return -1;
> +	}
>  	command_line[0] = script;
>  	for (i = 1; i < argc + 1; i++)
>  		command_line[i] = argv[i - 1];
>  	PyImport_AppendInittab(name, initperf_trace_context);
>  #else
>  	command_line = malloc((argc + 1) * sizeof(wchar_t *));
> +	if(!command_line) {
> +		return -1;
> +	}
>  	command_line[0] = Py_DecodeLocale(script, NULL);
>  	for (i = 1; i < argc + 1; i++)
>  		command_line[i] = Py_DecodeLocale(argv[i - 1], NULL);


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] perf script python: Fail check on dynamic allocation.
  2023-11-13  7:44 ` Adrian Hunter
@ 2023-11-14 13:36   ` Paran Lee
  0 siblings, 0 replies; 3+ messages in thread
From: Paran Lee @ 2023-11-14 13:36 UTC (permalink / raw)
  To: Adrian Hunter, Namhyung Kim, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Ian Rogers, Sean Christopherson, Li Dong
  Cc: linux-perf-users, linux-kernel, shjy180909, austindh.kim,
	honggyu.kp



On 2023-11-13 오후 4:44, Adrian Hunter wrote:

Thank you so much for the review Adrian!
My first patch were my inexperience came in.

> Please ensure patches compile before submitting.  Also run checkpatch
> and fix any errors, and consider fixing warnings and checks.  Have a look
> at checkpatch --strict also.

I'll check you mentioned and send the v2 patch.

BR,
Paran Lee

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-14 13:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-12  7:14 [PATCH] perf script python: Fail check on dynamic allocation Paran Lee
2023-11-13  7:44 ` Adrian Hunter
2023-11-14 13:36   ` Paran Lee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).