Skip to content

Commit

Permalink
bgpd: fix memory leaks in bgp_alias2community_str
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Ryzhov <[email protected]>
  • Loading branch information
idryzhov committed Aug 11, 2021
1 parent 8545712 commit 15bc6a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
24 changes: 16 additions & 8 deletions bgpd/bgp_clist.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ static bool community_regexp_include(regex_t *reg, struct community *com, int i)
static bool community_regexp_match(struct community *com, regex_t *reg)
{
const char *str;
char *regstr;
int rv;

/* When there is no communities attribute it is treated as empty
string. */
Expand All @@ -557,12 +559,14 @@ static bool community_regexp_match(struct community *com, regex_t *reg)
else
str = community_str(com, false);

regstr = bgp_alias2community_str(str);

/* Regular expression match. */
if (regexec(reg, bgp_alias2community_str(str), 0, NULL, 0) == 0)
return true;
rv = regexec(reg, regstr, 0, NULL, 0);

/* No match. */
return false;
XFREE(MTYPE_TMP, regstr);

return rv == 0;
}

static char *lcommunity_str_get(struct lcommunity *lcom, int i)
Expand Down Expand Up @@ -619,6 +623,8 @@ static bool lcommunity_regexp_include(regex_t *reg, struct lcommunity *lcom,
static bool lcommunity_regexp_match(struct lcommunity *com, regex_t *reg)
{
const char *str;
char *regstr;
int rv;

/* When there is no communities attribute it is treated as empty
string. */
Expand All @@ -627,12 +633,14 @@ static bool lcommunity_regexp_match(struct lcommunity *com, regex_t *reg)
else
str = lcommunity_str(com, false);

regstr = bgp_alias2community_str(str);

/* Regular expression match. */
if (regexec(reg, bgp_alias2community_str(str), 0, NULL, 0) == 0)
return true;
rv = regexec(reg, regstr, 0, NULL, 0);

/* No match. */
return false;
XFREE(MTYPE_TMP, regstr);

return rv == 0;
}


Expand Down
19 changes: 11 additions & 8 deletions bgpd/bgp_community_alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,25 @@ const char *bgp_alias2community(char *alias)
* This is a helper to convert already aliased version
* of communities into numerical-only format.
*/
const char *bgp_alias2community_str(const char *str)
char *bgp_alias2community_str(const char *str)
{
char **aliases;
int num;
char *comstr;
int num, i;

frrstr_split(str, " ", &aliases, &num);
const char *communities[num + 1];
const char *communities[num];

for (int i = 0; i < num; i++) {
communities[i] =
XSTRDUP(MTYPE_TMP, bgp_alias2community(aliases[i]));
for (i = 0; i < num; i++)
communities[i] = bgp_alias2community(aliases[i]);

comstr = frrstr_join(communities, num, " ");

for (i = 0; i < num; i++)
XFREE(MTYPE_TMP, aliases[i]);
}
XFREE(MTYPE_TMP, aliases);

return frrstr_join(communities, num, " ");
return comstr;
}

static int bgp_community_alias_vector_walker(struct hash_bucket *bucket,
Expand Down
2 changes: 1 addition & 1 deletion bgpd/bgp_community_alias.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extern void bgp_ca_alias_delete(struct community_alias *ca);
extern int bgp_community_alias_write(struct vty *vty);
extern const char *bgp_community2alias(char *community);
extern const char *bgp_alias2community(char *alias);
extern const char *bgp_alias2community_str(const char *str);
extern char *bgp_alias2community_str(const char *str);
extern void bgp_community_alias_command_completion_setup(void);

#endif /* FRR_BGP_COMMUNITY_ALIAS_H */

0 comments on commit 15bc6a4

Please sign in to comment.