[Asterisk-code-review] vector.h: Fix implementation of AST_VECTOR_COMPACT() for empty vectors (asterisk[18])
Joshua Colp
asteriskteam at digium.com
Mon Aug 10 07:10:31 CDT 2020
Joshua Colp has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/14744 )
Change subject: vector.h: Fix implementation of AST_VECTOR_COMPACT() for empty vectors
......................................................................
vector.h: Fix implementation of AST_VECTOR_COMPACT() for empty vectors
The assumed behavior of realloc() - that it was effectively a free() if
its second argument was 0 - is Linux specific behavior and is not
guaranteed by either POSIX or the C specification.
Instead, if we want to resize a vector to 0, do it explicitly.
Change-Id: Ife31d4b510ebab41cb5477fdc7ea4e3138ca8b4f
---
M include/asterisk/vector.h
1 file changed, 22 insertions(+), 18 deletions(-)
Approvals:
Joshua Colp: Looks good to me, approved; Approved for Submit
Benjamin Keith Ford: Looks good to me, but someone else must approve
diff --git a/include/asterisk/vector.h b/include/asterisk/vector.h
index 8f5cf8c..00d701b 100644
--- a/include/asterisk/vector.h
+++ b/include/asterisk/vector.h
@@ -637,24 +637,28 @@
* \return 0 on success.
* \return Non-zero on failure.
*/
-#define AST_VECTOR_COMPACT(vec) ({ \
- int res = 0; \
- do { \
- if ((vec)->max > (vec)->current) { \
- size_t new_max = (vec)->current; \
- typeof((vec)->elems) new_elems = ast_realloc( \
- (vec)->elems, \
- new_max * sizeof(*new_elems)); \
- if (new_elems || (vec)->current == 0) { \
- (vec)->elems = new_elems; \
- (vec)->max = new_max; \
- } else { \
- res = -1; \
- break; \
- } \
- } \
- } while(0); \
- res; \
+#define AST_VECTOR_COMPACT(vec) ({ \
+ int res = 0; \
+ do { \
+ size_t new_max = (vec)->current; \
+ if (new_max == 0) { \
+ ast_free((vec)->elems); \
+ (vec)->elems = NULL; \
+ (vec)->max = 0; \
+ } else if ((vec)->max > new_max) { \
+ typeof((vec)->elems) new_elems = ast_realloc( \
+ (vec)->elems, \
+ new_max * sizeof(*new_elems)); \
+ if (new_elems) { \
+ (vec)->elems = new_elems; \
+ (vec)->max = new_max; \
+ } else { \
+ res = -1; \
+ break; \
+ } \
+ } \
+ } while(0); \
+ res; \
})
/*!
--
To view, visit https://gerrit.asterisk.org/c/asterisk/+/14744
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings
Gerrit-Project: asterisk
Gerrit-Branch: 18
Gerrit-Change-Id: Ife31d4b510ebab41cb5477fdc7ea4e3138ca8b4f
Gerrit-Change-Number: 14744
Gerrit-PatchSet: 2
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>
Gerrit-Reviewer: Benjamin Keith Ford <bford at digium.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200810/4e1ea869/attachment.html>
More information about the asterisk-code-review
mailing list