diff options
author | Masami Hiramatsu <mhiramat@kernel.org> | 2016-05-10 01:47:53 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2016-05-10 10:58:09 -0400 |
commit | 682f4f035e0fcffce511fe77a02a0f19f0996d70 (patch) | |
tree | 49c08883b388f288ca914031618098ced5bf7785 /tools/perf/util/help-unknown-cmd.c | |
parent | 11db4e29bb50442ecef2173f325b7be4e7790025 (diff) |
perf help: Do not use ALLOC_GROW in add_cmd_list
Replace ALLOC_GROW with normal realloc code in add_cmd_list() so that it
can handle errors directly.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/20160510054752.6158.30562.stgit@devbox
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/help-unknown-cmd.c')
-rw-r--r-- | tools/perf/util/help-unknown-cmd.c | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/tools/perf/util/help-unknown-cmd.c b/tools/perf/util/help-unknown-cmd.c index 43a98a4dc1e1..d62ccaeeadd6 100644 --- a/tools/perf/util/help-unknown-cmd.c +++ b/tools/perf/util/help-unknown-cmd.c | |||
@@ -27,16 +27,27 @@ static int levenshtein_compare(const void *p1, const void *p2) | |||
27 | return l1 != l2 ? l1 - l2 : strcmp(s1, s2); | 27 | return l1 != l2 ? l1 - l2 : strcmp(s1, s2); |
28 | } | 28 | } |
29 | 29 | ||
30 | static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old) | 30 | static int add_cmd_list(struct cmdnames *cmds, struct cmdnames *old) |
31 | { | 31 | { |
32 | unsigned int i; | 32 | unsigned int i, nr = cmds->cnt + old->cnt; |
33 | 33 | void *tmp; | |
34 | ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc); | 34 | |
35 | 35 | if (nr > cmds->alloc) { | |
36 | /* Choose bigger one to alloc */ | ||
37 | if (alloc_nr(cmds->alloc) < nr) | ||
38 | cmds->alloc = nr; | ||
39 | else | ||
40 | cmds->alloc = alloc_nr(cmds->alloc); | ||
41 | tmp = realloc(cmds->names, cmds->alloc * sizeof(*cmds->names)); | ||
42 | if (!tmp) | ||
43 | return -1; | ||
44 | cmds->names = tmp; | ||
45 | } | ||
36 | for (i = 0; i < old->cnt; i++) | 46 | for (i = 0; i < old->cnt; i++) |
37 | cmds->names[cmds->cnt++] = old->names[i]; | 47 | cmds->names[cmds->cnt++] = old->names[i]; |
38 | zfree(&old->names); | 48 | zfree(&old->names); |
39 | old->cnt = 0; | 49 | old->cnt = 0; |
50 | return 0; | ||
40 | } | 51 | } |
41 | 52 | ||
42 | const char *help_unknown_cmd(const char *cmd) | 53 | const char *help_unknown_cmd(const char *cmd) |
@@ -52,8 +63,11 @@ const char *help_unknown_cmd(const char *cmd) | |||
52 | 63 | ||
53 | load_command_list("perf-", &main_cmds, &other_cmds); | 64 | load_command_list("perf-", &main_cmds, &other_cmds); |
54 | 65 | ||
55 | add_cmd_list(&main_cmds, &aliases); | 66 | if (add_cmd_list(&main_cmds, &aliases) < 0 || |
56 | add_cmd_list(&main_cmds, &other_cmds); | 67 | add_cmd_list(&main_cmds, &other_cmds) < 0) { |
68 | fprintf(stderr, "ERROR: Failed to allocate command list for unknown command.\n"); | ||
69 | goto end; | ||
70 | } | ||
57 | qsort(main_cmds.names, main_cmds.cnt, | 71 | qsort(main_cmds.names, main_cmds.cnt, |
58 | sizeof(main_cmds.names), cmdname_compare); | 72 | sizeof(main_cmds.names), cmdname_compare); |
59 | uniq(&main_cmds); | 73 | uniq(&main_cmds); |
@@ -99,6 +113,6 @@ const char *help_unknown_cmd(const char *cmd) | |||
99 | for (i = 0; i < n; i++) | 113 | for (i = 0; i < n; i++) |
100 | fprintf(stderr, "\t%s\n", main_cmds.names[i]->name); | 114 | fprintf(stderr, "\t%s\n", main_cmds.names[i]->name); |
101 | } | 115 | } |
102 | 116 | end: | |
103 | exit(1); | 117 | exit(1); |
104 | } | 118 | } |