Skip to content

Commit 4d83e3b

Browse files
authored
Merge pull request #13344 from opensourcerouting/fix/backport_8cb4892c0669d916557d693b878420faec8e6e2a
bgpd: [8.5] Fix lcom->str string length to correctly cover aliases
2 parents 16f8b0f + 88acb8a commit 4d83e3b

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

bgpd/bgp_lcommunity.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,13 @@ static void set_lcommunity_string(struct lcommunity *lcom, bool make_json,
211211
}
212212

213213
/* 1 space + lcom->size lcom strings + null terminator */
214-
size_t str_buf_sz = BUFSIZ;
214+
size_t str_buf_sz = (LCOMMUNITY_STRLEN * lcom->size) + 2;
215215
str_buf = XCALLOC(MTYPE_LCOMMUNITY_STR, str_buf_sz);
216216

217+
len = 0;
217218
for (i = 0; i < lcom->size; i++) {
218219
if (i > 0)
219-
strlcat(str_buf, " ", str_buf_sz);
220+
len = strlcat(str_buf, " ", str_buf_sz);
220221

221222
pnt = lcom->val + (i * LCOMMUNITY_SIZE);
222223
pnt = ptr_get_be32(pnt, &global);
@@ -229,11 +230,22 @@ static void set_lcommunity_string(struct lcommunity *lcom, bool make_json,
229230
snprintf(lcsb, sizeof(lcsb), "%u:%u:%u", global, local1,
230231
local2);
231232

233+
/*
234+
* Aliases can cause havoc, if the alias length is greater
235+
* than the LCOMMUNITY_STRLEN for a particular item
236+
* then we need to realloc the memory associated
237+
* with the string so that it can fit
238+
*/
232239
const char *com2alias =
233240
translate_alias ? bgp_community2alias(lcsb) : lcsb;
241+
size_t individual_len = strlen(com2alias);
242+
if (individual_len + len > str_buf_sz) {
243+
str_buf_sz = individual_len + len + 1;
244+
str_buf = XREALLOC(MTYPE_LCOMMUNITY_STR, str_buf,
245+
str_buf_sz);
246+
}
234247

235248
len = strlcat(str_buf, com2alias, str_buf_sz);
236-
assert((unsigned int)len < str_buf_sz);
237249

238250
if (make_json) {
239251
json_string = json_object_new_string(com2alias);

0 commit comments

Comments
 (0)