Skip to content

Commit bf5be0a

Browse files
committed
Resync
1 parent fb5de9e commit bf5be0a

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

include/lists/dir_list.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ bool dir_list_append(struct string_list *list, const char *dir, const char *ext,
6464
struct string_list *dir_list_new(const char *dir, const char *ext,
6565
bool include_dirs, bool include_hidden, bool include_compressed, bool recursive);
6666

67+
/* Warning: 'list' must zero initialised before
68+
* calling this function, otherwise memory leaks/
69+
* undefined behaviour will occur */
6770
bool dir_list_initialize(struct string_list *list,
6871
const char *dir,
6972
const char *ext, bool include_dirs,

lists/dir_list.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,17 @@ struct string_list *dir_list_new(const char *dir,
264264
return list;
265265
}
266266

267+
/* Warning: 'list' must zero initialised before
268+
* calling this function, otherwise memory leaks/
269+
* undefined behaviour will occur */
267270
bool dir_list_initialize(struct string_list *list,
268271
const char *dir,
269272
const char *ext, bool include_dirs,
270273
bool include_hidden, bool include_compressed,
271274
bool recursive)
272275
{
273-
if (!list)
274-
return NULL;
276+
if (!list || !string_list_initialize(list))
277+
return false;
275278
return dir_list_append(list, dir, ext, include_dirs,
276279
include_hidden, include_compressed, recursive);
277280
}

lists/string_list.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,15 @@ bool string_list_append(struct string_list *list, const char *elem,
168168
{
169169
char *data_dup = NULL;
170170

171+
/* Note: If 'list' is incorrectly initialised
172+
* (i.e. if struct is zero initialised and
173+
* string_list_initialize() is not called on
174+
* it) capacity will be zero. This will cause
175+
* a segfault. Handle this case by forcing the new
176+
* capacity to a fixed size of 32 */
171177
if (list->size >= list->cap &&
172-
!string_list_capacity(list, list->cap * 2))
178+
!string_list_capacity(list,
179+
(list->cap > 0) ? (list->cap * 2) : 32))
173180
return false;
174181

175182
data_dup = strdup(elem);

0 commit comments

Comments
 (0)