<p>Joshua Colp <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/14742">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Joshua Colp: Looks good to me, approved; Approved for Submit
Benjamin Keith Ford: Looks good to me, but someone else must approve
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">vector.h: Fix implementation of AST_VECTOR_COMPACT() for empty vectors<br><br>The assumed behavior of realloc() - that it was effectively a free() if<br>its second argument was 0 - is Linux specific behavior and is not<br>guaranteed by either POSIX or the C specification.<br><br>Instead, if we want to resize a vector to 0, do it explicitly.<br><br>Change-Id: Ife31d4b510ebab41cb5477fdc7ea4e3138ca8b4f<br>---<br>M include/asterisk/vector.h<br>1 file changed, 22 insertions(+), 18 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/vector.h b/include/asterisk/vector.h</span><br><span>index 8f5cf8c..00d701b 100644</span><br><span>--- a/include/asterisk/vector.h</span><br><span>+++ b/include/asterisk/vector.h</span><br><span>@@ -637,24 +637,28 @@</span><br><span> * \return 0 on success.</span><br><span> * \return Non-zero on failure.</span><br><span> */</span><br><span style="color: hsl(0, 100%, 40%);">-#define AST_VECTOR_COMPACT(vec) ({ \</span><br><span style="color: hsl(0, 100%, 40%);">- int res = 0; \</span><br><span style="color: hsl(0, 100%, 40%);">- do { \</span><br><span style="color: hsl(0, 100%, 40%);">- if ((vec)->max > (vec)->current) { \</span><br><span style="color: hsl(0, 100%, 40%);">- size_t new_max = (vec)->current; \</span><br><span style="color: hsl(0, 100%, 40%);">- typeof((vec)->elems) new_elems = ast_realloc( \</span><br><span style="color: hsl(0, 100%, 40%);">- (vec)->elems, \</span><br><span style="color: hsl(0, 100%, 40%);">- new_max * sizeof(*new_elems)); \</span><br><span style="color: hsl(0, 100%, 40%);">- if (new_elems || (vec)->current == 0) { \</span><br><span style="color: hsl(0, 100%, 40%);">- (vec)->elems = new_elems; \</span><br><span style="color: hsl(0, 100%, 40%);">- (vec)->max = new_max; \</span><br><span style="color: hsl(0, 100%, 40%);">- } else { \</span><br><span style="color: hsl(0, 100%, 40%);">- res = -1; \</span><br><span style="color: hsl(0, 100%, 40%);">- break; \</span><br><span style="color: hsl(0, 100%, 40%);">- } \</span><br><span style="color: hsl(0, 100%, 40%);">- } \</span><br><span style="color: hsl(0, 100%, 40%);">- } while(0); \</span><br><span style="color: hsl(0, 100%, 40%);">- res; \</span><br><span style="color: hsl(120, 100%, 40%);">+#define AST_VECTOR_COMPACT(vec) ({ \</span><br><span style="color: hsl(120, 100%, 40%);">+ int res = 0; \</span><br><span style="color: hsl(120, 100%, 40%);">+ do { \</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t new_max = (vec)->current; \</span><br><span style="color: hsl(120, 100%, 40%);">+ if (new_max == 0) { \</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free((vec)->elems); \</span><br><span style="color: hsl(120, 100%, 40%);">+ (vec)->elems = NULL; \</span><br><span style="color: hsl(120, 100%, 40%);">+ (vec)->max = 0; \</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if ((vec)->max > new_max) { \</span><br><span style="color: hsl(120, 100%, 40%);">+ typeof((vec)->elems) new_elems = ast_realloc( \</span><br><span style="color: hsl(120, 100%, 40%);">+ (vec)->elems, \</span><br><span style="color: hsl(120, 100%, 40%);">+ new_max * sizeof(*new_elems)); \</span><br><span style="color: hsl(120, 100%, 40%);">+ if (new_elems) { \</span><br><span style="color: hsl(120, 100%, 40%);">+ (vec)->elems = new_elems; \</span><br><span style="color: hsl(120, 100%, 40%);">+ (vec)->max = new_max; \</span><br><span style="color: hsl(120, 100%, 40%);">+ } else { \</span><br><span style="color: hsl(120, 100%, 40%);">+ res = -1; \</span><br><span style="color: hsl(120, 100%, 40%);">+ break; \</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%);">+ } while(0); \</span><br><span style="color: hsl(120, 100%, 40%);">+ res; \</span><br><span> })</span><br><span> </span><br><span> /*!</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/14742">change 14742</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/+/14742"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: Ife31d4b510ebab41cb5477fdc7ea4e3138ca8b4f </div>
<div style="display:none"> Gerrit-Change-Number: 14742 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: Sean Bright <sean.bright@gmail.com> </div>
<div style="display:none"> Gerrit-Reviewer: Benjamin Keith Ford <bford@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>