From 34faa2e7ca95f8f7f4a235bb5b25fd604612f140 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Thu, 19 Apr 2012 10:34:11 +0800 Subject: stringlist: use seperately allocated string Allocate the string in string list seperately instead of embedding a zero length string into struct stringlist. Besides looking cleaner this allows us to implement a string_list_asprintf. Signed-off-by: Sascha Hauer Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD --- lib/stringlist.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'lib/stringlist.c') diff --git a/lib/stringlist.c b/lib/stringlist.c index a8ff97964f..c8b835ed09 100644 --- a/lib/stringlist.c +++ b/lib/stringlist.c @@ -16,9 +16,8 @@ int string_list_add(struct string_list *sl, char *str) { struct string_list *new; - new = xmalloc(sizeof(struct string_list) + strlen(str) + 1); - - strcpy(new->str, str); + new = xmalloc(sizeof(*new)); + new->str = xstrdup(str); list_add_tail(&new->list, &sl->list); @@ -29,9 +28,8 @@ int string_list_add_sorted(struct string_list *sl, char *str) { struct string_list *new; - new = xmalloc(sizeof(struct string_list) + strlen(str) + 1); - - strcpy(new->str, str); + new = xmalloc(sizeof(*new)); + new->str = xstrdup(str); list_add_sort(&new->list, &sl->list, string_list_compare); -- cgit v1.2.3