perf python: Validate attribute setters in pyrf_evsel

If val is NULL when setting an attribute, PyErr_SetString should be
called as deleting the attribute isn't supported. In addition, ensure
PyErr_Occurred is checked before setting the attribute to avoid setting
a garbage value.

Fixes: 877108e42b ("perf tools: Initial python binding")
Signed-off-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
This commit is contained in:
Ian Rogers 2026-08-09 00:14:48 -07:00 committed by Namhyung Kim
parent 9a142beb1e
commit 0b274050c4

View File

@ -2301,6 +2301,11 @@ static int pyrf_evsel__set_tracking(PyObject *self, PyObject *val, void *closure
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
if (val == NULL) {
PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
return -1;
}
is_true = PyObject_IsTrue(val);
if (is_true < 0)
return -1;
@ -2312,11 +2317,21 @@ static int pyrf_evsel__set_tracking(PyObject *self, PyObject *val, void *closure
static int pyrf_evsel__set_attr_config(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
pevsel->evsel->core.attr.config = PyLong_AsUnsignedLongLong(val);
return PyErr_Occurred() ? -1 : 0;
if (val == NULL) {
PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
return -1;
}
new_val = PyLong_AsUnsignedLongLong(val);
if (PyErr_Occurred())
return -1;
pevsel->evsel->core.attr.config = new_val;
return 0;
}
static PyObject *pyrf_evsel__get_attr_config(PyObject *self, void *closure __maybe_unused)
@ -2331,11 +2346,21 @@ static PyObject *pyrf_evsel__get_attr_config(PyObject *self, void *closure __may
static int pyrf_evsel__set_attr_read_format(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
pevsel->evsel->core.attr.read_format = PyLong_AsUnsignedLongLong(val);
return PyErr_Occurred() ? -1 : 0;
if (val == NULL) {
PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
return -1;
}
new_val = PyLong_AsUnsignedLongLong(val);
if (PyErr_Occurred())
return -1;
pevsel->evsel->core.attr.read_format = new_val;
return 0;
}
static PyObject *pyrf_evsel__get_attr_read_format(PyObject *self, void *closure __maybe_unused)
@ -2350,11 +2375,21 @@ static PyObject *pyrf_evsel__get_attr_read_format(PyObject *self, void *closure
static int pyrf_evsel__set_attr_sample_period(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
pevsel->evsel->core.attr.sample_period = PyLong_AsUnsignedLongLong(val);
return PyErr_Occurred() ? -1 : 0;
if (val == NULL) {
PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
return -1;
}
new_val = PyLong_AsUnsignedLongLong(val);
if (PyErr_Occurred())
return -1;
pevsel->evsel->core.attr.sample_period = new_val;
return 0;
}
static PyObject *pyrf_evsel__get_attr_sample_period(PyObject *self, void *closure __maybe_unused)
@ -2369,11 +2404,21 @@ static PyObject *pyrf_evsel__get_attr_sample_period(PyObject *self, void *closur
static int pyrf_evsel__set_attr_sample_type(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
pevsel->evsel->core.attr.sample_type = PyLong_AsUnsignedLongLong(val);
return PyErr_Occurred() ? -1 : 0;
if (val == NULL) {
PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
return -1;
}
new_val = PyLong_AsUnsignedLongLong(val);
if (PyErr_Occurred())
return -1;
pevsel->evsel->core.attr.sample_type = new_val;
return 0;
}
static PyObject *pyrf_evsel__get_attr_sample_type(PyObject *self, void *closure __maybe_unused)
@ -2397,11 +2442,21 @@ static PyObject *pyrf_evsel__get_attr_size(PyObject *self, void *closure __maybe
static int pyrf_evsel__set_attr_type(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
unsigned long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
pevsel->evsel->core.attr.type = PyLong_AsUnsignedLong(val);
return PyErr_Occurred() ? -1 : 0;
if (val == NULL) {
PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
return -1;
}
new_val = PyLong_AsUnsignedLong(val);
if (PyErr_Occurred())
return -1;
pevsel->evsel->core.attr.type = new_val;
return 0;
}
static PyObject *pyrf_evsel__get_attr_type(PyObject *self, void *closure __maybe_unused)
@ -2416,11 +2471,21 @@ static PyObject *pyrf_evsel__get_attr_type(PyObject *self, void *closure __maybe
static int pyrf_evsel__set_attr_wakeup_events(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
unsigned long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
pevsel->evsel->core.attr.wakeup_events = PyLong_AsUnsignedLong(val);
return PyErr_Occurred() ? -1 : 0;
if (val == NULL) {
PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
return -1;
}
new_val = PyLong_AsUnsignedLong(val);
if (PyErr_Occurred())
return -1;
pevsel->evsel->core.attr.wakeup_events = new_val;
return 0;
}
static PyObject *pyrf_evsel__get_attr_wakeup_events(PyObject *self, void *closure __maybe_unused)