builtin-shortlog.c: use string_list_append(), and don't strdup unnecessarily
Make insert_one_record() use string_list_append(), instead of duplicating its code. Because of this, do not free the "util" member when clearing the "onelines" string lists: with the new code path it is not initialized to any value (was being initialized to NULL previously). Also, avoid unnecessary strdup() calls when inserting names in log->list. This list always has "strdup_strings" activated, hence strdup'ing namebuf is unnecessary. This change also removes a latent memory leak in the old code. NB: The duplicated code mentioned above predated the appearance of string_list_append(). Signed-off-by: Adeodato Simó <dato@net.com.org.es> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Adeodato Simó committed
Dec 30, 2008 at 22:01 UTC
fe73fc1abe785a423fcc51b753d57ffa1485edab
1 file changed
+3
-16
builtin-shortlog.c
+3
-16
@@ -36,7 +36,6 @@ static void insert_one_record(struct shortlog *log,
36
const char *dot3 = log->common_repo_prefix;
37
char *buffer, *p;
38
struct string_list_item *item;
39
- struct string_list *onelines;
39
char namebuf[1024];
40
size_t len;
41
const char *eol;
@@ -68,12 +67,9 @@ static void insert_one_record(struct shortlog *log,
67
snprintf(namebuf + len, room, " %.*s", maillen, boemail);
68
}
69
71
- buffer = xstrdup(namebuf);
72
- item = string_list_insert(buffer, &log->list);
70
+ item = string_list_insert(namebuf, &log->list);
71
if (item->util == NULL)
72
item->util = xcalloc(1, sizeof(struct string_list));
75
- else
76
- free(buffer);
73
74
/* Skip any leading whitespace, including any blank lines. */
75
while (*oneline && isspace(*oneline))
@@ -104,16 +100,7 @@ static void insert_one_record(struct shortlog *log,
100
}
101
}
102
107
- onelines = item->util;
108
- if (onelines->nr >= onelines->alloc) {
109
- onelines->alloc = alloc_nr(onelines->nr);
110
- onelines->items = xrealloc(onelines->items,
111
- onelines->alloc
112
- * sizeof(struct string_list_item));
113
- }
114
-
115
- onelines->items[onelines->nr].util = NULL;
116
- onelines->items[onelines->nr++].string = buffer;
103
+ string_list_append(buffer, item->util);
104
}
105
106
static void read_from_stdin(struct shortlog *log)
@@ -323,7 +310,7 @@ void shortlog_output(struct shortlog *log)
310
}
311
312
onelines->strdup_strings = 1;
326
- string_list_clear(onelines, 1);
313
+ string_list_clear(onelines, 0);
314
free(onelines);
315
log->list.items[i].util = NULL;
316
}