Skip to content

Commit dcfc675

Browse files
committed
Fixing an edge case memory leak.
1 parent 48870e7 commit dcfc675

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/array.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,26 @@ Array *array_new()
6363
*/
6464
Array *array_new_conf(ArrayConf *conf)
6565
{
66-
Array *ar = conf->mem_calloc(1, sizeof(Array));
67-
68-
if (ar == NULL)
69-
return NULL;
70-
66+
float ex;
67+
68+
/* The expansion factor must be greater than one for the
69+
* array to grow */
7170
if (conf->exp_factor <= 1)
72-
ar->exp_factor = DEFAULT_EXPANSION_FACTOR;
73-
71+
ex = DEFAULT_EXPANSION_FACTOR;
72+
else
73+
ex = conf->exp_factor;
74+
7475
/* Needed to avoid an integer overflow on the first resize and
7576
* to easily check for any future oveflows. */
76-
else if (conf->exp_factor >= MAX_ELEMENTS / conf->capacity)
77+
if (!conf->capacity || ex >= MAX_ELEMENTS / conf->capacity)
7778
return NULL;
78-
else
79-
ar->exp_factor = conf->exp_factor;
79+
80+
Array *ar = conf->mem_calloc(1, sizeof(Array));
8081

82+
if (ar == NULL)
83+
return NULL;
84+
85+
ar->exp_factor = ex;
8186
ar->capacity = conf->capacity;
8287
ar->mem_alloc = conf->mem_alloc;
8388
ar->mem_calloc = conf->mem_calloc;

0 commit comments

Comments
 (0)