res_prometheus: Clone containers before iterating

The channels, bridges and endpoints scrape functions were
grabbing their respective global containers, getting the
count of entries, allocating metric arrays based on
that count, then iterating over the container.  If the
global container had new objects added after the count
was taken and the metric arrays were allocated, we'd run
out of metric entries and attempt to write past the end
of the arrays.

Now each of the scape functions clone their respective
global containers and all operations are done on the
clone.  Since the clone is stable between getting the
count and iterating over it, we can't run past the end
of the metrics array.

ASTERISK-29130
Reported-By: Francisco Correia
Reported-By: BJ Weschke
Reported-By: Sébastien Duthil

Change-Id: If0c8e40853bc0e9429f2ba9c7f5f358d90c311af
This commit is contained in:
George Joseph
2021-04-01 07:39:03 -06:00
committed by Friendly Automation
parent a9a9864478
commit 19eef2a6dc
3 changed files with 31 additions and 5 deletions

View File

@@ -96,6 +96,7 @@ struct endpoint_metric_defs {
*/
static void endpoints_scrape_cb(struct ast_str **response)
{
struct ao2_container *endpoint_cache;
struct ao2_container *endpoints;
struct ao2_iterator it_endpoints;
struct stasis_message *message;
@@ -111,10 +112,16 @@ static void endpoints_scrape_cb(struct ast_str **response)
ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
endpoints = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type());
endpoint_cache = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type());
if (!endpoint_cache) {
return;
}
endpoints = ao2_container_clone(endpoint_cache, 0);
ao2_ref(endpoint_cache, -1);
if (!endpoints) {
return;
}
num_endpoints = ao2_container_count(endpoints);
/* Current endpoint count */