In pyrf_evsel__read, if PyArg_ParseTuple fails, the allocated count_values is leaked. Move the allocation of count_values after the PyArg_ParseTuple call to prevent the memory leak.
Showing the changes to tools/perf/util/python.c (1 of 1 file). Show all
Unified · Split
| @@ -2211,11 +2211,6 @@ static PyObject *pyrf_evsel__read(struct pyrf_evsel *pevsel, · 2210 unmodified lines | ||
| 2211 | 2211 | |
| 2212 | 2212 | CHECK_INITIALIZED(evsel, "evsel"); |
| 2213 | 2213 | |
| 2214 | − count_values = PyObject_New(struct pyrf_counts_values, | |
| 2215 | − &pyrf_counts_values__type); | |
| 2216 | − if (!count_values) | |
| 2217 | − return NULL; | |
| 2218 | − | |
| 2219 | 2214 | if (!PyArg_ParseTuple(args, "ii", &cpu, &thread)) |
| 2220 | 2215 | return NULL; |
| 2221 | 2216 | |
| @@ -2234,6 +2229,10 @@ static PyObject *pyrf_evsel__read(struct pyrf_evsel *pevsel, · 12 unmodified lines | ||
| 2234 | 2229 | if (evsel__ensure_counts(evsel)) |
| 2235 | 2230 | return PyErr_NoMemory(); |
| 2236 | 2231 | |
| 2232 | + count_values = PyObject_New(struct pyrf_counts_values, &pyrf_counts_values__type); | |
| 2233 | + if (!count_values) | |
| 2234 | + return NULL; | |
| 2235 | + | |
| 2237 | 2236 | /* Set up pointers to the old and newly read counter values. */ |
| 2238 | 2237 | old_count = perf_counts(evsel->prev_raw_counts, cpu_idx, thread_idx); |
| 2239 | 2238 | new_count = perf_counts(evsel->counts, cpu_idx, thread_idx); |