git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH next] don't let mailmap provoke use of freed memory

From
Jim Meyering <jim@meyering.net>
Date
Oct 11, 2010, 15:41 UTC
Message-ID
<87tyksd9er.fsf@meyering.net>
On an x86_64 system (F13-based), I ran these commands in an empty directory:
    git init
    printf '%s\n' \
      '<jdoe@example.com> <jdoe@example.COM>' \
      'John <jdoe@example.com>' > .mailmap
    git shortlog < /dev/null
Here's the result:
    (reading log message from standard input)
    *** glibc detected *** git: free(): invalid pointer: 0x0000000000f53730 ***
    ======= Backtrace: =========
    /lib64/libc.so.6[0x31ba875676]
    git[0x48c2a5]
    git[0x4b9858]
    ...
    zsh: abort (core dumped)  git shortlog
What happened?

Some .mailmap entry is of the <email1> <email2> form, while a subsequent one looks like "User Name <Email2>, and the two email addresses on the right are not identical but are "equal" when using a case-insensitive comparator.

Then, when add_mapping is processing the latter line, new_email is NULL and we free me->email, yet do not replace it with a new strdup'd string. Thus, when later we attempt to use the buffer behind that ->email pointer, we reference freed memory.

The solution is to free ->email and ->name only if we're about to replace them.
Signed-off-by: Jim Meyering <meyering@redhat.com>
---
 mailmap.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/mailmap.c b/mailmap.c
index f80b701..02fcfde 100644
--- a/mailmap.c
+++ b/mailmap.c
@@ -79,12 +79,14 @@ static void add_mapping(struct string_list *map,
 	if (old_name == NULL) {
 		debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email, index);
 		/* Replace current name and new email for simple entry */
-		free(me->name);
-		free(me->email);
-		if (new_name)
+		if (new_name) {
+			free(me->name);
 			me->name = xstrdup(new_name);
-		if (new_email)
+		}
+		if (new_email) {
+			free(me->email);
 			me->email = xstrdup(new_email);
+		}
 	} else {
 		struct mailmap_info *mi = xmalloc(sizeof(struct mailmap_info));
 		debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
--
1.7.3.1.104.gc752e
Next: Jonathan Nieder
Message 1 of 9 in “don't let mailmap provoke use of freed memory”
  1. don't let mailmap provoke use of freed memoryJim Meyering, Oct 11, 2010
  2. Jonathan NiederOct 11, 2010
  3. Ævar Arnfjörð BjarmasonOct 15, 2010
  4. Jonathan NiederOct 15, 2010
  5. Jonathan NiederOct 15, 2010
  6. t4203 (mailmap): stop hardcoding commit ids and datesJonathan Nieder, Oct 15, 2010
  7. Junio C HamanoOct 17, 2010
  8. Junio C HamanoOct 20, 2010
  9. Junio C HamanoOct 20, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.