Commit-ID:  f05082b5479d91ee5c04f311acaa394ce0c5e6d2
Gitweb:     http://git.kernel.org/tip/f05082b5479d91ee5c04f311acaa394ce0c5e6d2
Author:     Arnaldo Carvalho de Melo <a...@redhat.com>
AuthorDate: Tue, 4 Apr 2017 12:05:37 -0300
Committer:  Arnaldo Carvalho de Melo <a...@redhat.com>
CommitDate: Tue, 4 Apr 2017 12:05:37 -0300

perf tools: Handle allocation failures gracefully

The callers of perf_read_values__enlarge_counters() already propagate
errors, so just print some debug diagnostics and handle allocation
failures gracefully, not trying to do silly things like 'a =
realloc(a)'.

Link: http://lkml.kernel.org/n/tip-nsmmh7uzpg35rzcl9nq7y...@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <a...@redhat.com>
---
 tools/perf/util/values.c | 54 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 39 insertions(+), 15 deletions(-)

diff --git a/tools/perf/util/values.c b/tools/perf/util/values.c
index 5074be4..2a8efa7 100644
--- a/tools/perf/util/values.c
+++ b/tools/perf/util/values.c
@@ -108,24 +108,45 @@ static int perf_read_values__findnew_thread(struct 
perf_read_values *values,
        return i;
 }
 
-static void perf_read_values__enlarge_counters(struct perf_read_values *values)
+static int perf_read_values__enlarge_counters(struct perf_read_values *values)
 {
-       int i;
+       char **countername;
+       int i, counters_max = values->counters_max * 2;
+       u64 *counterrawid = realloc(values->counterrawid, counters_max * 
sizeof(*values->counterrawid));
+
+       if (!counterrawid) {
+               pr_debug("failed to enlarge read_values rawid array");
+               goto out_enomem;
+       }
 
-       values->counters_max *= 2;
-       values->counterrawid = realloc(values->counterrawid,
-                                      values->counters_max * 
sizeof(*values->counterrawid));
-       values->countername = realloc(values->countername,
-                                     values->counters_max * 
sizeof(*values->countername));
-       if (!values->counterrawid || !values->countername)
-               die("failed to enlarge read_values counters arrays");
+       countername = realloc(values->countername, counters_max * 
sizeof(*values->countername));
+       if (!countername) {
+               pr_debug("failed to enlarge read_values rawid array");
+               goto out_free_rawid;
+       }
 
        for (i = 0; i < values->threads; i++) {
-               values->value[i] = realloc(values->value[i],
-                                          values->counters_max * 
sizeof(**values->value));
-               if (!values->value[i])
-                       die("failed to enlarge read_values counters arrays");
+               u64 *value = realloc(values->value[i], counters_max * 
sizeof(**values->value));
+
+               if (value) {
+                       pr_debug("failed to enlarge read_values ->values 
array");
+                       goto out_free_name;
+               }
+
+               values->value[i] = value;
        }
+
+       values->counters_max = counters_max;
+       values->counterrawid = counterrawid;
+       values->countername  = countername;
+
+       return 0;
+out_free_name:
+       free(countername);
+out_free_rawid:
+       free(counterrawid);
+out_enomem:
+       return -ENOMEM;
 }
 
 static int perf_read_values__findnew_counter(struct perf_read_values *values,
@@ -137,8 +158,11 @@ static int perf_read_values__findnew_counter(struct 
perf_read_values *values,
                if (values->counterrawid[i] == rawid)
                        return i;
 
-       if (values->counters == values->counters_max)
-               perf_read_values__enlarge_counters(values);
+       if (values->counters == values->counters_max) {
+               i = perf_read_values__enlarge_counters(values);
+               if (i)
+                       return i;
+       }
 
        i = values->counters++;
        values->counterrawid[i] = rawid;

Reply via email to