]> git.puffer.fish Git - matthieu/frr.git/commitdiff
bgpd: XREALLOC handles NULL properly
authorDonald Sharp <sharpd@nvidia.com>
Mon, 12 Jul 2021 23:32:42 +0000 (19:32 -0400)
committerDonald Sharp <sharpd@nvidia.com>
Mon, 12 Jul 2021 23:32:42 +0000 (19:32 -0400)
the realloc man page:

If ptr is NULL, then the call is equivalent to malloc(size)

This should be sufficient for our needs to not have to have
XMALLOC and XREALLOC

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
bgpd/bgp_attr.c
bgpd/bgp_community.c
bgpd/bgp_ecommunity.c
bgpd/bgp_lcommunity.c
bgpd/bgpd.c

index 24b48178d2ca63f7de3b3cd1453b194f5e36cbf9..adf408220e036c3aec05c63f3c4adbbb76dd2cc8 100644 (file)
@@ -2915,11 +2915,8 @@ static bgp_attr_parse_ret_t bgp_attr_unknown(struct bgp_attr_parser_args *args)
        if (!transit)
                transit = XCALLOC(MTYPE_TRANSIT, sizeof(struct transit));
 
-       if (transit->val)
-               transit->val = XREALLOC(MTYPE_TRANSIT_VAL, transit->val,
-                                       transit->length + total);
-       else
-               transit->val = XMALLOC(MTYPE_TRANSIT_VAL, total);
+       transit->val = XREALLOC(MTYPE_TRANSIT_VAL, transit->val,
+                               transit->length + total);
 
        memcpy(transit->val + transit->length, startp, total);
        transit->length += total;
index 2aa6a56a5e16dd0a7e229d9b3a6a38bb7cfcfeab..e91166449a1fb5a0e6bf9bad4113f42269f90e04 100644 (file)
@@ -60,11 +60,7 @@ void community_free(struct community **com)
 void community_add_val(struct community *com, uint32_t val)
 {
        com->size++;
-       if (com->val)
-               com->val = XREALLOC(MTYPE_COMMUNITY_VAL, com->val,
-                                   com_length(com));
-       else
-               com->val = XMALLOC(MTYPE_COMMUNITY_VAL, com_length(com));
+       com->val = XREALLOC(MTYPE_COMMUNITY_VAL, com->val, com_length(com));
 
        val = htonl(val);
        memcpy(com_lastval(com), &val, sizeof(uint32_t));
@@ -618,13 +614,8 @@ bool community_cmp(const struct community *com1, const struct community *com2)
 struct community *community_merge(struct community *com1,
                                  struct community *com2)
 {
-       if (com1->val)
-               com1->val =
-                       XREALLOC(MTYPE_COMMUNITY_VAL, com1->val,
-                                (com1->size + com2->size) * COMMUNITY_SIZE);
-       else
-               com1->val = XMALLOC(MTYPE_COMMUNITY_VAL,
-                                   (com1->size + com2->size) * COMMUNITY_SIZE);
+       com1->val = XREALLOC(MTYPE_COMMUNITY_VAL, com1->val,
+                            (com1->size + com2->size) * COMMUNITY_SIZE);
 
        memcpy(com1->val + com1->size, com2->val, com2->size * COMMUNITY_SIZE);
        com1->size += com2->size;
index 923c9b0d7e875b7fe6bdf63ebb2c4e863ffd08b0..3a951e6e80c2ab1a42140086d7a02ef740542d2e 100644 (file)
@@ -158,7 +158,6 @@ static bool ecommunity_add_val_internal(struct ecommunity *ecom,
        ecom->val = XREALLOC(MTYPE_ECOMMUNITY_VAL, ecom->val,
                         ecom_length_size(ecom, ecom_size));
 
-
        memmove(ecom->val + ((ins_idx + 1) * ecom_size),
                ecom->val + (ins_idx * ecom_size),
                (ecom->size - 1 - ins_idx) * ecom_size);
@@ -287,14 +286,9 @@ char *ecommunity_str(struct ecommunity *ecom)
 struct ecommunity *ecommunity_merge(struct ecommunity *ecom1,
                                    struct ecommunity *ecom2)
 {
-       if (ecom1->val)
-               ecom1->val = XREALLOC(MTYPE_ECOMMUNITY_VAL, ecom1->val,
-                                     (size_t)(ecom1->size + ecom2->size)
-                                             * (size_t)ecom1->unit_size);
-       else
-               ecom1->val = XMALLOC(MTYPE_ECOMMUNITY_VAL,
-                                    (size_t)(ecom1->size + ecom2->size)
-                                            * (size_t)ecom1->unit_size);
+       ecom1->val = XREALLOC(MTYPE_ECOMMUNITY_VAL, ecom1->val,
+                             (size_t)(ecom1->size + ecom2->size)
+                                     * (size_t)ecom1->unit_size);
 
        memcpy(ecom1->val + (ecom1->size * ecom1->unit_size), ecom2->val,
               (size_t)ecom2->size * (size_t)ecom1->unit_size);
index fa4d4aee289f28fd3ccac2f7fb1bc005fcab5581..6121c4905f7ae71cee02d07397c203b6b1c001d6 100644 (file)
@@ -166,12 +166,8 @@ struct lcommunity *lcommunity_dup(struct lcommunity *lcom)
 struct lcommunity *lcommunity_merge(struct lcommunity *lcom1,
                                    struct lcommunity *lcom2)
 {
-       if (lcom1->val)
-               lcom1->val = XREALLOC(MTYPE_LCOMMUNITY_VAL, lcom1->val,
-                                     lcom_length(lcom1) + lcom_length(lcom2));
-       else
-               lcom1->val = XMALLOC(MTYPE_LCOMMUNITY_VAL,
-                                    lcom_length(lcom1) + lcom_length(lcom2));
+       lcom1->val = XREALLOC(MTYPE_LCOMMUNITY_VAL, lcom1->val,
+                             lcom_length(lcom1) + lcom_length(lcom2));
 
        memcpy(lcom1->val + lcom_length(lcom1), lcom2->val, lcom_length(lcom2));
        lcom1->size += lcom2->size;
index acade16ef27fb8cebf0de2e67def58ec6621731f..89acf9cad250cd9d3a96bfcc888ac09994d92c61 100644 (file)
@@ -656,14 +656,9 @@ int bgp_confederation_peers_add(struct bgp *bgp, as_t as)
        if (bgp_confederation_peers_check(bgp, as))
                return -1;
 
-       if (bgp->confed_peers)
-               bgp->confed_peers =
-                       XREALLOC(MTYPE_BGP_CONFED_LIST, bgp->confed_peers,
-                                (bgp->confed_peers_cnt + 1) * sizeof(as_t));
-       else
-               bgp->confed_peers =
-                       XMALLOC(MTYPE_BGP_CONFED_LIST,
-                               (bgp->confed_peers_cnt + 1) * sizeof(as_t));
+       bgp->confed_peers =
+               XREALLOC(MTYPE_BGP_CONFED_LIST, bgp->confed_peers,
+                        (bgp->confed_peers_cnt + 1) * sizeof(as_t));
 
        bgp->confed_peers[bgp->confed_peers_cnt] = as;
        bgp->confed_peers_cnt++;