threads / patch / 29605

patchcolumn: Fix an incorrect parse of the 'nodense' option token

Subject: [PATCH] column: Fix an incorrect parse of the 'nodense' option token

## tl;dr

3 messages between Feb 11, 2012 and Feb 14, 2012. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Ramsay Jones· Feb 11, 2012, 18:41 UTC · lore

The parse_option() function always saw the 'nodense' token as 'dense', since it stripped the 'no' off the input argument string earlier when checking for the presence of the 'color' token.

In order to fix the parse, we use local variables (within the loop) initialised to the function input arguments instead, which allows each token check to be independent.

Also, add some 'nodense' tests to t/t9002-column.sh.
Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
---
Hi Nguyen,

While writing the earlier "column: Fix some compiler and sparse warnings" patch a few days ago, I had a feeling that a bug lurked in the parse_option() code, but I just could not put my finger on the problem; so I just sent what I had.

This morning I suddenly realised what was bothering me ... As a first step, I duplicated the 'dense' tests in t9002-column.sh and changed the 'dense' token to 'nodense' and the tests still passed!

Note that the code supports the 'nocolor' token, but it is not documented in config.txt.

If I understand correctly (and it's quite possible that I don't!), the 'nodense' token has the same meaning as the absence of the 'dense' token (and I assume a similar comment applies to the '[no]color' tokens). If that is the case, then maybe a better solution would be to simply remove the 'nodense' and 'nocolor' tokens. I will leave it to you to decide which solution is more appropriate.

ATB, Ramsay Jones

 column.c          |   13 +++++++------
 t/t9002-column.sh |   26 ++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 6 deletions(-)
Show changes to 2 files +33 −6

column.c, t/t9002-column.sh

diff --git a/column.c b/column.c
index 98328cf..6df7640 100644
--- a/column.c
+++ b/column.c
@@ -313,19 +313,20 @@ static int parse_option(const char *arg, int len,
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(opts); i++) {
-		int set = 1, name_len;
+		int set = 1, arg_len = len, name_len;
+		const char *arg_str = arg;
 
 		if (opts[i].type == OPTION) {
-			if (len > 2 && !strncmp(arg, "no", 2)) {
-				arg += 2;
-				len -= 2;
+			if (arg_len > 2 && !strncmp(arg_str, "no", 2)) {
+				arg_str += 2;
+				arg_len -= 2;
 				set = 0;
 			}
 		}
 
 		name_len = strlen(opts[i].name);
-		if (len != name_len ||
-		    strncmp(arg, opts[i].name, name_len))
+		if (arg_len != name_len ||
+		    strncmp(arg_str, opts[i].name, name_len))
 			continue;
 
 		switch (opts[i].type) {
diff --git a/t/t9002-column.sh b/t/t9002-column.sh
index 23d340e..fe7a30e 100755
--- a/t/t9002-column.sh
+++ b/t/t9002-column.sh
@@ -71,6 +71,19 @@ EOF
 	test_cmp expected actual
 '
 
+test_expect_success '20 columns, nodense' '
+	cat >expected <<\EOF &&
+one    seven
+two    eight
+three  nine
+four   ten
+five   eleven
+six
+EOF
+	git column --mode=column,nodense < lista > actual &&
+	test_cmp expected actual
+'
+
 test_expect_success '20 columns, dense' '
 	cat >expected <<\EOF &&
 one   five  nine
@@ -121,6 +134,19 @@ EOF
 	test_cmp expected actual
 '
 
+test_expect_success '20 columns, row first, nodense' '
+	cat >expected <<\EOF &&
+one    two
+three  four
+five   six
+seven  eight
+nine   ten
+eleven
+EOF
+	git column --mode=row,nodense <lista >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success '20 columns, row first, dense' '
 	cat >expected <<\EOF &&
 one   two    three
-- 
1.7.9
Nguyen Thai Ngoc Duy· Feb 12, 2012, 03:37 UTC · re: Ramsay Jones · lore

Re: [PATCH] column: Fix an incorrect parse of the 'nodense' option token

On Sun, Feb 12, 2012 at 1:41 AM, Ramsay Jones <ramsay@ramsay1.demon.co.uk> wrote:

Show 12 quoted lines
>
> The parse_option() function always saw the 'nodense' token as
> 'dense', since it stripped the 'no' off the input argument string
> earlier when checking for the presence of the 'color' token.
>
> In order to fix the parse, we use local variables (within the loop)
> initialised to the function input arguments instead, which allows
> each token check to be independent.
>
> Also, add some 'nodense' tests to t/t9002-column.sh.
>
> Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Acked-by: Nguyen Thai Ngoc Duy <pclouds@gmail.com>
Show 9 quoted lines
> Note that the code supports the 'nocolor' token, but it is not documented
> in config.txt.
>
> If I understand correctly (and it's quite possible that I don't!), the
> 'nodense' token has the same meaning as the absence of the 'dense' token
> (and I assume a similar comment applies to the '[no]color' tokens). If that
> is the case, then maybe a better solution would be to simply remove the
> 'nodense' and 'nocolor' tokens. I will leave it to you to decide which
> solution is more appropriate.

It's about overriding config. If you set "dense" by default in column.ui but do not want it in this particular run, you can say --column=nodense.

The [no]color is for plumbing only. If a command produces colored output, "color" is required to calculate text length correctly. Overriding it with "nocolor" would break the layout badly so it's no use there. It does not make sense (to me) for users to put "color" in column.ui. Which is why it's not mentioned in document.

-- 
Duy
Ramsay Jones· Feb 14, 2012, 18:28 UTC · re: Nguyen Thai Ngoc Duy · lore

Re: [PATCH] column: Fix an incorrect parse of the 'nodense' option token

Nguyen Thai Ngoc Duy wrote:
> It's about overriding config. If you set "dense" by default in
> column.ui but do not want it in this particular run, you can say
> --column=nodense.
Ah, OK, I missed that (obvious in retrospect!). Thanks.
Show 5 quoted lines
> The [no]color is for plumbing only. If a command produces colored
> output, "color" is required to calculate text length correctly.
> Overriding it with "nocolor" would break the layout badly so it's no
> use there. It does not make sense (to me) for users to put "color" in
> column.ui. Which is why it's not mentioned in document.

Er... but 'color' is documented with column.ui in config.txt. I'm obviously (still) being dense! :-D

ATB, Ramsay Jones

← back to recent threads