From 345c5fa631d5638ea0df4b18bc9438fe4ba6f5ac Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Thu, 24 Nov 2016 11:28:00 +0100 Subject: [PATCH] lua cache.count(): return nil on error ... and avoid returning a negative number. --- daemon/README.rst | 2 +- daemon/bindings.c | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/daemon/README.rst b/daemon/README.rst index b3958eecc..fbad4f787 100644 --- a/daemon/README.rst +++ b/daemon/README.rst @@ -768,7 +768,7 @@ daemons or manipulated from other processes, making for example synchronised loa .. function:: cache.count() - :return: Number of entries in the cache. + :return: Number of entries in the cache or nil on error. .. function:: cache.close() diff --git a/daemon/bindings.c b/daemon/bindings.c index 408603a05..75cd16c8b 100644 --- a/daemon/bindings.c +++ b/daemon/bindings.c @@ -430,10 +430,11 @@ static int cache_count(lua_State *L) struct engine *engine = engine_luaget(L); const struct kr_cdb_api *api = engine->resolver.cache.api; - /* First key is a version counter, omit it. */ struct kr_cache *cache = &engine->resolver.cache; - if (kr_cache_is_open(cache)) { - lua_pushinteger(L, api->count(cache->db) - 1); + int count = api->count(cache->db); + if (kr_cache_is_open(cache) && count >= 0) { + /* First key is a version counter, omit it if nonempty. */ + lua_pushinteger(L, count ? count - 1 : 0); return 1; } return 0; -- 2.47.2