aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/perf_counter/util/help.c
diff options
context:
space:
mode:
authorIngo Molnar <mingo@elte.hu>2009-06-03 14:09:11 -0400
committerIngo Molnar <mingo@elte.hu>2009-06-03 14:09:11 -0400
commit051cdc3c2d0e24443ac03aff03ee89807ec8c589 (patch)
tree4a1db532835c656a0cb6c8e2bfdb3fddd734fb1d /Documentation/perf_counter/util/help.c
parenteed4dcd443da7a46131ef37c7a389b444905960e (diff)
perf help: Fix bug when there's no perf-* command around
main_cmds can be empty - fix util/help.c to handle this case without segfaulting. Reported-by: Steven Rostedt <rostedt@goodmis.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Mike Galbraith <efault@gmx.de> Cc: Paul Mackerras <paulus@samba.org> Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com> Cc: Marcelo Tosatti <mtosatti@redhat.com> Cc: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Thomas Gleixner <tglx@linutronix.de> LKML-Reference: <new-submission> Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'Documentation/perf_counter/util/help.c')
-rw-r--r--Documentation/perf_counter/util/help.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/Documentation/perf_counter/util/help.c b/Documentation/perf_counter/util/help.c
index 397487fb2bee..6653f7dd1d78 100644
--- a/Documentation/perf_counter/util/help.c
+++ b/Documentation/perf_counter/util/help.c
@@ -298,7 +298,7 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
298 298
299const char *help_unknown_cmd(const char *cmd) 299const char *help_unknown_cmd(const char *cmd)
300{ 300{
301 int i, n, best_similarity = 0; 301 int i, n = 0, best_similarity = 0;
302 struct cmdnames main_cmds, other_cmds; 302 struct cmdnames main_cmds, other_cmds;
303 303
304 memset(&main_cmds, 0, sizeof(main_cmds)); 304 memset(&main_cmds, 0, sizeof(main_cmds));
@@ -315,20 +315,24 @@ const char *help_unknown_cmd(const char *cmd)
315 sizeof(main_cmds.names), cmdname_compare); 315 sizeof(main_cmds.names), cmdname_compare);
316 uniq(&main_cmds); 316 uniq(&main_cmds);
317 317
318 /* This reuses cmdname->len for similarity index */ 318 if (main_cmds.cnt) {
319 for (i = 0; i < main_cmds.cnt; ++i) 319 /* This reuses cmdname->len for similarity index */
320 main_cmds.names[i]->len = 320 for (i = 0; i < main_cmds.cnt; ++i)
321 levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4); 321 main_cmds.names[i]->len =
322 levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
322 323
323 qsort(main_cmds.names, main_cmds.cnt, 324 qsort(main_cmds.names, main_cmds.cnt,
324 sizeof(*main_cmds.names), levenshtein_compare); 325 sizeof(*main_cmds.names), levenshtein_compare);
326
327 best_similarity = main_cmds.names[0]->len;
328 n = 1;
329 while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
330 ++n;
331 }
325 332
326 best_similarity = main_cmds.names[0]->len;
327 n = 1;
328 while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
329 ++n;
330 if (autocorrect && n == 1) { 333 if (autocorrect && n == 1) {
331 const char *assumed = main_cmds.names[0]->name; 334 const char *assumed = main_cmds.names[0]->name;
335
332 main_cmds.names[0] = NULL; 336 main_cmds.names[0] = NULL;
333 clean_cmdnames(&main_cmds); 337 clean_cmdnames(&main_cmds);
334 fprintf(stderr, "WARNING: You called a Git program named '%s', " 338 fprintf(stderr, "WARNING: You called a Git program named '%s', "
@@ -345,7 +349,7 @@ const char *help_unknown_cmd(const char *cmd)
345 349
346 fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd); 350 fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd);
347 351
348 if (best_similarity < 6) { 352 if (main_cmds.cnt && best_similarity < 6) {
349 fprintf(stderr, "\nDid you mean %s?\n", 353 fprintf(stderr, "\nDid you mean %s?\n",
350 n < 2 ? "this": "one of these"); 354 n < 2 ? "this": "one of these");
351 355