Commit 92994c35 authored by Marek Vavrusa's avatar Marek Vavrusa

lib/cache: fixed reinsert of invalidated record

when opening an empty database, an invalidated key
value was written resulting in possible corruption
of the cache
parent 9259b273
......@@ -430,14 +430,14 @@ int main(int argc, char **argv)
ret = run_worker(loop, &engine);
}
}
if (ret != 0) {
ret = EXIT_FAILURE;
}
/* Cleanup. */
array_clear(addr_set);
engine_deinit(&engine);
worker_reclaim(worker);
mp_delete(pool.ctx);
if (ret != 0) {
ret = EXIT_FAILURE;
}
array_clear(addr_set);
kr_crypto_cleanup();
return ret;
}
......@@ -53,15 +53,12 @@ static int assert_right_version(struct kr_cache *cache)
{
/* Check cache ABI version */
knot_db_val_t key = { KEY_VERSION, 2 };
knot_db_val_t val = { NULL, 0 };
knot_db_val_t val = { KEY_VERSION, 2 };
int ret = cache_op(cache, read, &key, &val, 1);
if (ret == 0) {
ret = kr_error(EEXIST);
} else {
/*
* Version doesn't match.
* Recreate cache and write version key.
*/
/* Version doesn't match. Recreate cache and write version key. */
ret = cache_op(cache, count);
if (ret != 0) { /* Non-empty cache, purge it. */
kr_log_info("[cache] purging cache\n");
......@@ -69,9 +66,14 @@ static int assert_right_version(struct kr_cache *cache)
}
/* Either purged or empty. */
if (ret == 0) {
/* Key/Val is invalidated by cache purge, recreate it */
key.data = KEY_VERSION;
key.len = 2;
val = key;
ret = cache_op(cache, write, &key, &val, 1);
}
}
cache_op(cache, sync);
return ret;
}
......
......@@ -63,6 +63,11 @@ static int fake_test_init(knot_db_t **db, struct kr_cdb_opts *opts, knot_mm_t *p
return mock();
}
static int fake_test_sync(knot_db_t *db)
{
return 0;
}
static void fake_test_deinit(knot_db_t *db)
{
}
......@@ -94,7 +99,7 @@ static const struct kr_cdb_api *fake_knot_db_lmdb_api(void)
{
static const struct kr_cdb_api api = {
"lmdb_fake_api",
fake_test_init, fake_test_deinit, NULL, NULL, NULL,
fake_test_init, fake_test_deinit, NULL, NULL, fake_test_sync,
fake_test_find, fake_test_ins, NULL,
NULL, NULL
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment