<p>Friendly Automation <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/15724">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  Kevin Harwell: Looks good to me, but someone else must approve
  Sean Bright: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved
  Friendly Automation: Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_prometheus: Clone containers before iterating<br><br>The channels, bridges and endpoints scrape functions were<br>grabbing their respective global containers, getting the<br>count of entries, allocating metric arrays based on<br>that count, then iterating over the container.  If the<br>global container had new objects added after the count<br>was taken and the metric arrays were allocated, we'd run<br>out of metric entries and attempt to write past the end<br>of the arrays.<br><br>Now each of the scape functions clone their respective<br>global containers and all operations are done on the<br>clone.  Since the clone is stable between getting the<br>count and iterating over it, we can't run past the end<br>of the metrics array.<br><br>ASTERISK-29130<br>Reported-By: Francisco Correia<br>Reported-By: BJ Weschke<br>Reported-By: Sébastien Duthil<br><br>Change-Id: If0c8e40853bc0e9429f2ba9c7f5f358d90c311af<br>---<br>M res/prometheus/bridges.c<br>M res/prometheus/channels.c<br>M res/prometheus/endpoints.c<br>3 files changed, 31 insertions(+), 5 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/prometheus/bridges.c b/res/prometheus/bridges.c</span><br><span>index 47dee9a..0f09603 100644</span><br><span>--- a/res/prometheus/bridges.c</span><br><span>+++ b/res/prometheus/bridges.c</span><br><span>@@ -77,6 +77,7 @@</span><br><span>  */</span><br><span> static void bridges_scrape_cb(struct ast_str **response)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+    struct ao2_container *bridge_cache;</span><br><span>  struct ao2_container *bridges;</span><br><span>       struct ao2_iterator it_bridges;</span><br><span>      struct ast_bridge *bridge;</span><br><span>@@ -92,10 +93,17 @@</span><br><span> </span><br><span>         ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-     bridges = ast_bridges();</span><br><span style="color: hsl(120, 100%, 40%);">+      bridge_cache = ast_bridges();</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!bridge_cache) {</span><br><span style="color: hsl(120, 100%, 40%);">+          return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   bridges = ao2_container_clone(bridge_cache, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+       ao2_ref(bridge_cache, -1);</span><br><span>   if (!bridges) {</span><br><span>              return;</span><br><span>      }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  num_bridges = ao2_container_count(bridges);</span><br><span> </span><br><span>      /* Current endpoint count */</span><br><span>@@ -178,4 +186,4 @@</span><br><span>   prometheus_callback_register(&bridges_callback);</span><br><span> </span><br><span>     return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span>\ No newline at end of file</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span>diff --git a/res/prometheus/channels.c b/res/prometheus/channels.c</span><br><span>index 97b7519..930ae54 100644</span><br><span>--- a/res/prometheus/channels.c</span><br><span>+++ b/res/prometheus/channels.c</span><br><span>@@ -129,6 +129,7 @@</span><br><span>  */</span><br><span> static void channels_scrape_cb(struct ast_str **response)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+   struct ao2_container *channel_cache;</span><br><span>         struct ao2_container *channels;</span><br><span>      struct ao2_iterator it_chans;</span><br><span>        struct ast_channel_snapshot *snapshot;</span><br><span>@@ -145,7 +146,17 @@</span><br><span> </span><br><span>    ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-     channels = ast_channel_cache_all();</span><br><span style="color: hsl(120, 100%, 40%);">+   channel_cache = ast_channel_cache_all();</span><br><span style="color: hsl(120, 100%, 40%);">+      if (!channel_cache) {</span><br><span style="color: hsl(120, 100%, 40%);">+         return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   channels = ao2_container_clone(channel_cache, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+     ao2_ref(channel_cache, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+   if (!channels) {</span><br><span style="color: hsl(120, 100%, 40%);">+              return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  num_channels = ao2_container_count(channels);</span><br><span> </span><br><span>    /* Channel count */</span><br><span>@@ -233,4 +244,4 @@</span><br><span>    prometheus_callback_register(&channels_callback);</span><br><span> </span><br><span>    return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span>\ No newline at end of file</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span>diff --git a/res/prometheus/endpoints.c b/res/prometheus/endpoints.c</span><br><span>index 5f758c5..a575198 100644</span><br><span>--- a/res/prometheus/endpoints.c</span><br><span>+++ b/res/prometheus/endpoints.c</span><br><span>@@ -96,6 +96,7 @@</span><br><span>  */</span><br><span> static void endpoints_scrape_cb(struct ast_str **response)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+        struct ao2_container *endpoint_cache;</span><br><span>        struct ao2_container *endpoints;</span><br><span>     struct ao2_iterator it_endpoints;</span><br><span>    struct stasis_message *message;</span><br><span>@@ -111,10 +112,16 @@</span><br><span> </span><br><span>  ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-     endpoints = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type());</span><br><span style="color: hsl(120, 100%, 40%);">+    endpoint_cache = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type());</span><br><span style="color: hsl(120, 100%, 40%);">+       if (!endpoint_cache) {</span><br><span style="color: hsl(120, 100%, 40%);">+                return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+     endpoints = ao2_container_clone(endpoint_cache, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+   ao2_ref(endpoint_cache, -1);</span><br><span>         if (!endpoints) {</span><br><span>            return;</span><br><span>      }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  num_endpoints = ao2_container_count(endpoints);</span><br><span> </span><br><span>  /* Current endpoint count */</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/15724">change 15724</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/15724"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 18 </div>
<div style="display:none"> Gerrit-Change-Id: If0c8e40853bc0e9429f2ba9c7f5f358d90c311af </div>
<div style="display:none"> Gerrit-Change-Number: 15724 </div>
<div style="display:none"> Gerrit-PatchSet: 3 </div>
<div style="display:none"> Gerrit-Owner: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Sean Bright <sean.bright@gmail.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>