help -a: handle aliases with long names gracefully

We take pains to determine the longest command beforehand, so that we can align the category column after printing the command names. However, then we re-use that value when printing the aliases. If any alias name is longer than the longest command name, we consequently try to add a negative number of spaces (but `mput_char()` does not expect any negative values and simply decrements until the value is 0, i.e. it tries to add close to 2**31 spaces). Let's fix this by adjusting the `longest` variable before printing the aliases. This fixes https://github.com/git-for-windows/git/issues/1975. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Dec 11, 2018 at 06:58 UTC 1c4b985965a4c424e7e5ae4756e139c98183278d
1 file changed +9 -1
help.c
+9 -1
@@ -83,8 +83,9 @@ static void print_command_list(const struct cmdname_help *cmds,
83
84 for (i = 0; cmds[i].name; i++) {
85 if (cmds[i].category & mask) {
86 + size_t len = strlen(cmds[i].name);
87 printf(" %s ", cmds[i].name);
87 - mput_char(' ', longest - strlen(cmds[i].name));
88 + mput_char(' ', longest > len ? longest - len : 1);
89 puts(_(cmds[i].help));
90 }
91 }
@@ -526,6 +527,13 @@ void list_all_cmds_help(void)
527
528 git_config(get_alias, &alias_list);
529 string_list_sort(&alias_list);
530 +
531 + for (i = 0; i < alias_list.nr; i++) {
532 + size_t len = strlen(alias_list.items[i].string);
533 + if (longest < len)
534 + longest = len;
535 + }
536 +
537 if (alias_list.nr) {
538 printf("\n%s\n", _("Command aliases"));
539 ALLOC_ARRAY(aliases, alias_list.nr + 1);