column: fix parsing of the '--nl' option

'git column's '--nl' option can be used to specify a "string to be printed at the end of each line" (quoting the man page), but this option and its mandatory argument has been parsed as OPT_INTEGER since the introduction of the command in 7e29b8254f (Add column layout skeleton and git-column, 2012-04-21). Consequently, any non-number argument is rejected by parse-options, and any number other than 0 leads to segfault: $ printf "%s\n" one two |git column --mode=plain --nl=foo error: option `nl' expects a numerical value $ printf "%s\n" one two |git column --mode=plain --nl=42 Segmentation fault (core dumped) $ printf "%s\n" one two |git column --mode=plain --nl=0 one two Parse this option as OPT_STRING. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Aug 18, 2021 at 11:24 UTC c93ca46cf5d0b0cd22e357a6460fa84fd4633440
3 files changed +20 -2
Documentation/git-column.txt
+1 -1
@@ -39,7 +39,7 @@ OPTIONS
39 --indent=<string>::
40 String to be printed at the beginning of each line.
41
42 ---nl=<N>::
42 +--nl=<string>::
43 String to be printed at the end of each line,
44 including newline character.
45
builtin/column.c
+1 -1
@@ -29,7 +29,7 @@ int cmd_column(int argc, const char **argv, const char *prefix)
29 OPT_INTEGER(0, "raw-mode", &colopts, N_("layout to use")),
30 OPT_INTEGER(0, "width", &copts.width, N_("maximum width")),
31 OPT_STRING(0, "indent", &copts.indent, N_("string"), N_("padding space on left border")),
32 - OPT_INTEGER(0, "nl", &copts.nl, N_("padding space on right border")),
32 + OPT_STRING(0, "nl", &copts.nl, N_("string"), N_("padding space on right border")),
33 OPT_INTEGER(0, "padding", &copts.padding, N_("padding space between columns")),
34 OPT_END()
35 };
t/t9002-column.sh
+18
@@ -42,6 +42,24 @@ EOF
42 test_cmp expected actual
43 '
44
45 +test_expect_success '--nl' '
46 + cat >expected <<\EOF &&
47 +oneZ
48 +twoZ
49 +threeZ
50 +fourZ
51 +fiveZ
52 +sixZ
53 +sevenZ
54 +eightZ
55 +nineZ
56 +tenZ
57 +elevenZ
58 +EOF
59 + git column --nl="Z$LF" --mode=plain <lista >actual &&
60 + test_cmp expected actual
61 +'
62 +
63 test_expect_success '80 columns' '
64 cat >expected <<\EOF &&
65 one two three four five six seven eight nine ten eleven