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
This commit is contained in:
Sean Bright 2020-08-04 11:51:16 -04:00 committed by Joshua Colp
parent 1e58da7814
commit abad395098
1 changed files with 22 additions and 18 deletions

View File

@ -637,24 +637,28 @@ int ast_vector_string_split(struct ast_vector_string *dest,
* \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; \
})
/*!