]> git.puffer.fish Git - matthieu/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)
committerDonatas Abraitis <donatas.abraitis@gmail.com>
Mon, 16 Aug 2021 14:28:39 +0000 (17:28 +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 879001cde7e2f548368845deebe21d049ac7698e..aaf3e408c396a19daaa4cdaf42f9cf456ad50e21 100644 (file)
@@ -175,20 +175,23 @@ 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;
 }
index 418134e4013d18aa11259ef78022c4925963b842..66782651019b5eb7864a9142d2dbd81487e04c54 100644 (file)
@@ -43,6 +43,6 @@ 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);
 
 #endif /* FRR_BGP_COMMUNITY_ALIAS_H */