summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDonald Sharp <donaldsharp72@gmail.com>2023-04-21 07:10:36 -0400
committerGitHub <noreply@github.com>2023-04-21 07:10:36 -0400
commit4d83e3b1b8461fde3cb4659bb5e1aabcda3b0004 (patch)
tree308d95d695149884cfd552e961be5ab5f54906c7
parent16f8b0fb7f857b610651bc1f798c981986360c2d (diff)
parent88acb8a0f9202df153527eeedd35af822a992ffb (diff)
Merge pull request #13344 from opensourcerouting/fix/backport_8cb4892c0669d916557d693b878420faec8e6e2a
bgpd: [8.5] Fix lcom->str string length to correctly cover aliases
-rw-r--r--bgpd/bgp_lcommunity.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/bgpd/bgp_lcommunity.c b/bgpd/bgp_lcommunity.c
index e0cca50d89..223882ba05 100644
--- a/bgpd/bgp_lcommunity.c
+++ b/bgpd/bgp_lcommunity.c
@@ -211,12 +211,13 @@ static void set_lcommunity_string(struct lcommunity *lcom, bool make_json,
}
/* 1 space + lcom->size lcom strings + null terminator */
- size_t str_buf_sz = BUFSIZ;
+ size_t str_buf_sz = (LCOMMUNITY_STRLEN * lcom->size) + 2;
str_buf = XCALLOC(MTYPE_LCOMMUNITY_STR, str_buf_sz);
+ len = 0;
for (i = 0; i < lcom->size; i++) {
if (i > 0)
- strlcat(str_buf, " ", str_buf_sz);
+ len = strlcat(str_buf, " ", str_buf_sz);
pnt = lcom->val + (i * LCOMMUNITY_SIZE);
pnt = ptr_get_be32(pnt, &global);
@@ -229,11 +230,22 @@ static void set_lcommunity_string(struct lcommunity *lcom, bool make_json,
snprintf(lcsb, sizeof(lcsb), "%u:%u:%u", global, local1,
local2);
+ /*
+ * Aliases can cause havoc, if the alias length is greater
+ * than the LCOMMUNITY_STRLEN for a particular item
+ * then we need to realloc the memory associated
+ * with the string so that it can fit
+ */
const char *com2alias =
translate_alias ? bgp_community2alias(lcsb) : lcsb;
+ size_t individual_len = strlen(com2alias);
+ if (individual_len + len > str_buf_sz) {
+ str_buf_sz = individual_len + len + 1;
+ str_buf = XREALLOC(MTYPE_LCOMMUNITY_STR, str_buf,
+ str_buf_sz);
+ }
len = strlcat(str_buf, com2alias, str_buf_sz);
- assert((unsigned int)len < str_buf_sz);
if (make_json) {
json_string = json_object_new_string(com2alias);