perf annotate-data: Ensure the number of type histograms

Arnaldo reported that there is a case where nr_histograms and histograms
don't agree each other.

It ended up in a segfault trying to access a NULL histograms array.

Let's make sure to update the nr_histograms when the histograms array is
changed.

Reported-by: Arnaldo Carvalho de Melo <acme@kernel.org>
Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240510210452.2449944-2-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by Namhyung Kim and committed by Arnaldo Carvalho de Melo 2af1280b 9ef30265

+4 -1
+4 -1
tools/perf/util/annotate-data.c
··· 1800 sz += sizeof(struct type_hist_entry) * adt->self.size; 1801 1802 /* Allocate a table of pointers for each event */ 1803 - adt->nr_histograms = nr_entries; 1804 adt->histograms = calloc(nr_entries, sizeof(*adt->histograms)); 1805 if (adt->histograms == NULL) 1806 return -ENOMEM; ··· 1813 if (adt->histograms[i] == NULL) 1814 goto err; 1815 } 1816 return 0; 1817 1818 err: ··· 1828 { 1829 for (int i = 0; i < adt->nr_histograms; i++) 1830 zfree(&(adt->histograms[i])); 1831 zfree(&adt->histograms); 1832 } 1833 1834 void annotated_data_type__tree_delete(struct rb_root *root)
··· 1800 sz += sizeof(struct type_hist_entry) * adt->self.size; 1801 1802 /* Allocate a table of pointers for each event */ 1803 adt->histograms = calloc(nr_entries, sizeof(*adt->histograms)); 1804 if (adt->histograms == NULL) 1805 return -ENOMEM; ··· 1814 if (adt->histograms[i] == NULL) 1815 goto err; 1816 } 1817 + 1818 + adt->nr_histograms = nr_entries; 1819 return 0; 1820 1821 err: ··· 1827 { 1828 for (int i = 0; i < adt->nr_histograms; i++) 1829 zfree(&(adt->histograms[i])); 1830 + 1831 zfree(&adt->histograms); 1832 + adt->nr_histograms = 0; 1833 } 1834 1835 void annotated_data_type__tree_delete(struct rb_root *root)