Skip to content

Commit f0062d3

Browse files
telezhnayagitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent deec6b8 commit f0062d3

File tree

1 file changed

+54
-42
lines changed

1 file changed

+54
-42
lines changed

ref-filter.c

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_
875875
if (deref)
876876
name++;
877877
if (!strcmp(name, "objecttype"))
878-
v->s = type_name(oi->type);
878+
v->s = xstrdup(type_name(oi->type));
879879
else if (!strcmp(name, "objectsize")) {
880880
v->value = oi->size;
881881
v->s = xstrfmt("%lu", oi->size);
@@ -899,9 +899,9 @@ static void grab_tag_values(struct atom_value *val, int deref, struct object *ob
899899
if (deref)
900900
name++;
901901
if (!strcmp(name, "tag"))
902-
v->s = tag->tag;
902+
v->s = xstrdup(tag->tag);
903903
else if (!strcmp(name, "type") && tag->tagged)
904-
v->s = type_name(tag->tagged->type);
904+
v->s = xstrdup(type_name(tag->tagged->type));
905905
else if (!strcmp(name, "object") && tag->tagged)
906906
v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
907907
}
@@ -1032,7 +1032,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
10321032
v->value = timestamp;
10331033
return;
10341034
bad:
1035-
v->s = "";
1035+
v->s = xstrdup("");
10361036
v->value = 0;
10371037
}
10381038

@@ -1227,7 +1227,7 @@ static void fill_missing_values(struct atom_value *val)
12271227
for (i = 0; i < used_atom_cnt; i++) {
12281228
struct atom_value *v = &val[i];
12291229
if (v->s == NULL)
1230-
v->s = "";
1230+
v->s = xstrdup("");
12311231
}
12321232
}
12331233

@@ -1273,7 +1273,8 @@ static inline char *copy_advance(char *dst, const char *src)
12731273
static const char *lstrip_ref_components(const char *refname, int len)
12741274
{
12751275
long remaining = len;
1276-
const char *start = refname;
1276+
const char *start = xstrdup(refname);
1277+
const char *to_free = start;
12771278

12781279
if (len < 0) {
12791280
int i;
@@ -1294,20 +1295,24 @@ static const char *lstrip_ref_components(const char *refname, int len)
12941295
while (remaining > 0) {
12951296
switch (*start++) {
12961297
case '\0':
1297-
return "";
1298+
free((char *)to_free);
1299+
return xstrdup("");
12981300
case '/':
12991301
remaining--;
13001302
break;
13011303
}
13021304
}
13031305

1306+
start = xstrdup(start);
1307+
free((char *)to_free);
13041308
return start;
13051309
}
13061310

13071311
static const char *rstrip_ref_components(const char *refname, int len)
13081312
{
13091313
long remaining = len;
1310-
char *start = xstrdup(refname);
1314+
const char *start = xstrdup(refname);
1315+
const char *to_free = start;
13111316

13121317
if (len < 0) {
13131318
int i;
@@ -1327,9 +1332,10 @@ static const char *rstrip_ref_components(const char *refname, int len)
13271332

13281333
while (remaining-- > 0) {
13291334
char *p = strrchr(start, '/');
1330-
if (p == NULL)
1331-
return "";
1332-
else
1335+
if (p == NULL) {
1336+
free((char *)to_free);
1337+
return xstrdup("");
1338+
} else
13331339
p[0] = '\0';
13341340
}
13351341
return start;
@@ -1344,7 +1350,7 @@ static const char *show_ref(struct refname_atom *atom, const char *refname)
13441350
else if (atom->option == R_RSTRIP)
13451351
return rstrip_ref_components(refname, atom->rstrip);
13461352
else
1347-
return refname;
1353+
return xstrdup(refname);
13481354
}
13491355

13501356
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,
13581364
NULL, AHEAD_BEHIND_FULL) < 0) {
13591365
*s = xstrdup(msgs.gone);
13601366
} else if (!num_ours && !num_theirs)
1361-
*s = "";
1367+
*s = xstrdup("");
13621368
else if (!num_ours)
13631369
*s = xstrfmt(msgs.behind, num_theirs);
13641370
else if (!num_theirs)
@@ -1373,36 +1379,31 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
13731379
}
13741380
} else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
13751381
if (stat_tracking_info(branch, &num_ours, &num_theirs,
1376-
NULL, AHEAD_BEHIND_FULL) < 0)
1382+
NULL, AHEAD_BEHIND_FULL) < 0) {
1383+
*s = xstrdup("");
13771384
return;
1378-
1385+
}
13791386
if (!num_ours && !num_theirs)
1380-
*s = "=";
1387+
*s = xstrdup("=");
13811388
else if (!num_ours)
1382-
*s = "<";
1389+
*s = xstrdup("<");
13831390
else if (!num_theirs)
1384-
*s = ">";
1391+
*s = xstrdup(">");
13851392
else
1386-
*s = "<>";
1393+
*s = xstrdup("<>");
13871394
} else if (atom->u.remote_ref.option == RR_REMOTE_NAME) {
13881395
int explicit;
13891396
const char *remote = atom->u.remote_ref.push ?
13901397
pushremote_for_branch(branch, &explicit) :
13911398
remote_for_branch(branch, &explicit);
1392-
if (explicit)
1393-
*s = xstrdup(remote);
1394-
else
1395-
*s = "";
1399+
*s = xstrdup(explicit ? remote : "");
13961400
} else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
13971401
int explicit;
13981402
const char *merge;
13991403

14001404
merge = remote_ref_for_branch(branch, atom->u.remote_ref.push,
14011405
&explicit);
1402-
if (explicit)
1403-
*s = xstrdup(merge);
1404-
else
1405-
*s = "";
1406+
*s = xstrdup(explicit ? merge : "");
14061407
} else
14071408
BUG("unhandled RR_* enum");
14081409
}
@@ -1451,7 +1452,7 @@ char *get_head_description(void)
14511452
static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref)
14521453
{
14531454
if (!ref->symref)
1454-
return "";
1455+
return xstrdup("");
14551456
else
14561457
return show_ref(&atom->u.refname, ref->symref);
14571458
}
@@ -1510,7 +1511,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
15101511
ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
15111512
NULL, NULL);
15121513
if (!ref->symref)
1513-
ref->symref = "";
1514+
ref->symref = xstrdup("");
15141515
}
15151516

15161517
/* Fill in specials first */
@@ -1536,20 +1537,23 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
15361537
refname = get_symref(atom, ref);
15371538
else if (starts_with(name, "upstream")) {
15381539
const char *branch_name;
1539-
v->s = "";
15401540
/* only local branches may have an upstream */
15411541
if (!skip_prefix(ref->refname, "refs/heads/",
1542-
&branch_name))
1542+
&branch_name)) {
1543+
v->s = xstrdup("");
15431544
continue;
1545+
}
15441546
branch = branch_get(branch_name);
15451547

15461548
refname = branch_get_upstream(branch, NULL);
15471549
if (refname)
15481550
fill_remote_ref_details(atom, refname, branch, &v->s);
1551+
else
1552+
v->s = xstrdup("");
15491553
continue;
15501554
} else if (atom->u.remote_ref.push) {
15511555
const char *branch_name;
1552-
v->s = "";
1556+
v->s = xstrdup("");
15531557
if (!skip_prefix(ref->refname, "refs/heads/",
15541558
&branch_name))
15551559
continue;
@@ -1562,10 +1566,12 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
15621566
if (!refname)
15631567
continue;
15641568
}
1569+
/* We will definitely re-init v->s on the next line. */
1570+
free((char *)v->s);
15651571
fill_remote_ref_details(atom, refname, branch, &v->s);
15661572
continue;
15671573
} else if (starts_with(name, "color:")) {
1568-
v->s = atom->u.color;
1574+
v->s = xstrdup(atom->u.color);
15691575
continue;
15701576
} else if (!strcmp(name, "flag")) {
15711577
char buf[256], *cp = buf;
@@ -1574,7 +1580,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
15741580
if (ref->flag & REF_ISPACKED)
15751581
cp = copy_advance(cp, ",packed");
15761582
if (cp == buf)
1577-
v->s = "";
1583+
v->s = xstrdup("");
15781584
else {
15791585
*cp = '\0';
15801586
v->s = xstrdup(buf + 1);
@@ -1584,40 +1590,42 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
15841590
continue;
15851591
} else if (!strcmp(name, "HEAD")) {
15861592
if (atom->u.head && !strcmp(ref->refname, atom->u.head))
1587-
v->s = "*";
1593+
v->s = xstrdup("*");
15881594
else
1589-
v->s = " ";
1595+
v->s = xstrdup(" ");
15901596
continue;
15911597
} else if (starts_with(name, "align")) {
15921598
v->handler = align_atom_handler;
1593-
v->s = "";
1599+
v->s = xstrdup("");
15941600
continue;
15951601
} else if (!strcmp(name, "end")) {
15961602
v->handler = end_atom_handler;
1597-
v->s = "";
1603+
v->s = xstrdup("");
15981604
continue;
15991605
} else if (starts_with(name, "if")) {
16001606
const char *s;
1601-
v->s = "";
16021607
if (skip_prefix(name, "if:", &s))
16031608
v->s = xstrdup(s);
1609+
else
1610+
v->s = xstrdup("");
16041611
v->handler = if_atom_handler;
16051612
continue;
16061613
} else if (!strcmp(name, "then")) {
16071614
v->handler = then_atom_handler;
1608-
v->s = "";
1615+
v->s = xstrdup("");
16091616
continue;
16101617
} else if (!strcmp(name, "else")) {
16111618
v->handler = else_atom_handler;
1612-
v->s = "";
1619+
v->s = xstrdup("");
16131620
continue;
16141621
} else
16151622
continue;
16161623

16171624
if (!deref)
1618-
v->s = refname;
1625+
v->s = xstrdup(refname);
16191626
else
16201627
v->s = xstrfmt("%s^{}", refname);
1628+
free((char *)refname);
16211629
}
16221630

16231631
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,
19881996
static void free_array_item(struct ref_array_item *item)
19891997
{
19901998
free((char *)item->symref);
1999+
if (item->value) {
2000+
free((char *)item->value->s);
2001+
free(item->value);
2002+
}
19912003
free(item);
19922004
}
19932005

0 commit comments

Comments
 (0)