From 15bc6a40d3bf86bec574244ce89150a73774dd80 Mon Sep 17 00:00:00 2001 From: Igor Ryzhov Date: Wed, 11 Aug 2021 12:09:15 +0300 Subject: [PATCH] bgpd: fix memory leaks in bgp_alias2community_str Signed-off-by: Igor Ryzhov --- bgpd/bgp_clist.c | 24 ++++++++++++++++-------- bgpd/bgp_community_alias.c | 19 +++++++++++-------- bgpd/bgp_community_alias.h | 2 +- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/bgpd/bgp_clist.c b/bgpd/bgp_clist.c index fd8f51fed379..8b38f4dfa083 100644 --- a/bgpd/bgp_clist.c +++ b/bgpd/bgp_clist.c @@ -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. */ @@ -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) @@ -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. */ @@ -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; } diff --git a/bgpd/bgp_community_alias.c b/bgpd/bgp_community_alias.c index 5f45e19a3b73..793f3ac9ac1c 100644 --- a/bgpd/bgp_community_alias.c +++ b/bgpd/bgp_community_alias.c @@ -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, diff --git a/bgpd/bgp_community_alias.h b/bgpd/bgp_community_alias.h index fc9eb9f9e4ba..57cde0ad4454 100644 --- a/bgpd/bgp_community_alias.h +++ b/bgpd/bgp_community_alias.h @@ -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 */