Skip to content

Commit

Permalink
Better handling of really long AS paths
Browse files Browse the repository at this point in the history
Previously an AS path with more than ~400 hops would cause a crash when trying create an AS path string. This adds dynamic memory allocation as a fall back in such cases.
  • Loading branch information
alistairking committed Feb 12, 2019
1 parent bc7535f commit 394908e
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/_pybgpstream_bgpelem.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,25 @@ static PyObject *get_pfx_pystr(bgpstream_pfx_t *pfx)

static PyObject *get_aspath_pystr(bgpstream_as_path_t *aspath)
{
// assuming 10 char per ASN, then this will hold >400 hops
// assuming 10 char per ASN, then this will hold >400 hops, if we
// see a longer AS path we resort to dynamically allocating the
// string
char buf[4096] = "";
if (bgpstream_as_path_snprintf(buf, 4096, aspath) >= 4096)
return NULL;
return PYSTR_FROMSTR(buf);
PyObject *pystr = NULL;
int len = 0;
if ((len = bgpstream_as_path_snprintf(buf, sizeof(buf), aspath)) >=
sizeof(buf)) {
char *bufp = NULL;
if ((bufp = malloc(len*2)) == NULL ||
bgpstream_as_path_snprintf(bufp, len*2, aspath) >= len*2) {
return NULL;
}
pystr = PYSTR_FROMSTR(bufp);
free(bufp);
} else {
pystr = PYSTR_FROMSTR(buf);
}
return pystr;
}

static PyObject *get_communities_pyset(bgpstream_community_set_t *communities)
Expand Down

0 comments on commit 394908e

Please sign in to comment.