# [PATCH] mailmap: xcalloc mailmap_info

3 messages from 2011-11-16 to 2011-11-17. Participants: Marc-André Lureau, Junio C Hamano.
Thread: https://gitlist.dev/t/28952

## Marc-André Lureau, 2011-11-16 23:51

Subject: [PATCH] mailmap: xcalloc mailmap_info
Message-ID: <1321487473-29194-1-git-send-email-marcandre.lureau@gmail.com>
URL: https://gitlist.dev/e/1321487473-29194-1-git-send-email-marcandre.lureau%40gmail.com

```
This is to avoid reaching free of uninitialized members.

With an invalid .mailmap (and perhaps in other cases), it can reach
free(mi->name) with garbage for example.
---
 mailmap.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/mailmap.c b/mailmap.c
index 02fcfde..fbf7764 100644
--- a/mailmap.c
+++ b/mailmap.c
@@ -88,7 +88,7 @@ static void add_mapping(struct string_list *map,
 			me->email = xstrdup(new_email);
 		}
 	} else {
-		struct mailmap_info *mi = xmalloc(sizeof(struct mailmap_info));
+		struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
 		debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
 		if (new_name)
 			mi->name = xstrdup(new_name);
-- 
1.7.7

```

## Junio C Hamano, 2011-11-17 01:10

Subject: Re: [PATCH] mailmap: xcalloc mailmap_info
Message-ID: <7v8vnfpn9v.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v8vnfpn9v.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1321487473-29194-1-git-send-email-marcandre.lureau@gmail.com>

```
Marc-André Lureau <marcandre.lureau@gmail.com> writes:

> This is to avoid reaching free of uninitialized members.
>
> With an invalid .mailmap (and perhaps in other cases), it can reach
> free(mi->name) with garbage for example.
> ---

Sign-off?

Thanks. We might want to turn xmalloc() followed by memset(,0,) for the
allocation of the mailmap entry itself in the same function, but that is a
minor issue.

>  mailmap.c |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
>
> diff --git a/mailmap.c b/mailmap.c
> index 02fcfde..fbf7764 100644
> --- a/mailmap.c
> +++ b/mailmap.c
> @@ -88,7 +88,7 @@ static void add_mapping(struct string_list *map,
>  			me->email = xstrdup(new_email);
>  		}
>  	} else {
> -		struct mailmap_info *mi = xmalloc(sizeof(struct mailmap_info));
> +		struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
>  		debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
>  		if (new_name)
>  			mi->name = xstrdup(new_name);

```

## Marc-André Lureau, 2011-11-17 01:25

Subject: [PATCH] mailmap: xcalloc mailmap_info
Message-ID: <1321493106-6533-1-git-send-email-marcandre.lureau@gmail.com>
URL: https://gitlist.dev/e/1321493106-6533-1-git-send-email-marcandre.lureau%40gmail.com
In-Reply-To: <7v8vnfpn9v.fsf@alter.siamese.dyndns.org>

```
This is to avoid reaching free of uninitialized members.

With an invalid .mailmap (and perhaps in other cases), it can reach
free(mi->name) with garbage for example.

Signed-off-by: Marc-André Lureau <marcandre.lureau@gmail.com>
---
 mailmap.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/mailmap.c b/mailmap.c
index 02fcfde..8c3196c 100644
--- a/mailmap.c
+++ b/mailmap.c
@@ -70,8 +70,7 @@ static void add_mapping(struct string_list *map,
 	} else {
 		/* create mailmap entry */
 		struct string_list_item *item = string_list_insert_at_index(map, index, old_email);
-		item->util = xmalloc(sizeof(struct mailmap_entry));
-		memset(item->util, 0, sizeof(struct mailmap_entry));
+		item->util = xcalloc(1, sizeof(struct mailmap_entry));
 		((struct mailmap_entry *)item->util)->namemap.strdup_strings = 1;
 	}
 	me = (struct mailmap_entry *)map->items[index].util;
@@ -88,7 +87,7 @@ static void add_mapping(struct string_list *map,
 			me->email = xstrdup(new_email);
 		}
 	} else {
-		struct mailmap_info *mi = xmalloc(sizeof(struct mailmap_info));
+		struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
 		debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
 		if (new_name)
 			mi->name = xstrdup(new_name);
-- 
1.7.7

```
