From 394908e21822c91afc89a4a57b6eb6337cfb104b Mon Sep 17 00:00:00 2001 From: Alistair King Date: Tue, 12 Feb 2019 11:13:12 -0800 Subject: [PATCH] Better handling of really long AS paths 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. --- src/_pybgpstream_bgpelem.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/_pybgpstream_bgpelem.c b/src/_pybgpstream_bgpelem.c index ff20d40..8e19f32 100644 --- a/src/_pybgpstream_bgpelem.c +++ b/src/_pybgpstream_bgpelem.c @@ -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)