From c2f41bf521b5b2ffb9ea93b98e4a57bf73d70864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Thu, 19 Jan 2017 18:41:21 +0700 Subject: color.c: fix color_parse_mem() with value_len == 0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In this code we want to match the word "reset". If len is zero, strncasecmp() will return zero and we incorrectly assume it's "reset" as a result. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- color.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'color.c') diff --git a/color.c b/color.c index 81c2676723..a9eadd190a 100644 --- a/color.c +++ b/color.c @@ -207,6 +207,9 @@ int color_parse_mem(const char *value, int value_len, char *dst) struct color fg = { COLOR_UNSPECIFIED }; struct color bg = { COLOR_UNSPECIFIED }; + if (!len) + return -1; + if (!strncasecmp(value, "reset", len)) { xsnprintf(dst, end - dst, GIT_COLOR_RESET); return 0; -- cgit v1.2.3 From bc4075653e3f704f0440ec54e16f88fbc39a682d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Thu, 19 Jan 2017 18:41:22 +0700 Subject: color.c: trim leading spaces in color_parse_mem() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Normally color_parse_mem() is called from config parser which trims the leading spaces already. The new caller in the next patch won't. Let's be tidy and trim leading spaces too (we already trim trailing spaces after a word). Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- color.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'color.c') diff --git a/color.c b/color.c index a9eadd190a..7bb4a96f8c 100644 --- a/color.c +++ b/color.c @@ -207,10 +207,15 @@ int color_parse_mem(const char *value, int value_len, char *dst) struct color fg = { COLOR_UNSPECIFIED }; struct color bg = { COLOR_UNSPECIFIED }; + while (len > 0 && isspace(*ptr)) { + ptr++; + len--; + } + if (!len) return -1; - if (!strncasecmp(value, "reset", len)) { + if (!strncasecmp(ptr, "reset", len)) { xsnprintf(dst, end - dst, GIT_COLOR_RESET); return 0; } -- cgit v1.2.3 From 55cccf4bb3d7e99223b8ebb3d4480f73c456dd61 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 1 Feb 2017 01:21:29 +0100 Subject: color_parse_mem: allow empty color spec Prior to c2f41bf52 (color.c: fix color_parse_mem() with value_len == 0, 2017-01-19), the empty string was interpreted as a color "reset". This was an accidental outcome, and that commit turned it into an error. However, scripts may pass the empty string as a default value to "git config --get-color" to disable color when the value is not defined. The git-add--interactive script does this. As a result, the script is unusable since c2f41bf52 unless you have color.diff.plain defined (if it is defined, then we don't parse the empty default at all). Our test scripts didn't notice the recent breakage because they run without a terminal, and thus without color. They never hit this code path at all. And nobody noticed the original buggy "reset" behavior, because it was effectively a noop. Let's fix the code to have an empty color name produce an empty sequence of color codes. The tests need a few fixups: - we'll add a new test in t4026 to cover this case. But note that we need to tweak the color() helper. While we're there, let's factor out the literal ANSI ESC character. Otherwise it makes the diff quite hard to read. - we'll add a basic sanity-check in t4026 that "git add -p" works at all when color is enabled. That would have caught this bug, as well as any others that are specific to the color code paths. - 73c727d69 (log --graph: customize the graph lines with config log.graphColors, 2017-01-19) added a test to t4202 that checks some "invalid" graph color config. Since ",, blue" before yielded only "blue" as valid, and now yields "empty, empty, blue", we don't match the expected output. One way to fix this would be to change the expectation to the empty color strings. But that makes the test much less interesting, since we show only two graph lines, both of which would be colorless. Since the empty-string case is now covered by t4026, let's remove them entirely here. They're just in the way of the primary thing the test is supposed to be checking. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- color.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'color.c') diff --git a/color.c b/color.c index 7bb4a96f8c..2925a819b2 100644 --- a/color.c +++ b/color.c @@ -212,8 +212,10 @@ int color_parse_mem(const char *value, int value_len, char *dst) len--; } - if (!len) - return -1; + if (!len) { + dst[0] = '\0'; + return 0; + } if (!strncasecmp(ptr, "reset", len)) { xsnprintf(dst, end - dst, GIT_COLOR_RESET); -- cgit v1.2.3