From af785cf2df7f6c7ceacfaac572c298b0f294c588 Mon Sep 17 00:00:00 2001 From: anlan_cs Date: Sun, 1 May 2022 08:33:01 -0400 Subject: [PATCH] bgpd: delay "stream_new" in str2prefix_rd() `stream_new` in `str2prefix_rd()` can be called after some checkings are passed, so the last `if (s)` in this fuction will make sense. Additionally some changes for `str2prefix_rd()`: 1) Use `RD_BYTES` instead of hard number for `stream_new()` 2) Remove unnecessary `lret` variable Signed-off-by: anlan_cs --- bgpd/bgp_rd.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/bgpd/bgp_rd.c b/bgpd/bgp_rd.c index 097ab9254e..9ab48aedc6 100644 --- a/bgpd/bgp_rd.c +++ b/bgpd/bgp_rd.c @@ -99,20 +99,16 @@ void decode_rd_vnc_eth(const uint8_t *pnt, struct rd_vnc_eth *rd_vnc_eth) int str2prefix_rd(const char *str, struct prefix_rd *prd) { - int ret; /* ret of called functions */ - int lret; /* local ret, of this func */ + int ret = 0; char *p; char *p2; struct stream *s = NULL; char *half = NULL; struct in_addr addr; - s = stream_new(8); - prd->family = AF_UNSPEC; prd->prefixlen = 64; - lret = 0; p = strchr(str, ':'); if (!p) goto out; @@ -120,6 +116,8 @@ int str2prefix_rd(const char *str, struct prefix_rd *prd) if (!all_digit(p + 1)) goto out; + s = stream_new(RD_BYTES); + half = XMALLOC(MTYPE_TMP, (p - str) + 1); memcpy(half, str, (p - str)); half[p - str] = '\0'; @@ -143,8 +141,7 @@ int str2prefix_rd(const char *str, struct prefix_rd *prd) stream_putl(s, atol(p + 1)); } } else { - ret = inet_aton(half, &addr); - if (!ret) + if (!inet_aton(half, &addr)) goto out; stream_putw(s, RD_TYPE_IP); @@ -152,13 +149,13 @@ int str2prefix_rd(const char *str, struct prefix_rd *prd) stream_putw(s, atol(p + 1)); } memcpy(prd->val, s->data, 8); - lret = 1; + ret = 1; out: if (s) stream_free(s); XFREE(MTYPE_TMP, half); - return lret; + return ret; } char *prefix_rd2str(const struct prefix_rd *prd, char *buf, size_t size)