]> git.ipfire.org Git - thirdparty/git.git/commitdiff
utf8.c: fix strbuf_utf8_replace() consuming data beyond input string
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sun, 10 Aug 2014 07:05:21 +0000 (14:05 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 11 Aug 2014 18:52:22 +0000 (11:52 -0700)
The main loop in strbuf_utf8_replace() could summed up as:

  while ('src' is still valid) {
    1) advance 'src' to copy ANSI escape sequences
    2) advance 'src' to copy/replace visible characters
  }

The problem is after #1, 'src' may have reached the end of the string
(so 'src' points to NUL) and #2 will continue to copy that NUL as if
it's a normal character. Because the output is stored in a strbuf,
this NUL accounted in the 'len' field as well. Check after #1 and
break the loop if necessary.

The test does not look obvious, but the combination of %>>() should
make a call trace like this

  show_log()
  pretty_print_commit()
  format_commit_message()
  strbuf_expand()
  format_commit_item()
  format_and_pad_commit()
  strbuf_utf8_replace()

where %C(auto)%d would insert a color reset escape sequence in the end
of the string given to strbuf_utf8_replace() and show_log() uses
fwrite() to send everything to stdout (including the incorrect NUL
inserted by strbuf_utf8_replace)

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t4205-log-pretty-formats.sh
utf8.c

index c84ec9ae6139be752831365244f5580e46138884..a2f70f6cb37cd1151569de6e5ce2b3973eb5fa69 100755 (executable)
@@ -431,6 +431,13 @@ EOF
        test_cmp expected actual
 '
 
+test_expect_success 'strbuf_utf8_replace() not producing NUL' '
+       git log --color --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)%C(auto)%d" |
+               test_decode_color |
+               nul_to_q >actual &&
+       ! grep Q actual
+'
+
 # get new digests (with no abbreviations)
 head1=$(git rev-parse --verify HEAD~0) &&
 head2=$(git rev-parse --verify HEAD~1) &&
diff --git a/utf8.c b/utf8.c
index 77c28d492cccfcbcb8a302d168a81b21d909ef12..fe35e2f833eb88e092b94a145f55de6500d7cbad 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -444,6 +444,9 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
                        dst += n;
                }
 
+               if (src >= end)
+                       break;
+
                old = src;
                n = utf8_width((const char**)&src, NULL);
                if (!src)       /* broken utf-8, do nothing */