Skip to content

Commit 182af83

Browse files
MadCodergitster
authored andcommitted
Use xmemdupz() in many places.
Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 39bd2eb commit 182af83

23 files changed

+60
-197
lines changed

attr.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,7 @@ static const char *parse_attr(const char *src, int lineno, const char *cp,
160160
else if (!equals)
161161
e->setto = ATTR__TRUE;
162162
else {
163-
char *value;
164-
int vallen = ep - equals;
165-
value = xmalloc(vallen);
166-
memcpy(value, equals+1, vallen-1);
167-
value[vallen-1] = 0;
168-
e->setto = value;
163+
e->setto = xmemdupz(equals + 1, ep - equals - 1);
169164
}
170165
e->attr = git_attr(cp, len);
171166
}

builtin-add.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,8 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec,
7171
baselen = common_prefix(pathspec);
7272
path = ".";
7373
base = "";
74-
if (baselen) {
75-
char *common = xmalloc(baselen + 1);
76-
memcpy(common, *pathspec, baselen);
77-
common[baselen] = 0;
78-
path = base = common;
79-
}
74+
if (baselen)
75+
path = base = xmemdupz(*pathspec, baselen);
8076

8177
/* Read the directory and prune it */
8278
read_directory(dir, path, base, baselen, pathspec);

builtin-apply.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,7 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
293293
return def;
294294
}
295295

296-
name = xmalloc(len + 1);
297-
memcpy(name, start, len);
298-
name[len] = 0;
299-
free(def);
300-
return name;
296+
return xmemdupz(start, len);
301297
}
302298

303299
static int count_slashes(const char *cp)
@@ -687,10 +683,7 @@ static char *git_header_name(char *line, int llen)
687683
break;
688684
}
689685
if (second[len] == '\n' && !memcmp(name, second, len)) {
690-
char *ret = xmalloc(len + 1);
691-
memcpy(ret, name, len);
692-
ret[len] = 0;
693-
return ret;
686+
return xmemdupz(name, len);
694687
}
695688
}
696689
}

builtin-fetch--tool.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,19 +222,15 @@ static char *find_local_name(const char *remote_name, const char *refs,
222222
}
223223
if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
224224
const char *local_part = ref + len + 1;
225-
char *ret;
226225
int retlen;
227226

228227
if (!next)
229228
retlen = strlen(local_part);
230229
else
231230
retlen = next - local_part;
232-
ret = xmalloc(retlen + 1);
233-
memcpy(ret, local_part, retlen);
234-
ret[retlen] = 0;
235231
*force_p = single_force;
236232
*not_for_merge_p = not_for_merge;
237-
return ret;
233+
return xmemdupz(local_part, retlen);
238234
}
239235
ref = next;
240236
}

builtin-fmt-merge-msg.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,10 @@ static int handle_line(char *line)
140140
if (!strcmp(".", src) || !strcmp(src, origin)) {
141141
int len = strlen(origin);
142142
if (origin[0] == '\'' && origin[len - 1] == '\'') {
143-
char *new_origin = xmalloc(len - 1);
144-
memcpy(new_origin, origin + 1, len - 2);
145-
new_origin[len - 2] = 0;
146-
origin = new_origin;
147-
} else
143+
origin = xmemdupz(origin + 1, len - 2);
144+
} else {
148145
origin = xstrdup(origin);
146+
}
149147
} else {
150148
char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
151149
sprintf(new_origin, "%s of %s", origin, src);
@@ -211,14 +209,11 @@ static void shortlog(const char *name, unsigned char *sha1,
211209

212210
bol += 2;
213211
eol = strchr(bol, '\n');
214-
215212
if (eol) {
216-
int len = eol - bol;
217-
oneline = xmalloc(len + 1);
218-
memcpy(oneline, bol, len);
219-
oneline[len] = 0;
220-
} else
213+
oneline = xmemdupz(bol, eol - bol);
214+
} else {
221215
oneline = xstrdup(bol);
216+
}
222217
append_to_list(&subjects, oneline, NULL);
223218
}
224219

builtin-for-each-ref.c

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ static int used_atom_cnt, sort_atom_limit, need_tagged;
8787
static int parse_atom(const char *atom, const char *ep)
8888
{
8989
const char *sp;
90-
char *n;
9190
int i, at;
9291

9392
sp = atom;
@@ -120,10 +119,7 @@ static int parse_atom(const char *atom, const char *ep)
120119
(sizeof *used_atom) * used_atom_cnt);
121120
used_atom_type = xrealloc(used_atom_type,
122121
(sizeof(*used_atom_type) * used_atom_cnt));
123-
n = xmalloc(ep - atom + 1);
124-
memcpy(n, atom, ep - atom);
125-
n[ep-atom] = 0;
126-
used_atom[at] = n;
122+
used_atom[at] = xmemdupz(atom, ep - atom);
127123
used_atom_type[at] = valid_atom[i].cmp_type;
128124
return at;
129125
}
@@ -305,46 +301,28 @@ static const char *find_wholine(const char *who, int wholen, const char *buf, un
305301
static const char *copy_line(const char *buf)
306302
{
307303
const char *eol = strchr(buf, '\n');
308-
char *line;
309-
int len;
310304
if (!eol)
311305
return "";
312-
len = eol - buf;
313-
line = xmalloc(len + 1);
314-
memcpy(line, buf, len);
315-
line[len] = 0;
316-
return line;
306+
return xmemdupz(buf, eol - buf);
317307
}
318308

319309
static const char *copy_name(const char *buf)
320310
{
321-
const char *eol = strchr(buf, '\n');
322-
const char *eoname = strstr(buf, " <");
323-
char *line;
324-
int len;
325-
if (!(eoname && eol && eoname < eol))
326-
return "";
327-
len = eoname - buf;
328-
line = xmalloc(len + 1);
329-
memcpy(line, buf, len);
330-
line[len] = 0;
331-
return line;
311+
const char *cp;
312+
for (cp = buf; *cp != '\n'; cp++) {
313+
if (!strncmp(cp, " <", 2))
314+
return xmemdupz(buf, cp - buf);
315+
}
316+
return "";
332317
}
333318

334319
static const char *copy_email(const char *buf)
335320
{
336321
const char *email = strchr(buf, '<');
337322
const char *eoemail = strchr(email, '>');
338-
char *line;
339-
int len;
340323
if (!email || !eoemail)
341324
return "";
342-
eoemail++;
343-
len = eoemail - email;
344-
line = xmalloc(len + 1);
345-
memcpy(line, email, len);
346-
line[len] = 0;
347-
return line;
325+
return xmemdupz(email, eoemail + 1 - email);
348326
}
349327

350328
static void grab_date(const char *buf, struct atom_value *v)

builtin-log.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,6 @@ static const char *clean_message_id(const char *msg_id)
441441
{
442442
char ch;
443443
const char *a, *z, *m;
444-
char *n;
445-
size_t len;
446444

447445
m = msg_id;
448446
while ((ch = *m) && (isspace(ch) || (ch == '<')))
@@ -458,11 +456,7 @@ static const char *clean_message_id(const char *msg_id)
458456
die("insane in-reply-to: %s", msg_id);
459457
if (++z == m)
460458
return a;
461-
len = z - a;
462-
n = xmalloc(len + 1);
463-
memcpy(n, a, len);
464-
n[len] = 0;
465-
return n;
459+
return xmemdupz(a, z - a);
466460
}
467461

468462
int cmd_format_patch(int argc, const char **argv, const char *prefix)
@@ -541,9 +535,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
541535
endpos = strchr(committer, '>');
542536
if (!endpos)
543537
die("bogos committer info %s\n", committer);
544-
add_signoff = xmalloc(endpos - committer + 2);
545-
memcpy(add_signoff, committer, endpos - committer + 1);
546-
add_signoff[endpos - committer + 1] = 0;
538+
add_signoff = xmemdupz(committer, endpos - committer + 1);
547539
}
548540
else if (!strcmp(argv[i], "--attach")) {
549541
rev.mime_boundary = git_version_string;

builtin-ls-files.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ static void prune_cache(const char *prefix)
299299
static const char *verify_pathspec(const char *prefix)
300300
{
301301
const char **p, *n, *prev;
302-
char *real_prefix;
303302
unsigned long max;
304303

305304
prev = NULL;
@@ -326,14 +325,8 @@ static const char *verify_pathspec(const char *prefix)
326325
if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
327326
die("git-ls-files: cannot generate relative filenames containing '..'");
328327

329-
real_prefix = NULL;
330328
prefix_len = max;
331-
if (max) {
332-
real_prefix = xmalloc(max + 1);
333-
memcpy(real_prefix, prev, max);
334-
real_prefix[max] = 0;
335-
}
336-
return real_prefix;
329+
return max ? xmemdupz(prev, max) : NULL;
337330
}
338331

339332
/*

builtin-mv.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ static const char **copy_pathspec(const char *prefix, const char **pathspec,
2222
for (i = 0; i < count; i++) {
2323
int length = strlen(result[i]);
2424
if (length > 0 && result[i][length - 1] == '/') {
25-
char *without_slash = xmalloc(length);
26-
memcpy(without_slash, result[i], length - 1);
27-
without_slash[length - 1] = '\0';
28-
result[i] = without_slash;
25+
result[i] = xmemdupz(result[i], length - 1);
2926
}
3027
if (base_name) {
3128
const char *last_slash = strrchr(result[i], '/');

builtin-revert.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,7 @@ static void set_author_ident_env(const char *message)
168168
char *line, *pend, *email, *timestamp;
169169

170170
p += 7;
171-
line = xmalloc(eol + 1 - p);
172-
memcpy(line, p, eol - p);
173-
line[eol - p] = '\0';
171+
line = xmemdupz(p, eol - p);
174172
email = strchr(line, '<');
175173
if (!email)
176174
die ("Could not extract author email from %s",

0 commit comments

Comments
 (0)