Skip to content

Commit

Permalink
dict-file: Fix potential crash when doing other dict calls during ite…
Browse files Browse the repository at this point in the history
…ration.

If file was refreshed, the hash table was cleared, which broke the existing
iterators.
  • Loading branch information
sirainen committed Jan 17, 2017
1 parent cc64a82 commit cba7c7f
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/lib-dict/dict-file.c
Expand Up @@ -110,6 +110,12 @@ static bool file_dict_need_refresh(struct file_dict *dict)
{
struct stat st1, st2;

if (dict->dict.iter_count > 0) {
/* Change nothing while there are iterators or they can crash
because the hash table content recreated. */
return FALSE;
}

if (dict->fd == -1)
return TRUE;

Expand Down Expand Up @@ -171,7 +177,7 @@ static int file_dict_refresh(struct file_dict *dict)

if (file_dict_open_latest(dict) < 0)
return -1;
if (dict->refreshed)
if (dict->refreshed || dict->dict.iter_count > 0)
return 0;

hash_table_clear(dict->hash, TRUE);
Expand Down Expand Up @@ -229,10 +235,11 @@ file_dict_iterate_init(struct dict *_dict, const char *const *paths,
ctx->paths[i].len = strlen(paths[i]);
}
ctx->flags = flags;
ctx->iter = hash_table_iterate_init(dict->hash);

if (file_dict_refresh(dict) < 0)
ctx->failed = TRUE;

ctx->iter = hash_table_iterate_init(dict->hash);
return &ctx->ctx;
}

Expand Down

0 comments on commit cba7c7f

Please sign in to comment.