]> git.proxmox.com Git - mirror_frr.git/commitdiff
bgpd: fix memory leaks in bgp_alias2community_str
authorIgor Ryzhov <iryzhov@nfware.com>
Wed, 11 Aug 2021 09:09:15 +0000 (12:09 +0300)
committerIgor Ryzhov <iryzhov@nfware.com>
Wed, 11 Aug 2021 09:09:15 +0000 (12:09 +0300)
Signed-off-by: Igor Ryzhov <iryzhov@nfware.com>
bgpd/bgp_clist.c
bgpd/bgp_community_alias.c
bgpd/bgp_community_alias.h

index fd8f51fed379262cab928050d6546794855defa0..8b38f4dfa08309f7c69db10efc06edd2500ea8d6 100644 (file)
@@ -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;
 }
 
 
index 5f45e19a3b73d4ca35e9771e674f2979b432fae2..793f3ac9ac1cf3bfaf5fa43c9b98b48af5c6a211 100644 (file)
@@ -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,
index fc9eb9f9e4bacf69a2abf5a7a8ff022819eaa54b..57cde0ad4454461eccdcf3e04b9a84c01b2e0ce7 100644 (file)
@@ -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 */