Skip to content

Commit

Permalink
dict-sql: Fix iterator to not fail
Browse files Browse the repository at this point in the history
When iterating a prefix without exact key flag,
do not fail when no more maps are matched if at least
one map has already matched.

Fixes
Error: dict_iterate_deinit failed: sql dict iterate failed for ...: Invalid/unmapped path
  • Loading branch information
cmouse authored and GitLab committed Jan 24, 2017
1 parent 4adf5d2 commit be57188
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/lib-dict/dict-sql.c
Expand Up @@ -37,6 +37,7 @@ struct sql_dict_iterate_context {
unsigned int path_idx, sql_fields_start_idx, next_map_idx;
bool synchronous_result;
bool iter_query_sent;
bool allow_null_map; /* allow next map to be NULL */
const char *error;
};

Expand Down Expand Up @@ -550,8 +551,12 @@ sql_dict_iterate_build_next_query(struct sql_dict_iterate_context *ctx,
unsigned int i, count;

map = sql_dict_iterate_find_next_map(ctx, &values);
/* NULL map is allowed if we have already done some lookups */
if (map == NULL) {
*error_r = "Invalid/unmapped path";
if (!ctx->allow_null_map) {
*error_r = "Invalid/unmapped path";
return -1;
}
return 0;
}

Expand Down Expand Up @@ -630,6 +635,9 @@ static int sql_dict_iterate_next_query(struct sql_dict_iterate_context *ctx)

ret = sql_dict_iterate_build_next_query(ctx, query, &error);
if (ret <= 0) {
/* this is expected error */
if (ret == 0)
return ret;
/* failed */
ctx->error = p_strdup_printf(ctx->pool,
"sql dict iterate failed for %s: %s",
Expand Down Expand Up @@ -702,6 +710,9 @@ static bool sql_dict_iterate(struct dict_iterate_context *_ctx,
if ((ctx->flags & DICT_ITERATE_FLAG_EXACT_KEY) != 0)
return FALSE;
ctx->iter_query_sent = FALSE;
/* we have gotten *SOME* results, so can allow
unmapped next key now. */
ctx->allow_null_map = TRUE;
return sql_dict_iterate(_ctx, key_r, value_r);
}
if (ret < 0) {
Expand Down

0 comments on commit be57188

Please sign in to comment.