threads / patch / 28952

patchmailmap: xcalloc mailmap_info

Subject: [PATCH] mailmap: xcalloc mailmap_info

## tl;dr

3 messages between Nov 16, 2011 and Nov 17, 2011. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Marc-André Lureau· Nov 16, 2011, 23:51 UTC · lore
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(-)
Show changes to mailmap.c +1 −1
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· Nov 17, 2011, 01:10 UTC · re: Marc-André Lureau · lore

Re: [PATCH] mailmap: xcalloc mailmap_info

Marc-André Lureau <marcandre.lureau@gmail.com> writes:
Show 5 quoted lines
> 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.

Show 16 quoted lines
>  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· Nov 17, 2011, 01:25 UTC · re: Junio C Hamano · lore
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(-)
Show changes to mailmap.c +2 −3
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

← back to recent threads