mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-04 20:04:50 +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:
@@ -610,12 +610,16 @@ AST_VECTOR(ast_vector_string, char *);
|
|||||||
#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) { \
|
||||||
|
ast_free((vec)->elems); \
|
||||||
|
(vec)->elems = NULL; \
|
||||||
|
(vec)->max = 0; \
|
||||||
|
} else if ((vec)->max > new_max) { \
|
||||||
typeof((vec)->elems) new_elems = ast_realloc( \
|
typeof((vec)->elems) new_elems = ast_realloc( \
|
||||||
(vec)->elems, \
|
(vec)->elems, \
|
||||||
new_max * sizeof(*new_elems)); \
|
new_max * sizeof(*new_elems)); \
|
||||||
if (new_elems || (vec)->current == 0) { \
|
if (new_elems) { \
|
||||||
(vec)->elems = new_elems; \
|
(vec)->elems = new_elems; \
|
||||||
(vec)->max = new_max; \
|
(vec)->max = new_max; \
|
||||||
} else { \
|
} else { \
|
||||||
|
Reference in New Issue
Block a user