]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lua cache.count(): return nil on error
authorVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 24 Nov 2016 10:28:00 +0000 (11:28 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 24 Nov 2016 10:30:02 +0000 (11:30 +0100)
... and avoid returning a negative number.

daemon/README.rst
daemon/bindings.c

index b3958eecc28831a9f285eb942bc9c06fc3efc4f8..fbad4f787da45378fcae9499664c965759b3a084 100644 (file)
@@ -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()
 
index 408603a0532bdfa8712e19e486c50e6a6511967b..75cd16c8b74d47b19e7907490d8fcf0839c01738 100644 (file)
@@ -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;