Skip to contentSkip to conversation

sideband: highlight keywords in remote sideband output

PATCH In review Han-Wen Nienhuys 4 participants 17 messages Aug 7, 2018 – Aug 20, 2018 Documentation

lore
Share & tools
lore
#0 lore raw

v7 cover sideband: highlight keywords in remote sideband output

Fix nits; remove debug printf.

Han-Wen Nienhuys (1):
sideband: highlight keywords in remote sideband output

 Documentation/config.txt            |  12 +++
 help.c                              |   1 +
 help.h                              |   1 +
 sideband.c                          | 125 ++++++++++++++++++++++++++--
 t/t5409-colorize-remote-messages.sh |  87 +++++++++++++++++++
 5 files changed, 217 insertions(+), 9 deletions(-)
 create mode 100755 t/t5409-colorize-remote-messages.sh

--
2.18.0.597.ga71716f1ad-goog

#1 lore raw

PATCH v7 1/1 sideband: highlight keywords in remote sideband output

The colorization is controlled with the config setting "color.remote".

Supported keywords are "error", "warning", "hint" and "success". They are highlighted if they appear at the start of the line, which is common in error messages, eg.

ERROR: commit is missing Change-Id

The Git push process itself prints lots of non-actionable messages (eg. bandwidth statistics, object counters for different phases of the process). This obscures actionable error messages that servers may send back. Highlighting keywords in the sideband draws more attention to those messages.

The background for this change is that Gerrit does server-side processing to create or update code reviews, and actionable error messages (eg. missing Change-Id) must be communicated back to the user during the push. User research has shown that new users have trouble seeing these messages.

The highlighting is done on the client rather than server side, so servers don't have to grow capabilities to understand terminal escape codes and terminal state. It also consistent with the current state where Git is control of the local display (eg. prefixing messages with "remote: ").

The highlighting can be configured using color.remote.<KEYWORD> configuration settings. Since the keys are matched case insensitively, we match the keywords case insensitively too.

Finally, this solution is backwards compatible: many servers already prefix their messages with "error", and they will benefit from this change without requiring a server update. By contrast, a server-side solution would likely require plumbing the TERM variable through the git protocol, so it would require changes to both server and client.

---
 Documentation/config.txt            |  12 +++
 help.c                              |   1 +
 help.h                              |   1 +
 sideband.c                          | 125 ++++++++++++++++++++++++++--
 t/t5409-colorize-remote-messages.sh |  87 +++++++++++++++++++
 5 files changed, 217 insertions(+), 9 deletions(-)
 create mode 100755 t/t5409-colorize-remote-messages.sh
Documentation/config.txt +12 -0
index 63365dcf3d..33bc1a3def 100644
@@ -1263,6 +1263,18 @@ color.push::
 color.push.error::
 	Use customized color for push errors.
 
+color.remote::
+	If set, keywords at the start of the line are highlighted. The
+	keywords are "error", "warning", "hint" and "success", and are
+	matched case-insensitively. Maybe set to `always`, `false` (or
+	`never`) or `auto` (or `true`). If unset, then the value of
+	`color.ui` is used (`auto` by default).
+
+color.remote.<slot>::
+	Use customized color for each remote keyword. `<slot>` may be
+	`hint`, `warning`, `success` or `error` which match the
+	corresponding keyword.
+
 color.showBranch::
 	A boolean to enable/disable color in the output of
 	linkgit:git-show-branch[1]. May be set to `always`,
help.c +1 -0
index 3ebf0568db..b6cafcfc0a 100644
@@ -425,6 +425,7 @@ void list_config_help(int for_human)
 		{ "color.diff", "<slot>", list_config_color_diff_slots },
 		{ "color.grep", "<slot>", list_config_color_grep_slots },
 		{ "color.interactive", "<slot>", list_config_color_interactive_slots },
+		{ "color.remote", "<slot>", list_config_color_sideband_slots },
 		{ "color.status", "<slot>", list_config_color_status_slots },
 		{ "fsck", "<msg-id>", list_config_fsck_msg_ids },
 		{ "receive.fsck", "<msg-id>", list_config_fsck_msg_ids },
help.h +1 -0
index f8b15323a6..9eab6a3f89 100644
@@ -83,6 +83,7 @@ void list_config_color_diff_slots(struct string_list *list, const char *prefix);
 void list_config_color_grep_slots(struct string_list *list, const char *prefix);
 void list_config_color_interactive_slots(struct string_list *list, const char *prefix);
 void list_config_color_status_slots(struct string_list *list, const char *prefix);
+void list_config_color_sideband_slots(struct string_list *list, const char *prefix);
 void list_config_fsck_msg_ids(struct string_list *list, const char *prefix);
 
 #endif /* HELP_H */
sideband.c +116 -9

150 lines not on this page · raw message

t/t5409-colorize-remote-messages.sh new file +87 -0

87 lines not on this page · raw message

Signature · 1 line
2.18.0.597.ga71716f1ad-goog
#15 lore raw
In reply to Han-Wen Nienhuys
Quoted 13 lines · Han-Wen Nienhuys

Fix nits; remove debug printf.

Han-Wen Nienhuys (1):
sideband: highlight keywords in remote sideband output

 Documentation/config.txt            |  12 +++
 help.c                              |   1 +
 help.h                              |   1 +
 sideband.c                          | 125 ++++++++++++++++++++++++++--
 t/t5409-colorize-remote-messages.sh |  87 +++++++++++++++++++
 5 files changed, 217 insertions(+), 9 deletions(-)
 create mode 100755 t/t5409-colorize-remote-messages.sh
Quoted 2 lines

--
2.18.0.597.ga71716f1ad-goog

I'll squash in the following while queuing for

<CAPig+cScb_7s4a_ZSVCsr+nBxAHGHZVMZOtnrOgbhZUi96-VFA@mail.gmail.com>

Thanks for sticking to the topic.

Documentation/config.txt +1 -1
index 33bc1a3def..9a38dd2cbb 100644
@@ -1266,7 +1266,7 @@ color.push.error::
 color.remote::
 	If set, keywords at the start of the line are highlighted. The
 	keywords are "error", "warning", "hint" and "success", and are
-	matched case-insensitively. Maybe set to `always`, `false` (or
+	matched case-insensitively. May be set to `always`, `false` (or
 	`never`) or `auto` (or `true`). If unset, then the value of
 	`color.ui` is used (`auto` by default).
 
#16 lore raw
Quoted 16 lines · Junio C Hamano
Quoted 13 lines · Han-Wen Nienhuys

Fix nits; remove debug printf.

Han-Wen Nienhuys (1):
sideband: highlight keywords in remote sideband output

 Documentation/config.txt            |  12 +++
 help.c                              |   1 +
 help.h                              |   1 +
 sideband.c                          | 125 ++++++++++++++++++++++++++--
 t/t5409-colorize-remote-messages.sh |  87 +++++++++++++++++++
 5 files changed, 217 insertions(+), 9 deletions(-)
 create mode 100755 t/t5409-colorize-remote-messages.sh
Quoted 8 lines
Quoted 2 lines

--
2.18.0.597.ga71716f1ad-goog

I'll squash in the following while queuing for

<CAPig+cScb_7s4a_ZSVCsr+nBxAHGHZVMZOtnrOgbhZUi96-VFA@mail.gmail.com>

Thanks for sticking to the topic.

Thanks, LGTM.

--

Google Germany GmbH, Erika-Mann-Strasse 33, 80636 Munich

Registergericht und -nummer: Hamburg, HRB 86891

Sitz der Gesellschaft: Hamburg

Geschäftsführer: Paul Manicle, Halimah DeLaine Prado

#2 lore raw
In reply to Han-Wen Nienhuys
Quoted 9 lines · Han-Wen Nienhuys
(quoted diff) +9 -0
+/*
+ * Optionally highlight one keyword in remote output if it appears at the start
+ * of the line. This should be called for a single line only, which is
+ * passed as the first N characters of the SRC array.
+ */
+static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
+{
+	int i;
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+	if (!want_color_stderr(use_sideband_colors())) {
+		strbuf_add(dest, src, n);
+		return;
+	}
+
+	while (isspace(*src)) {
+		strbuf_addch(dest, *src);
+		src++;
+		n--;
+	}

This loop can run out of bytes in src in search of non-space before n gets to zero or negative, and when that happens ...

Review

Quoted 10 lines
(quoted diff) +10 -0
+	for (i = 0; i < ARRAY_SIZE(keywords); i++) {
+		struct keyword_entry *p = keywords + i;
+		int len = strlen(p->keyword);
+		/*
+		 * Match case insensitively, so we colorize output from existing
+		 * servers regardless of the case that they use for their
+		 * messages. We only highlight the word precisely, so
+		 * "successful" stays uncolored.
+		 */
+		if (!strncasecmp(p->keyword, src, len) && !isalnum(src[len])) {

... these access src[] beyond the end of what the caller intended to show us, and also ...

Review

Quoted 10 lines
(quoted diff) +10 -0
+			strbuf_addstr(dest, p->color);
+			strbuf_add(dest, src, len);
+			strbuf_addstr(dest, GIT_COLOR_RESET);
+			n -= len;
+			src += len;
+			break;
+		}
+	}
+
+	strbuf_add(dest, src, n);

... this will now try to add 0 or negative number of bytes.

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+}
+

Perhaps this will help (not really tested). The second hunk is an unrelated style clean-up.

 sideband.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
sideband.c +3 -2
index 1c6bb0e25b..d99a559a44 100644
@@ -75,11 +75,13 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 		return;
 	}
 
-	while (isspace(*src)) {
+	while (isspace(*src) && n) {
 		strbuf_addch(dest, *src);
 		src++;
 		n--;
 	}
+	if (!n)
+		return;
 
 	for (i = 0; i < ARRAY_SIZE(keywords); i++) {
 		struct keyword_entry *p = keywords + i;
@@ -101,7 +103,6 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 	}
 
 	strbuf_add(dest, src, n);
-
 }
 
 
#3 lore raw

Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output

Quoted 16 lines · Junio C Hamano

This loop can run out of bytes in src in search of non-space before n gets to zero or negative, and when that happens ...

Review

Quoted 10 lines
(quoted diff) +10 -0
+	for (i = 0; i < ARRAY_SIZE(keywords); i++) {
+		struct keyword_entry *p = keywords + i;
+		int len = strlen(p->keyword);
+		/*
+		 * Match case insensitively, so we colorize output from existing
+		 * servers regardless of the case that they use for their
+		 * messages. We only highlight the word precisely, so
+		 * "successful" stays uncolored.
+		 */
+		if (!strncasecmp(p->keyword, src, len) && !isalnum(src[len])) {

... these access src[] beyond the end of what the caller intended to show us, and also ...

Actually, leaving when !n before this loop is insufficient. src[] may have 2 bytes "in" remaining, and we may be trying to see if it begins with "info", for example, and using strncasecmp() with len==4 would of course read beyond the end of src[].

-- >8 --
Subject: sideband: do not read beyond the end of input

The caller of maybe_colorize_sideband() gives a counted buffer <src,n>, but the callee checked *src as if it were a NUL terminated buffer. If src[] had all isspace() bytes in it, we would have made n negative, and then (1) called number of strncasecmp() to see if the remaining bytes in src[] matched keywords, reading beyond the end of the array, and/or (2) called strbuf_add() with negative count, most likely triggering the "you want to use way too much memory" error due to unsigned integer overflow.

---
 sideband.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
sideband.c +6 -3
index 1c6bb0e25b..372039247f 100644
@@ -75,7 +75,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 		return;
 	}
 
-	while (isspace(*src)) {
+	while (0 < n && isspace(*src)) {
 		strbuf_addch(dest, *src);
 		src++;
 		n--;
@@ -84,6 +84,9 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 	for (i = 0; i < ARRAY_SIZE(keywords); i++) {
 		struct keyword_entry *p = keywords + i;
 		int len = strlen(p->keyword);
+
+		if (n <= len)
+			continue;
 		/*
 		 * Match case insensitively, so we colorize output from existing
 		 * servers regardless of the case that they use for their
@@ -100,8 +103,8 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 		}
 	}
 
-	strbuf_add(dest, src, n);
-
+	if (0 < n)
+		strbuf_add(dest, src, n);
 }
 
 
Reviewed-by
#4 lore raw

Re: Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output

(-cc: my @google.com email)
Hi,

Quoted 11 lines · Junio C Hamano

Subject: sideband: do not read beyond the end of input

The caller of maybe_colorize_sideband() gives a counted buffer <src,n>, but the callee checked *src as if it were a NUL terminated buffer. If src[] had all isspace() bytes in it, we would have made n negative, and then (1) called number of strncasecmp() to see if the remaining bytes in src[] matched keywords, reading beyond the end of the array, and/or (2) called strbuf_add() with negative count, most likely triggering the "you want to use way too much memory" error due to unsigned integer overflow.

Quoted 4 lines
---
 sideband.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

This indeed avoids the "you want to use way too much memory" error when I apply it.

sideband.c static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)

Quoted 3 lines
sideband.c +0 -0
@@ -75,7 +75,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)

Not about this patch: should the 'n' parameter be a size_t instead of an int? It doesn't matter in practice (since the caller has an int, it can never be more than INT_MAX) but it might make the intent clearer.

Based on inspecting the caller, using an int seems fine.

sideband.c static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) · line 78

Quoted 5 lines
sideband.c +1 -1
 		return;
 	}
 
-	while (isspace(*src)) {
+	while (0 < n && isspace(*src)) {

Yes, we need to check 'n && isspace(*src)' to avoid overflowing the buffer if it consists entirely of spaces.

sideband.c static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) · line 89

Quoted 10 lines
sideband.c +3 -0
 		strbuf_addch(dest, *src);
 		src++;
 		n--;
@@ -84,6 +84,9 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 	for (i = 0; i < ARRAY_SIZE(keywords); i++) {
 		struct keyword_entry *p = keywords + i;
 		int len = strlen(p->keyword);
+
+		if (n <= len)
+			continue;

Using <= instead of < since we look at the character after the word as well. Good.

Quoted 7 lines
sideband.c +0 -0
 		/*
 		 * Match case insensitively, so we colorize output from existing
 		 * servers regardless of the case that they use for their
		 * messages. We only highlight the word precisely, so
		 * "successful" stays uncolored.
		 */
		if (!strncasecmp(p->keyword, src, len) && !isalnum(src[len])) {

Not about this patch: should this check "&& src[len] == ':'" instead, as discussed upthread?

sideband.c static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) · line 107

Quoted 7 lines
sideband.c +2 -1
@@ -100,8 +103,8 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 		}
 	}
 
-	strbuf_add(dest, src, n);
+	if (0 < n)
+		strbuf_add(dest, src, n);

This check seems unnecessary. strbuf_add can cope fine with !n. Should we put

	assert(n >= 0);

or even

	if (n < 0)
		BUG();

instead, since the earlier part of the fix guarantees that n >= 0?

Thanks for the careful work. With or without such a change,

Thanks.

#13 lore raw

Re: Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output

In reply to Junio C Hamano
Quoted 11 lines · Junio C Hamano

Subject: sideband: do not read beyond the end of input

The caller of maybe_colorize_sideband() gives a counted buffer <src,n>, but the callee checked *src as if it were a NUL terminated buffer. If src[] had all isspace() bytes in it, we would have made n negative, and then (1) called number of strncasecmp() to see if the remaining bytes in src[] matched keywords, reading beyond the end of the array, and/or (2) called strbuf_add() with negative count, most likely triggering the "you want to use way too much memory" error due to unsigned integer overflow.

Quoted 4 lines
---
 sideband.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

And here are some tests to squash in.

A sideband line consisting entirely of spaces causes us to overflow our buffer and end up with a negative number of remaining characters, ultimately resulting in the message

fatal: you want to use way too much memory

when parsing it in order to add color.

We also forget to limit a strncasecmp and isalnum looking for keywords to color in to the memory region passed in. Fortunately all callers put a delimiter character (\0, \r, or \n) after the end of the region so this does not cause trouble in practice. Add a test for futureproofing to protect the new bounds checking code in case that ever changes.

---
Thanks again,
Jonathan

 t/t5409-colorize-remote-messages.sh | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
t/t5409-colorize-remote-messages.sh +14 -0
index eb1b8aa05df..f81b6813c03 100755
@@ -15,6 +15,8 @@ test_expect_success 'setup' '
 	echo warning: warning
 	echo prefixerror: error
 	echo " " "error: leading space"
+	echo "    "
+	echo Err
 	exit 0
 	EOF
 	echo 1 >file &&
@@ -44,6 +46,12 @@ test_expect_success 'whole words at line start' '
 	grep "prefixerror: error" decoded
 '
 
+test_expect_success 'short line' '
+	git -C child -c color.remote=always push -f origin HEAD:short-line 2>output &&
+	test_decode_color <output >decoded &&
+	grep "remote: Err" decoded
+'
+
 test_expect_success 'case-insensitive' '
 	git --git-dir child/.git -c color.remote=always push -f origin HEAD:refs/heads/case-insensitive 2>output &&
 	cat output &&
@@ -58,6 +66,12 @@ test_expect_success 'leading space' '
 	grep "  <BOLD;RED>error<RESET>: leading space" decoded
 '
 
+test_expect_success 'spaces only' '
+	git -C child -c color.remote=always push -f origin HEAD:only-space 2>output &&
+	test_decode_color <output >decoded &&
+	grep "remote:     " decoded
+'
+
 test_expect_success 'no coloring for redirected output' '
 	git --git-dir child/.git push -f origin HEAD:refs/heads/redirected-output 2>output &&
 	test_decode_color <output >decoded &&
Signature · 1 line
2.18.0.1017.ga543ac7ca45
#5 lore raw

Re: Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output

In reply to Jonathan Nieder
Quoted 8 lines · Jonathan Nieder

sideband.c static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)

Quoted 3 lines
sideband.c +0 -0
@@ -75,7 +75,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)

Not about this patch: should the 'n' parameter be a size_t instead of an int? It doesn't matter in practice (since the caller has an int, it can never be more than INT_MAX) but it might make the intent clearer.

I tend to agree, but I think a separate "clean-up" patch to do so is more appropriate than rolling it into this fix.

Quoted 10 lines

sideband.c static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) · line 81

Quoted 7 lines
sideband.c +0 -0
 		/*
 		 * Match case insensitively, so we colorize output from existing
 		 * servers regardless of the case that they use for their
 		 * messages. We only highlight the word precisely, so
 		 * "successful" stays uncolored.
 		 */
 		if (!strncasecmp(p->keyword, src, len) && !isalnum(src[len])) {

Not about this patch: should this check "&& src[len] == ':'" instead, as discussed upthread?

I originally was of an opinion that we should take only lowercase keyword followed by a colon, primarily because that is what we produce. Then "the real world need" told us that we are better off catching the keyword case-insensitively. Recalling that lesson, I am not sure I would support "let's limit to the colon, rejecting any other punctionation letter".

In any case, we should make such a policy decision outside a patch like this one that is about fixing a behaviour which all users would consider as a bug regardless of the policy they support.

Quoted 9 lines

sideband.c static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) · line 107

Quoted 7 lines
sideband.c +2 -1
@@ -100,8 +103,8 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 		}
 	}
 
-	strbuf_add(dest, src, n);
+	if (0 < n)
+		strbuf_add(dest, src, n);

This check seems unnecessary. strbuf_add can cope fine with !n.

I was primarily interested in catching negatives, and !n was a mere optimization, but you are correct to point out that negative n at this point in the codeflow is a BUG().

#6 lore raw

Re: Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output

Quoted 9 lines · Junio C Hamano
Quoted 5 lines

sideband.c static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) · line 107

Quoted 3 lines
sideband.c +2 -1
-	strbuf_add(dest, src, n);
+	if (0 < n)
+		strbuf_add(dest, src, n);

This check seems unnecessary. strbuf_add can cope fine with !n.

I was primarily interested in catching negatives, and !n was a mere optimization, but you are correct to point out that negative n at this point in the codeflow is a BUG().

Actually, let's just lose the conditional. strbuf_add() would catch and issue an error message when it notices that we fed negative count anyway ;-).

#7 lore raw

Re: Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output

In reply to Junio C Hamano
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Junio C Hamano
Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +2 -1
-	strbuf_add(dest, src, n);
+	if (0 < n)
+		strbuf_add(dest, src, n);

This check seems unnecessary. strbuf_add can cope fine with !n.

Quoted 7 lines
Quoted 3 lines

I was primarily interested in catching negatives, and !n was a mere optimization, but you are correct to point out that negative n at this point in the codeflow is a BUG().

Actually, let's just lose the conditional. strbuf_add() would catch and issue an error message when it notices that we fed negative count anyway ;-).

So I'll have this applied on top of the original topic to prevent a buggy version from escaping the lab.

-- >8 --
Subject: [PATCH] sideband: do not read beyond the end of input

The caller of maybe_colorize_sideband() gives a counted buffer <src, n>, but the callee checked src[] as if it were a NUL terminated buffer. If src[] had all isspace() bytes in it, we would have made n negative, and then

(1) made number of strncasecmp() calls to see if the remaining bytes in src[] matched keywords, reading beyond the end of the array (this actually happens even if n does not go negative), and/or

(2) called strbuf_add() with negative count, most likely triggering the "you want to use way too much memory" error due to unsigned integer overflow.

Fix both issues by making sure we do not go beyond &src[n].

In the longer term we may want to accept size_t as parameter for clarity (even though we know that a sideband message we are painting typically would fit on a line on a terminal and int is sufficient). Write it down as a NEEDSWORK comment.

---
 sideband.c                          |  8 ++++++--
 t/t5409-colorize-remote-messages.sh | 14 ++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)
sideband.c +6 -2
index 1c6bb0e25b..368647acf8 100644
@@ -65,6 +65,8 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
  * Optionally highlight one keyword in remote output if it appears at the start
  * of the line. This should be called for a single line only, which is
  * passed as the first N characters of the SRC array.
+ *
+ * NEEDSWORK: use "size_t n" instead for clarity.
  */
 static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 {
@@ -75,7 +77,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 		return;
 	}
 
-	while (isspace(*src)) {
+	while (0 < n && isspace(*src)) {
 		strbuf_addch(dest, *src);
 		src++;
 		n--;
@@ -84,6 +86,9 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 	for (i = 0; i < ARRAY_SIZE(keywords); i++) {
 		struct keyword_entry *p = keywords + i;
 		int len = strlen(p->keyword);
+
+		if (n <= len)
+			continue;
 		/*
 		 * Match case insensitively, so we colorize output from existing
 		 * servers regardless of the case that they use for their
@@ -101,7 +106,6 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 	}
 
 	strbuf_add(dest, src, n);
-
 }
 
 
t/t5409-colorize-remote-messages.sh +14 -0
index eb1b8aa05d..f81b6813c0 100755
@@ -15,6 +15,8 @@ test_expect_success 'setup' '
 	echo warning: warning
 	echo prefixerror: error
 	echo " " "error: leading space"
+	echo "    "
+	echo Err
 	exit 0
 	EOF
 	echo 1 >file &&
@@ -44,6 +46,12 @@ test_expect_success 'whole words at line start' '
 	grep "prefixerror: error" decoded
 '
 
+test_expect_success 'short line' '
+	git -C child -c color.remote=always push -f origin HEAD:short-line 2>output &&
+	test_decode_color <output >decoded &&
+	grep "remote: Err" decoded
+'
+
 test_expect_success 'case-insensitive' '
 	git --git-dir child/.git -c color.remote=always push -f origin HEAD:refs/heads/case-insensitive 2>output &&
 	cat output &&
@@ -58,6 +66,12 @@ test_expect_success 'leading space' '
 	grep "  <BOLD;RED>error<RESET>: leading space" decoded
 '
 
+test_expect_success 'spaces only' '
+	git -C child -c color.remote=always push -f origin HEAD:only-space 2>output &&
+	test_decode_color <output >decoded &&
+	grep "remote:     " decoded
+'
+
 test_expect_success 'no coloring for redirected output' '
 	git --git-dir child/.git push -f origin HEAD:refs/heads/redirected-output 2>output &&
 	test_decode_color <output >decoded &&
Signature · 1 line
2.18.0-748-gfa03cdc39b
#8 lore raw

Re: Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output

Quoted 13 lines · Junio C Hamano

-- >8 --
Subject: [PATCH] sideband: do not read beyond the end of input

The caller of maybe_colorize_sideband() gives a counted buffer <src, n>, but the callee checked src[] as if it were a NUL terminated buffer. If src[] had all isspace() bytes in it, we would have made n negative, and then

(1) made number of strncasecmp() calls to see if the remaining bytes in src[] matched keywords, reading beyond the end of the array (this actually happens even if n does not go negative), and/or

Quoted 5 lines

(2) called strbuf_add() with negative count, most likely triggering the "you want to use way too much memory" error due to unsigned integer overflow.

Fix both issues by making sure we do not go beyond &src[n].

Thanks. I've been sporadically seeing "fatal: you want to use way too much memory" the last few days while running 'next', and finally managed to catch a reproducible case. This patch definitely fixes it.

Quoted 4 lines

In the longer term we may want to accept size_t as parameter for clarity (even though we know that a sideband message we are painting typically would fit on a line on a terminal and int is sufficient). Write it down as a NEEDSWORK comment.

This "typically" made me nervous about what happens in the non-typical case, but I think we can say something even stronger: the length comes from a pktline, so the maximum is less than 16 bits. I wondered if we might ever call this on the accumulated string from multiple sidebands, but it doesn't look like it.

-Peff

#10 lore raw

Re: Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output

In reply to Junio C Hamano
Quoted 61 lines · Junio C Hamano
Quoted 3 lines

Actually, let's just lose the conditional. strbuf_add() would catch and issue an error message when it notices that we fed negative count anyway ;-).

So I'll have this applied on top of the original topic to prevent a buggy version from escaping the lab.

-- >8 --
Subject: [PATCH] sideband: do not read beyond the end of input

The caller of maybe_colorize_sideband() gives a counted buffer <src, n>, but the callee checked src[] as if it were a NUL terminated buffer. If src[] had all isspace() bytes in it, we would have made n negative, and then

(1) made number of strncasecmp() calls to see if the remaining bytes in src[] matched keywords, reading beyond the end of the array (this actually happens even if n does not go negative), and/or

(2) called strbuf_add() with negative count, most likely triggering the "you want to use way too much memory" error due to unsigned integer overflow.

Fix both issues by making sure we do not go beyond &src[n].

In the longer term we may want to accept size_t as parameter for clarity (even though we know that a sideband message we are painting typically would fit on a line on a terminal and int is sufficient). Write it down as a NEEDSWORK comment.

---
 sideband.c                          |  8 ++++++--
 t/t5409-colorize-remote-messages.sh | 14 ++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)
sideband.c +3 -1
index 1c6bb0e25b..368647acf8 100644
@@ -65,6 +65,8 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
  * Optionally highlight one keyword in remote output if it appears at the start
  * of the line. This should be called for a single line only, which is
  * passed as the first N characters of the SRC array.
+ *
+ * NEEDSWORK: use "size_t n" instead for clarity.
  */
 static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 {
@@ -75,7 +77,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
                return;
        }
 
-       while (isspace(*src)) {
+       while (0 < n && isspace(*src)) {
                strbuf_addch(dest, *src);
                src++;
                n--;

sideband.c static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) · line 91

Quoted 6 lines
sideband.c +3 -0
@@ -84,6 +86,9 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
        for (i = 0; i < ARRAY_SIZE(keywords); i++) {
                struct keyword_entry *p = keywords + i;
                int len = strlen(p->keyword);
+
+               if (n <= len)
+                       continue;

I would suggest

if (n < len) continue;
..
if (!strncasecmp(p->keyword, src, len) && (n == len || !isalnum(src[len]))) {

so we colorize a single line that looks like "warning" as well

Other than that, LGTM.

--
Google Germany GmbH, Erika-Mann-Strasse 33, 80636 Munich Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado

#11 lore raw

Re: Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output

and, thanks for cleaning up after me :)

--

Google Germany GmbH, Erika-Mann-Strasse 33, 80636 Munich

Registergericht und -nummer: Hamburg, HRB 86891

Sitz der Gesellschaft: Hamburg

Geschäftsführer: Paul Manicle, Halimah DeLaine Prado

#9 lore raw

Re: Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output

In reply to Jeff King
Quoted 10 lines · Jeff King
Quoted 4 lines

In the longer term we may want to accept size_t as parameter for clarity (even though we know that a sideband message we are painting typically would fit on a line on a terminal and int is sufficient). Write it down as a NEEDSWORK comment.

This "typically" made me nervous about what happens in the non-typical case, but I think we can say something even stronger: the length comes from a pktline, so the maximum is less than 16 bits. I wondered if we might ever call this on the accumulated string from multiple sidebands, but it doesn't look like it.

I think a sideband packet may be split on one but not the other codepath to result in multiple calls, but I do not think we coalesce them together, so I agree that int is sufficient.

#12 lore raw

Re: Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output

In reply to Han-Wen Nienhuys
Quoted 10 lines · Han-Wen Nienhuys

sideband.c static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n) · line 91

Quoted 7 lines
sideband.c +3 -0
@@ -84,6 +86,9 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
        for (i = 0; i < ARRAY_SIZE(keywords); i++) {
                struct keyword_entry *p = keywords + i;
                int len = strlen(p->keyword);
+
+               if (n <= len)
+                       continue;

I would suggest

Quoted 5 lines

if (n < len) continue;
..
if (!strncasecmp(p->keyword, src, len) && (n == len || !isalnum(src[len]))) {

so we colorize a single line that looks like "warning" as well

That's the kind of thing I would have mentioned in the initial review of the feature, and I do not think it is a bad idea.

I do think it is a bad idea to roll it into this patch, though.