1
0
mirror of https://github.com/git/git synced 2024-07-05 00:58:49 +00:00

ref-filter: free item->value and item->value->s

Release item->value.
Initialize item->value->s dynamically and then release its resources.
Release some local variables.

Final goal of this patch is to reduce number of memory leaks.

Signed-off-by: Olga Telezhnaia <olyatelezhnaya@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Olga Telezhnaya 2018-10-18 07:28:54 +00:00 committed by Junio C Hamano
parent deec6b8e0f
commit f0062d3b74

View File

@ -875,7 +875,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_
if (deref) if (deref)
name++; name++;
if (!strcmp(name, "objecttype")) if (!strcmp(name, "objecttype"))
v->s = type_name(oi->type); v->s = xstrdup(type_name(oi->type));
else if (!strcmp(name, "objectsize")) { else if (!strcmp(name, "objectsize")) {
v->value = oi->size; v->value = oi->size;
v->s = xstrfmt("%lu", oi->size); v->s = xstrfmt("%lu", oi->size);
@ -899,9 +899,9 @@ static void grab_tag_values(struct atom_value *val, int deref, struct object *ob
if (deref) if (deref)
name++; name++;
if (!strcmp(name, "tag")) if (!strcmp(name, "tag"))
v->s = tag->tag; v->s = xstrdup(tag->tag);
else if (!strcmp(name, "type") && tag->tagged) else if (!strcmp(name, "type") && tag->tagged)
v->s = type_name(tag->tagged->type); v->s = xstrdup(type_name(tag->tagged->type));
else if (!strcmp(name, "object") && tag->tagged) else if (!strcmp(name, "object") && tag->tagged)
v->s = xstrdup(oid_to_hex(&tag->tagged->oid)); v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
} }
@ -1032,7 +1032,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
v->value = timestamp; v->value = timestamp;
return; return;
bad: bad:
v->s = ""; v->s = xstrdup("");
v->value = 0; v->value = 0;
} }
@ -1227,7 +1227,7 @@ static void fill_missing_values(struct atom_value *val)
for (i = 0; i < used_atom_cnt; i++) { for (i = 0; i < used_atom_cnt; i++) {
struct atom_value *v = &val[i]; struct atom_value *v = &val[i];
if (v->s == NULL) if (v->s == NULL)
v->s = ""; v->s = xstrdup("");
} }
} }
@ -1273,7 +1273,8 @@ static inline char *copy_advance(char *dst, const char *src)
static const char *lstrip_ref_components(const char *refname, int len) static const char *lstrip_ref_components(const char *refname, int len)
{ {
long remaining = len; long remaining = len;
const char *start = refname; const char *start = xstrdup(refname);
const char *to_free = start;
if (len < 0) { if (len < 0) {
int i; int i;
@ -1294,20 +1295,24 @@ static const char *lstrip_ref_components(const char *refname, int len)
while (remaining > 0) { while (remaining > 0) {
switch (*start++) { switch (*start++) {
case '\0': case '\0':
return ""; free((char *)to_free);
return xstrdup("");
case '/': case '/':
remaining--; remaining--;
break; break;
} }
} }
start = xstrdup(start);
free((char *)to_free);
return start; return start;
} }
static const char *rstrip_ref_components(const char *refname, int len) static const char *rstrip_ref_components(const char *refname, int len)
{ {
long remaining = len; long remaining = len;
char *start = xstrdup(refname); const char *start = xstrdup(refname);
const char *to_free = start;
if (len < 0) { if (len < 0) {
int i; int i;
@ -1327,9 +1332,10 @@ static const char *rstrip_ref_components(const char *refname, int len)
while (remaining-- > 0) { while (remaining-- > 0) {
char *p = strrchr(start, '/'); char *p = strrchr(start, '/');
if (p == NULL) if (p == NULL) {
return ""; free((char *)to_free);
else return xstrdup("");
} else
p[0] = '\0'; p[0] = '\0';
} }
return start; return start;
@ -1344,7 +1350,7 @@ static const char *show_ref(struct refname_atom *atom, const char *refname)
else if (atom->option == R_RSTRIP) else if (atom->option == R_RSTRIP)
return rstrip_ref_components(refname, atom->rstrip); return rstrip_ref_components(refname, atom->rstrip);
else else
return refname; return xstrdup(refname);
} }
static void fill_remote_ref_details(struct used_atom *atom, const char *refname, static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
@ -1358,7 +1364,7 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
NULL, AHEAD_BEHIND_FULL) < 0) { NULL, AHEAD_BEHIND_FULL) < 0) {
*s = xstrdup(msgs.gone); *s = xstrdup(msgs.gone);
} else if (!num_ours && !num_theirs) } else if (!num_ours && !num_theirs)
*s = ""; *s = xstrdup("");
else if (!num_ours) else if (!num_ours)
*s = xstrfmt(msgs.behind, num_theirs); *s = xstrfmt(msgs.behind, num_theirs);
else if (!num_theirs) else if (!num_theirs)
@ -1373,36 +1379,31 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
} }
} else if (atom->u.remote_ref.option == RR_TRACKSHORT) { } else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
if (stat_tracking_info(branch, &num_ours, &num_theirs, if (stat_tracking_info(branch, &num_ours, &num_theirs,
NULL, AHEAD_BEHIND_FULL) < 0) NULL, AHEAD_BEHIND_FULL) < 0) {
*s = xstrdup("");
return; return;
}
if (!num_ours && !num_theirs) if (!num_ours && !num_theirs)
*s = "="; *s = xstrdup("=");
else if (!num_ours) else if (!num_ours)
*s = "<"; *s = xstrdup("<");
else if (!num_theirs) else if (!num_theirs)
*s = ">"; *s = xstrdup(">");
else else
*s = "<>"; *s = xstrdup("<>");
} else if (atom->u.remote_ref.option == RR_REMOTE_NAME) { } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) {
int explicit; int explicit;
const char *remote = atom->u.remote_ref.push ? const char *remote = atom->u.remote_ref.push ?
pushremote_for_branch(branch, &explicit) : pushremote_for_branch(branch, &explicit) :
remote_for_branch(branch, &explicit); remote_for_branch(branch, &explicit);
if (explicit) *s = xstrdup(explicit ? remote : "");
*s = xstrdup(remote);
else
*s = "";
} else if (atom->u.remote_ref.option == RR_REMOTE_REF) { } else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
int explicit; int explicit;
const char *merge; const char *merge;
merge = remote_ref_for_branch(branch, atom->u.remote_ref.push, merge = remote_ref_for_branch(branch, atom->u.remote_ref.push,
&explicit); &explicit);
if (explicit) *s = xstrdup(explicit ? merge : "");
*s = xstrdup(merge);
else
*s = "";
} else } else
BUG("unhandled RR_* enum"); BUG("unhandled RR_* enum");
} }
@ -1451,7 +1452,7 @@ char *get_head_description(void)
static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref) static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref)
{ {
if (!ref->symref) if (!ref->symref)
return ""; return xstrdup("");
else else
return show_ref(&atom->u.refname, ref->symref); return show_ref(&atom->u.refname, ref->symref);
} }
@ -1510,7 +1511,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING, ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
NULL, NULL); NULL, NULL);
if (!ref->symref) if (!ref->symref)
ref->symref = ""; ref->symref = xstrdup("");
} }
/* Fill in specials first */ /* Fill in specials first */
@ -1536,20 +1537,23 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
refname = get_symref(atom, ref); refname = get_symref(atom, ref);
else if (starts_with(name, "upstream")) { else if (starts_with(name, "upstream")) {
const char *branch_name; const char *branch_name;
v->s = "";
/* only local branches may have an upstream */ /* only local branches may have an upstream */
if (!skip_prefix(ref->refname, "refs/heads/", if (!skip_prefix(ref->refname, "refs/heads/",
&branch_name)) &branch_name)) {
v->s = xstrdup("");
continue; continue;
}
branch = branch_get(branch_name); branch = branch_get(branch_name);
refname = branch_get_upstream(branch, NULL); refname = branch_get_upstream(branch, NULL);
if (refname) if (refname)
fill_remote_ref_details(atom, refname, branch, &v->s); fill_remote_ref_details(atom, refname, branch, &v->s);
else
v->s = xstrdup("");
continue; continue;
} else if (atom->u.remote_ref.push) { } else if (atom->u.remote_ref.push) {
const char *branch_name; const char *branch_name;
v->s = ""; v->s = xstrdup("");
if (!skip_prefix(ref->refname, "refs/heads/", if (!skip_prefix(ref->refname, "refs/heads/",
&branch_name)) &branch_name))
continue; continue;
@ -1562,10 +1566,12 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
if (!refname) if (!refname)
continue; continue;
} }
/* We will definitely re-init v->s on the next line. */
free((char *)v->s);
fill_remote_ref_details(atom, refname, branch, &v->s); fill_remote_ref_details(atom, refname, branch, &v->s);
continue; continue;
} else if (starts_with(name, "color:")) { } else if (starts_with(name, "color:")) {
v->s = atom->u.color; v->s = xstrdup(atom->u.color);
continue; continue;
} else if (!strcmp(name, "flag")) { } else if (!strcmp(name, "flag")) {
char buf[256], *cp = buf; char buf[256], *cp = buf;
@ -1574,7 +1580,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
if (ref->flag & REF_ISPACKED) if (ref->flag & REF_ISPACKED)
cp = copy_advance(cp, ",packed"); cp = copy_advance(cp, ",packed");
if (cp == buf) if (cp == buf)
v->s = ""; v->s = xstrdup("");
else { else {
*cp = '\0'; *cp = '\0';
v->s = xstrdup(buf + 1); v->s = xstrdup(buf + 1);
@ -1584,40 +1590,42 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
continue; continue;
} else if (!strcmp(name, "HEAD")) { } else if (!strcmp(name, "HEAD")) {
if (atom->u.head && !strcmp(ref->refname, atom->u.head)) if (atom->u.head && !strcmp(ref->refname, atom->u.head))
v->s = "*"; v->s = xstrdup("*");
else else
v->s = " "; v->s = xstrdup(" ");
continue; continue;
} else if (starts_with(name, "align")) { } else if (starts_with(name, "align")) {
v->handler = align_atom_handler; v->handler = align_atom_handler;
v->s = ""; v->s = xstrdup("");
continue; continue;
} else if (!strcmp(name, "end")) { } else if (!strcmp(name, "end")) {
v->handler = end_atom_handler; v->handler = end_atom_handler;
v->s = ""; v->s = xstrdup("");
continue; continue;
} else if (starts_with(name, "if")) { } else if (starts_with(name, "if")) {
const char *s; const char *s;
v->s = "";
if (skip_prefix(name, "if:", &s)) if (skip_prefix(name, "if:", &s))
v->s = xstrdup(s); v->s = xstrdup(s);
else
v->s = xstrdup("");
v->handler = if_atom_handler; v->handler = if_atom_handler;
continue; continue;
} else if (!strcmp(name, "then")) { } else if (!strcmp(name, "then")) {
v->handler = then_atom_handler; v->handler = then_atom_handler;
v->s = ""; v->s = xstrdup("");
continue; continue;
} else if (!strcmp(name, "else")) { } else if (!strcmp(name, "else")) {
v->handler = else_atom_handler; v->handler = else_atom_handler;
v->s = ""; v->s = xstrdup("");
continue; continue;
} else } else
continue; continue;
if (!deref) if (!deref)
v->s = refname; v->s = xstrdup(refname);
else else
v->s = xstrfmt("%s^{}", refname); v->s = xstrfmt("%s^{}", refname);
free((char *)refname);
} }
for (i = 0; i < used_atom_cnt; i++) { for (i = 0; i < used_atom_cnt; i++) {
@ -1988,6 +1996,10 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
static void free_array_item(struct ref_array_item *item) static void free_array_item(struct ref_array_item *item)
{ {
free((char *)item->symref); free((char *)item->symref);
if (item->value) {
free((char *)item->value->s);
free(item->value);
}
free(item); free(item);
} }