]> git.ipfire.org Git - location/libloc.git/commitdiff
database: Have the lookup function return 0 even if nothing was found
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 May 2024 16:39:57 +0000 (16:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 May 2024 16:39:57 +0000 (16:39 +0000)
This is a potentially breaking change, but I think we need to do this as
the previous return code was not very descriptive.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/database.c
src/lua/database.c
src/python/database.c
src/test-network.c

index 617b61eb5a8fe5966136aa54c7cc690579fa2bba..f6a7c8a3d54186f102cbe319cab37c03d1552e80 100644 (file)
@@ -924,11 +924,12 @@ static int __loc_database_lookup(struct loc_database* db, const struct in6_addr*
        // If this node has a leaf, we will check if it matches
        if (__loc_database_node_is_leaf(node_v1)) {
                r = __loc_database_lookup_handle_leaf(db, address, network, network_address, level, node_v1);
-               if (r <= 0)
+               if (r < 0)
                        return r;
        }
 
-       return 1;
+       // Return no error - even if nothing was found
+       return 0;
 }
 
 LOC_EXPORT int loc_database_lookup(struct loc_database* db,
index 8d10c43cbb18d6a62b321a2ad1fa2d729b2c9a09..8328196f9727849c230b4fcceeb3e4c9669b62ce 100644 (file)
@@ -202,8 +202,10 @@ static int Database_lookup(lua_State* L) {
        }
 
        // Create a network object
-       r = create_network(L, network);
-       loc_network_unref(network);
+       if (network) {
+               r = create_network(L, network);
+               loc_network_unref(network);
+       }
 
        return r;
 }
index d6ee4d02d0ed1d35fcf1ccc4244d25763e222cd3..f84b0036c96ca0cfa2855e9c48a411363179b18a 100644 (file)
@@ -201,35 +201,35 @@ static PyObject* Database_get_country(DatabaseObject* self, PyObject* args) {
 static PyObject* Database_lookup(DatabaseObject* self, PyObject* args) {
        struct loc_network* network = NULL;
        const char* address = NULL;
+       int r;
 
        if (!PyArg_ParseTuple(args, "s", &address))
                return NULL;
 
        // Try to retrieve a matching network
-       int r = loc_database_lookup_from_string(self->db, address, &network);
+       r = loc_database_lookup_from_string(self->db, address, &network);
+       if (r) {
+               // Handle any errors
+               switch (errno) {
+                       case EINVAL:
+                               PyErr_Format(PyExc_ValueError, "Invalid IP address: %s", address);
 
-       // We got a network
-       if (r == 0) {
-               PyObject* obj = new_network(&NetworkType, network);
-               loc_network_unref(network);
+                       default:
+                               PyErr_SetFromErrno(PyExc_OSError);
+               }
 
-               return obj;
+               return NULL;
        }
 
        // Nothing found
-       if (!errno)
+       if (!network)
                Py_RETURN_NONE;
 
-       // Handle any errors
-       switch (errno) {
-               case EINVAL:
-                       PyErr_Format(PyExc_ValueError, "Invalid IP address: %s", address);
-
-               default:
-                       PyErr_SetFromErrno(PyExc_OSError);
-       }
+       // We got a network
+       PyObject* obj = new_network(&NetworkType, network);
+       loc_network_unref(network);
 
-       return NULL;
+       return obj;
 }
 
 static PyObject* new_database_enumerator(PyTypeObject* type, struct loc_database_enumerator* enumerator) {
index 69544e29628bc32febc981ba3fa1f9dbc8dd2a24..9eda47fc5be32c2e1eeaa8437504356c2e40d7a1 100644 (file)
@@ -359,7 +359,7 @@ int main(int argc, char** argv) {
 
        // Lookup an address outside the subnet
        err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1);
-       if (err == 0) {
+       if (err || network1) {
                fprintf(stderr, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
                exit(EXIT_FAILURE);
        }