]> git.scripts.mit.edu Git - git.git/commitdiff
mailmap: handle mailmap blobs without trailing newlines
authorJeff King <peff@peff.net>
Wed, 28 Aug 2013 01:41:39 +0000 (21:41 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 28 Aug 2013 19:33:32 +0000 (12:33 -0700)
The read_mailmap_buf function reads each line of the mailmap
using strchrnul, like:

    const char *end = strchrnul(buf, '\n');
    unsigned long linelen = end - buf + 1;

But that's off-by-one when we actually hit the NUL byte; our
line does not have a terminator, and so is only "end - buf"
bytes long. As a result, when we subtract the linelen from
the total len, we end up with (unsigned long)-1 bytes left
in the buffer, and we start reading random junk from memory.

We could fix it with:

    unsigned long linelen = end - buf + !!*end;

but let's take a step back for a moment. It's questionable
in the first place for a function that takes a buffer and
length to be using strchrnul. But it works because we only
have one caller (and are only likely to ever have this one),
which is handing us data from read_sha1_file. Which means
that it's always NUL-terminated.

Instead of tightening the assumptions to make the
buffer/length pair work for a caller that doesn't actually
exist, let's let loosen the assumptions to what the real
caller has: a modifiable, NUL-terminated string.

This makes the code simpler and shorter (because we don't
have to correlate strchrnul with the length calculation),
correct (because the code with the off-by-one just goes
away), and more efficient (we can drop the extra allocation
we needed to create NUL-terminated strings for each line,
and just terminate in place).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
mailmap.c
t/t4203-mailmap.sh

index 2a7b36628cb5623eb0ffb95bfb54e52a6dd300f4..d2f28b0f35f497510498252130438fc3127e9f7f 100644 (file)
--- a/mailmap.c
+++ b/mailmap.c
@@ -187,20 +187,17 @@ static int read_mailmap_file(struct string_list *map, const char *filename,
        return 0;
 }
 
-static void read_mailmap_buf(struct string_list *map,
-                            const char *buf, unsigned long len,
-                            char **repo_abbrev)
+static void read_mailmap_string(struct string_list *map, char *buf,
+                               char **repo_abbrev)
 {
-       while (len) {
-               const char *end = strchrnul(buf, '\n');
-               unsigned long linelen = end - buf + 1;
-               char *line = xmemdupz(buf, linelen);
+       while (*buf) {
+               char *end = strchrnul(buf, '\n');
 
-               read_mailmap_line(map, line, repo_abbrev);
+               if (*end)
+                       *end++ = '\0';
 
-               free(line);
-               buf += linelen;
-               len -= linelen;
+               read_mailmap_line(map, buf, repo_abbrev);
+               buf = end;
        }
 }
 
@@ -224,7 +221,7 @@ static int read_mailmap_blob(struct string_list *map,
        if (type != OBJ_BLOB)
                return error("mailmap is not a blob: %s", name);
 
-       read_mailmap_buf(map, buf, size, repo_abbrev);
+       read_mailmap_string(map, buf, repo_abbrev);
 
        free(buf);
        return 0;
index 842b7549ec3ed6b1fa268a96d085048d2f0b7793..d5a8ff3f882be670099b382f85cc3bce2eb57fba 100755 (executable)
@@ -159,7 +159,8 @@ test_expect_success 'setup mailmap blob tests' '
        Blob Guy <author@example.com>
        Blob Guy <bugs@company.xx>
        EOF
-       git add just-bugs both &&
+       printf "Tricky Guy <author@example.com>" >no-newline &&
+       git add just-bugs both no-newline &&
        git commit -m "my mailmaps" &&
        echo "Repo Guy <author@example.com>" >.mailmap &&
        echo "Internal Guy <author@example.com>" >internal.map
@@ -243,6 +244,19 @@ test_expect_success 'mailmap.blob defaults to HEAD:.mailmap in bare repo' '
        )
 '
 
+test_expect_success 'mailmap.blob can handle blobs without trailing newline' '
+       cat >expect <<-\EOF &&
+       Tricky Guy (1):
+             initial
+
+       nick1 (1):
+             second
+
+       EOF
+       git -c mailmap.blob=map:no-newline shortlog HEAD >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'cleanup after mailmap.blob tests' '
        rm -f .mailmap
 '