Skip to content

Commit

Permalink
Use xmemdupz() in many places.
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Pierre Habouzit authored and Junio C Hamano committed Sep 19, 2007
1 parent 39bd2eb commit 182af83
Show file tree
Hide file tree
Showing 23 changed files with 60 additions and 197 deletions.
7 changes: 1 addition & 6 deletions attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,7 @@ static const char *parse_attr(const char *src, int lineno, const char *cp,
else if (!equals)
e->setto = ATTR__TRUE;
else {
char *value;
int vallen = ep - equals;
value = xmalloc(vallen);
memcpy(value, equals+1, vallen-1);
value[vallen-1] = 0;
e->setto = value;
e->setto = xmemdupz(equals + 1, ep - equals - 1);
}
e->attr = git_attr(cp, len);
}
Expand Down
8 changes: 2 additions & 6 deletions builtin-add.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,8 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec,
baselen = common_prefix(pathspec);
path = ".";
base = "";
if (baselen) {
char *common = xmalloc(baselen + 1);
memcpy(common, *pathspec, baselen);
common[baselen] = 0;
path = base = common;
}
if (baselen)
path = base = xmemdupz(*pathspec, baselen);

/* Read the directory and prune it */
read_directory(dir, path, base, baselen, pathspec);
Expand Down
11 changes: 2 additions & 9 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,7 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
return def;
}

name = xmalloc(len + 1);
memcpy(name, start, len);
name[len] = 0;
free(def);
return name;
return xmemdupz(start, len);
}

static int count_slashes(const char *cp)
Expand Down Expand Up @@ -687,10 +683,7 @@ static char *git_header_name(char *line, int llen)
break;
}
if (second[len] == '\n' && !memcmp(name, second, len)) {
char *ret = xmalloc(len + 1);
memcpy(ret, name, len);
ret[len] = 0;
return ret;
return xmemdupz(name, len);
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions builtin-fetch--tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,15 @@ static char *find_local_name(const char *remote_name, const char *refs,
}
if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
const char *local_part = ref + len + 1;
char *ret;
int retlen;

if (!next)
retlen = strlen(local_part);
else
retlen = next - local_part;
ret = xmalloc(retlen + 1);
memcpy(ret, local_part, retlen);
ret[retlen] = 0;
*force_p = single_force;
*not_for_merge_p = not_for_merge;
return ret;
return xmemdupz(local_part, retlen);
}
ref = next;
}
Expand Down
17 changes: 6 additions & 11 deletions builtin-fmt-merge-msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,10 @@ static int handle_line(char *line)
if (!strcmp(".", src) || !strcmp(src, origin)) {
int len = strlen(origin);
if (origin[0] == '\'' && origin[len - 1] == '\'') {
char *new_origin = xmalloc(len - 1);
memcpy(new_origin, origin + 1, len - 2);
new_origin[len - 2] = 0;
origin = new_origin;
} else
origin = xmemdupz(origin + 1, len - 2);
} else {
origin = xstrdup(origin);
}
} else {
char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
sprintf(new_origin, "%s of %s", origin, src);
Expand Down Expand Up @@ -211,14 +209,11 @@ static void shortlog(const char *name, unsigned char *sha1,

bol += 2;
eol = strchr(bol, '\n');

if (eol) {
int len = eol - bol;
oneline = xmalloc(len + 1);
memcpy(oneline, bol, len);
oneline[len] = 0;
} else
oneline = xmemdupz(bol, eol - bol);
} else {
oneline = xstrdup(bol);
}
append_to_list(&subjects, oneline, NULL);
}

Expand Down
40 changes: 9 additions & 31 deletions builtin-for-each-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ static int used_atom_cnt, sort_atom_limit, need_tagged;
static int parse_atom(const char *atom, const char *ep)
{
const char *sp;
char *n;
int i, at;

sp = atom;
Expand Down Expand Up @@ -120,10 +119,7 @@ static int parse_atom(const char *atom, const char *ep)
(sizeof *used_atom) * used_atom_cnt);
used_atom_type = xrealloc(used_atom_type,
(sizeof(*used_atom_type) * used_atom_cnt));
n = xmalloc(ep - atom + 1);
memcpy(n, atom, ep - atom);
n[ep-atom] = 0;
used_atom[at] = n;
used_atom[at] = xmemdupz(atom, ep - atom);
used_atom_type[at] = valid_atom[i].cmp_type;
return at;
}
Expand Down Expand Up @@ -305,46 +301,28 @@ static const char *find_wholine(const char *who, int wholen, const char *buf, un
static const char *copy_line(const char *buf)
{
const char *eol = strchr(buf, '\n');
char *line;
int len;
if (!eol)
return "";
len = eol - buf;
line = xmalloc(len + 1);
memcpy(line, buf, len);
line[len] = 0;
return line;
return xmemdupz(buf, eol - buf);
}

static const char *copy_name(const char *buf)
{
const char *eol = strchr(buf, '\n');
const char *eoname = strstr(buf, " <");
char *line;
int len;
if (!(eoname && eol && eoname < eol))
return "";
len = eoname - buf;
line = xmalloc(len + 1);
memcpy(line, buf, len);
line[len] = 0;
return line;
const char *cp;
for (cp = buf; *cp != '\n'; cp++) {
if (!strncmp(cp, " <", 2))
return xmemdupz(buf, cp - buf);
}
return "";
}

static const char *copy_email(const char *buf)
{
const char *email = strchr(buf, '<');
const char *eoemail = strchr(email, '>');
char *line;
int len;
if (!email || !eoemail)
return "";
eoemail++;
len = eoemail - email;
line = xmalloc(len + 1);
memcpy(line, email, len);
line[len] = 0;
return line;
return xmemdupz(email, eoemail + 1 - email);
}

static void grab_date(const char *buf, struct atom_value *v)
Expand Down
12 changes: 2 additions & 10 deletions builtin-log.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,6 @@ static const char *clean_message_id(const char *msg_id)
{
char ch;
const char *a, *z, *m;
char *n;
size_t len;

m = msg_id;
while ((ch = *m) && (isspace(ch) || (ch == '<')))
Expand All @@ -458,11 +456,7 @@ static const char *clean_message_id(const char *msg_id)
die("insane in-reply-to: %s", msg_id);
if (++z == m)
return a;
len = z - a;
n = xmalloc(len + 1);
memcpy(n, a, len);
n[len] = 0;
return n;
return xmemdupz(a, z - a);
}

int cmd_format_patch(int argc, const char **argv, const char *prefix)
Expand Down Expand Up @@ -541,9 +535,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
endpos = strchr(committer, '>');
if (!endpos)
die("bogos committer info %s\n", committer);
add_signoff = xmalloc(endpos - committer + 2);
memcpy(add_signoff, committer, endpos - committer + 1);
add_signoff[endpos - committer + 1] = 0;
add_signoff = xmemdupz(committer, endpos - committer + 1);
}
else if (!strcmp(argv[i], "--attach")) {
rev.mime_boundary = git_version_string;
Expand Down
9 changes: 1 addition & 8 deletions builtin-ls-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ static void prune_cache(const char *prefix)
static const char *verify_pathspec(const char *prefix)
{
const char **p, *n, *prev;
char *real_prefix;
unsigned long max;

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

real_prefix = NULL;
prefix_len = max;
if (max) {
real_prefix = xmalloc(max + 1);
memcpy(real_prefix, prev, max);
real_prefix[max] = 0;
}
return real_prefix;
return max ? xmemdupz(prev, max) : NULL;
}

/*
Expand Down
5 changes: 1 addition & 4 deletions builtin-mv.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ static const char **copy_pathspec(const char *prefix, const char **pathspec,
for (i = 0; i < count; i++) {
int length = strlen(result[i]);
if (length > 0 && result[i][length - 1] == '/') {
char *without_slash = xmalloc(length);
memcpy(without_slash, result[i], length - 1);
without_slash[length - 1] = '\0';
result[i] = without_slash;
result[i] = xmemdupz(result[i], length - 1);
}
if (base_name) {
const char *last_slash = strrchr(result[i], '/');
Expand Down
4 changes: 1 addition & 3 deletions builtin-revert.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ static void set_author_ident_env(const char *message)
char *line, *pend, *email, *timestamp;

p += 7;
line = xmalloc(eol + 1 - p);
memcpy(line, p, eol - p);
line[eol - p] = '\0';
line = xmemdupz(p, eol - p);
email = strchr(line, '<');
if (!email)
die ("Could not extract author email from %s",
Expand Down
11 changes: 2 additions & 9 deletions builtin-shortlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ static void insert_author_oneline(struct path_list *list,
while (authorlen > 0 && isspace(author[authorlen - 1]))
authorlen--;

buffer = xmalloc(authorlen + 1);
memcpy(buffer, author, authorlen);
buffer[authorlen] = '\0';

buffer = xmemdupz(author, authorlen);
item = path_list_insert(buffer, list);
if (item->util == NULL)
item->util = xcalloc(1, sizeof(struct path_list));
Expand All @@ -66,13 +63,9 @@ static void insert_author_oneline(struct path_list *list,
oneline++;
onelinelen--;
}

while (onelinelen > 0 && isspace(oneline[onelinelen - 1]))
onelinelen--;

buffer = xmalloc(onelinelen + 1);
memcpy(buffer, oneline, onelinelen);
buffer[onelinelen] = '\0';
buffer = xmemdupz(oneline, onelinelen);

if (dot3) {
int dot3len = strlen(dot3);
Expand Down
16 changes: 6 additions & 10 deletions commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,11 +628,7 @@ static char *get_header(const struct commit *commit, const char *key)
if (eol - line > key_len &&
!strncmp(line, key, key_len) &&
line[key_len] == ' ') {
int len = eol - line - key_len;
char *ret = xmalloc(len);
memcpy(ret, line + key_len + 1, len - 1);
ret[len - 1] = '\0';
return ret;
return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
}
line = next;
}
Expand Down Expand Up @@ -709,7 +705,7 @@ static void fill_person(struct interp *table, const char *msg, int len)
start = end + 1;
while (end > 0 && isspace(msg[end - 1]))
end--;
table[0].value = xstrndup(msg, end);
table[0].value = xmemdupz(msg, end);

if (start >= len)
return;
Expand All @@ -721,7 +717,7 @@ static void fill_person(struct interp *table, const char *msg, int len)
if (end >= len)
return;

table[1].value = xstrndup(msg + start, end - start);
table[1].value = xmemdupz(msg + start, end - start);

/* parse date */
for (start = end + 1; start < len && isspace(msg[start]); start++)
Expand All @@ -732,7 +728,7 @@ static void fill_person(struct interp *table, const char *msg, int len)
if (msg + start == ep)
return;

table[5].value = xstrndup(msg + start, ep - (msg + start));
table[5].value = xmemdupz(msg + start, ep - (msg + start));

/* parse tz */
for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
Expand Down Expand Up @@ -859,7 +855,7 @@ void format_commit_message(const struct commit *commit,
; /* do nothing */

if (state == SUBJECT) {
table[ISUBJECT].value = xstrndup(msg + i, eol - i);
table[ISUBJECT].value = xmemdupz(msg + i, eol - i);
i = eol;
}
if (i == eol) {
Expand All @@ -875,7 +871,7 @@ void format_commit_message(const struct commit *commit,
msg + i + 10, eol - i - 10);
else if (!prefixcmp(msg + i, "encoding "))
table[IENCODING].value =
xstrndup(msg + i + 9, eol - i - 9);
xmemdupz(msg + i + 9, eol - i - 9);
i = eol;
}
if (msg[i])
Expand Down
4 changes: 1 addition & 3 deletions connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,7 @@ static int git_proxy_command_options(const char *var, const char *value)
if (matchlen == 4 &&
!memcmp(value, "none", 4))
matchlen = 0;
git_proxy_command = xmalloc(matchlen + 1);
memcpy(git_proxy_command, value, matchlen);
git_proxy_command[matchlen] = 0;
git_proxy_command = xmemdupz(value, matchlen);
}
return 0;
}
Expand Down
7 changes: 1 addition & 6 deletions convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,8 @@ static int read_convert_config(const char *var, const char *value)
if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
break;
if (!drv) {
char *namebuf;
drv = xcalloc(1, sizeof(struct convert_driver));
namebuf = xmalloc(namelen + 1);
memcpy(namebuf, name, namelen);
namebuf[namelen] = 0;
drv->name = namebuf;
drv->next = NULL;
drv->name = xmemdupz(name, namelen);
*user_convert_tail = drv;
user_convert_tail = &(drv->next);
}
Expand Down
Loading

0 comments on commit 182af83

Please sign in to comment.