From: Vladimír Čunát Date: Thu, 24 Nov 2016 10:28:00 +0000 (+0100) Subject: lua cache.count(): return nil on error X-Git-Tag: v1.2.0-rc1~73^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=345c5fa631d5638ea0df4b18bc9438fe4ba6f5ac;p=thirdparty%2Fknot-resolver.git lua cache.count(): return nil on error ... and avoid returning a negative number. --- 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;