mirror of
https://github.com/asterisk/asterisk.git
synced 2025-10-13 08:13:22 +00:00
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:
@@ -637,24 +637,28 @@ int ast_vector_string_split(struct ast_vector_string *dest,
|
|||||||
* \return 0 on success.
|
* \return 0 on success.
|
||||||
* \return Non-zero on failure.
|
* \return Non-zero on failure.
|
||||||
*/
|
*/
|
||||||
#define AST_VECTOR_COMPACT(vec) ({ \
|
#define AST_VECTOR_COMPACT(vec) ({ \
|
||||||
int res = 0; \
|
int res = 0; \
|
||||||
do { \
|
do { \
|
||||||
if ((vec)->max > (vec)->current) { \
|
size_t new_max = (vec)->current; \
|
||||||
size_t new_max = (vec)->current; \
|
if (new_max == 0) { \
|
||||||
typeof((vec)->elems) new_elems = ast_realloc( \
|
ast_free((vec)->elems); \
|
||||||
(vec)->elems, \
|
(vec)->elems = NULL; \
|
||||||
new_max * sizeof(*new_elems)); \
|
(vec)->max = 0; \
|
||||||
if (new_elems || (vec)->current == 0) { \
|
} else if ((vec)->max > new_max) { \
|
||||||
(vec)->elems = new_elems; \
|
typeof((vec)->elems) new_elems = ast_realloc( \
|
||||||
(vec)->max = new_max; \
|
(vec)->elems, \
|
||||||
} else { \
|
new_max * sizeof(*new_elems)); \
|
||||||
res = -1; \
|
if (new_elems) { \
|
||||||
break; \
|
(vec)->elems = new_elems; \
|
||||||
} \
|
(vec)->max = new_max; \
|
||||||
} \
|
} else { \
|
||||||
} while(0); \
|
res = -1; \
|
||||||
res; \
|
break; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
} while(0); \
|
||||||
|
res; \
|
||||||
})
|
})
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
Reference in New Issue
Block a user