perf metricgroups: Use zfree() to reduce chances of use after free

Do defensive programming by using zfree() to initialize freed pointers
to NULL, so that eventual use after free result in a NULL pointer deref
instead of more subtle behaviour.

This file already used zfree() in other places, so this just plugs some
leftovers.
Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 2e384400
...@@ -90,9 +90,9 @@ static void metric_event_delete(struct rblist *rblist __maybe_unused, ...@@ -90,9 +90,9 @@ static void metric_event_delete(struct rblist *rblist __maybe_unused,
struct metric_expr *expr, *tmp; struct metric_expr *expr, *tmp;
list_for_each_entry_safe(expr, tmp, &me->head, nd) { list_for_each_entry_safe(expr, tmp, &me->head, nd) {
free((char *)expr->metric_name); zfree(&expr->metric_name);
free(expr->metric_refs); zfree(&expr->metric_refs);
free(expr->metric_events); zfree(&expr->metric_events);
free(expr); free(expr);
} }
...@@ -192,9 +192,9 @@ static void metric__free(struct metric *m) ...@@ -192,9 +192,9 @@ static void metric__free(struct metric *m)
if (!m) if (!m)
return; return;
free(m->metric_refs); zfree(&m->metric_refs);
expr__ctx_free(m->pctx); expr__ctx_free(m->pctx);
free((char *)m->modifier); zfree(&m->modifier);
evlist__delete(m->evlist); evlist__delete(m->evlist);
free(m); free(m);
} }
...@@ -617,7 +617,7 @@ static int decode_all_metric_ids(struct evlist *perf_evlist, const char *modifie ...@@ -617,7 +617,7 @@ static int decode_all_metric_ids(struct evlist *perf_evlist, const char *modifie
if (strstr(ev->name, "metric-id=")) { if (strstr(ev->name, "metric-id=")) {
bool has_slash = false; bool has_slash = false;
free(ev->name); zfree(&ev->name);
for (cur = strchr(sb.buf, '@') ; cur; cur = strchr(++cur, '@')) { for (cur = strchr(sb.buf, '@') ; cur; cur = strchr(++cur, '@')) {
*cur = '/'; *cur = '/';
has_slash = true; has_slash = true;
...@@ -1760,7 +1760,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp, ...@@ -1760,7 +1760,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
alloc_size = sizeof(*new_expr->metric_events); alloc_size = sizeof(*new_expr->metric_events);
new_expr->metric_events = calloc(nr + 1, alloc_size); new_expr->metric_events = calloc(nr + 1, alloc_size);
if (!new_expr->metric_events) { if (!new_expr->metric_events) {
free(new_expr->metric_refs); zfree(&new_expr->metric_refs);
free(new_expr); free(new_expr);
return -ENOMEM; return -ENOMEM;
} }
...@@ -1770,8 +1770,8 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp, ...@@ -1770,8 +1770,8 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
evsel = old_expr->metric_events[idx]; evsel = old_expr->metric_events[idx];
evsel = evlist__find_evsel(evlist, evsel->core.idx); evsel = evlist__find_evsel(evlist, evsel->core.idx);
if (evsel == NULL) { if (evsel == NULL) {
free(new_expr->metric_events); zfree(&new_expr->metric_events);
free(new_expr->metric_refs); zfree(&new_expr->metric_refs);
free(new_expr); free(new_expr);
return -EINVAL; return -EINVAL;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment