perf list: Use zfree() to reduce chances of use after free
authorArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 12 Apr 2023 12:50:08 +0000 (09:50 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 12 Apr 2023 12:55:44 +0000 (09:55 -0300)
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.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-list.c

index 29f09ce..1f5dbd5 100644 (file)
@@ -127,7 +127,7 @@ static void default_print_event(void *ps, const char *pmu_name, const char *topi
        if (strcmp(print_state->last_topic, topic ?: "")) {
                if (topic)
                        printf("\n%s:\n", topic);
-               free(print_state->last_topic);
+               zfree(&print_state->last_topic);
                print_state->last_topic = strdup(topic ?: "");
        }
 
@@ -197,7 +197,7 @@ static void default_print_metric(void *ps,
                        else
                                printf("%s\n", group);
                }
-               free(print_state->last_metricgroups);
+               zfree(&print_state->last_metricgroups);
                print_state->last_metricgroups = strdup(group ?: "");
        }
        if (!print_state->metrics)