]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
Don't abuse errno as return code
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 15 Aug 2022 18:12:10 +0000 (18:12 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 15 Aug 2022 18:12:10 +0000 (18:12 +0000)
errno is not either set properly, or left set properly by any function
that we have called.

Return codes should either be zero or non-zero for functions unless they
are some value.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
17 files changed:
src/as-list.c
src/as.c
src/country-list.c
src/country.c
src/database.c
src/libloc.c
src/network-list.c
src/network.c
src/python/database.c
src/stringpool.c
src/test-as.c
src/test-country.c
src/test-database.c
src/test-network.c
src/test-signature.c
src/test-stringpool.c
src/writer.c

index 7154fd2e296ff5fce43d7e93ac550ab54a4957fd..cfb93923ba065a757ab97aa6dd6d9eeefb4fd1bf 100644 (file)
@@ -14,7 +14,6 @@
        Lesser General Public License for more details.
 */
 
-#include <errno.h>
 #include <stdlib.h>
 
 #include <libloc/as.h>
@@ -42,7 +41,7 @@ static int loc_as_list_grow(struct loc_as_list* list) {
        struct loc_as** elements = reallocarray(list->elements,
                        list->elements_size + size, sizeof(*list->elements));
        if (!elements)
-               return -errno;
+               return 1;
 
        list->elements = elements;
        list->elements_size += size;
@@ -54,7 +53,7 @@ LOC_EXPORT int loc_as_list_new(struct loc_ctx* ctx,
                struct loc_as_list** list) {
        struct loc_as_list* l = calloc(1, sizeof(*l));
        if (!l)
-               return -ENOMEM;
+               return 1;
 
        l->ctx = loc_ref(ctx);
        l->refcount = 1;
index 2cbd5c8c4fe06ff1c54878aa80512901384c7804..97c0a567a48c9760ac30c800b43df957cae9dbc7 100644 (file)
--- a/src/as.c
+++ b/src/as.c
@@ -42,7 +42,7 @@ struct loc_as {
 LOC_EXPORT int loc_as_new(struct loc_ctx* ctx, struct loc_as** as, uint32_t number) {
        struct loc_as* a = calloc(1, sizeof(*a));
        if (!a)
-               return -ENOMEM;
+               return 1;
 
        a->ctx = loc_ref(ctx);
        a->refcount = 1;
index b2aea8b043477be11d2f6832dd57f12e607ab6eb..0ed7ef1a53fe49016eb294d53a02404126cc5478 100644 (file)
@@ -42,7 +42,7 @@ static int loc_country_list_grow(struct loc_country_list* list) {
        struct loc_country** elements = reallocarray(list->elements,
                        list->elements_size + size, sizeof(*list->elements));
        if (!elements)
-               return -errno;
+               return 1;
 
        list->elements = elements;
        list->elements_size += size;
@@ -159,8 +159,8 @@ LOC_EXPORT int loc_country_list_contains_code(
                // Ignore invalid country codes which would never match
                if (errno == EINVAL)
                        return 0;
-               else
-                       return r;
+
+               return r;
        }
 
        r = loc_country_list_contains(list, country);
index 0f133198e4ec50f1adcdf46bf334f511ecc071ec..a871c4cb2ea3f37607055c061c21671acdb2fda2 100644 (file)
@@ -54,7 +54,7 @@ LOC_EXPORT int loc_country_new(struct loc_ctx* ctx, struct loc_country** country
 
        struct loc_country* c = calloc(1, sizeof(*c));
        if (!c)
-               return -ENOMEM;
+               return 1;
 
        c->ctx = loc_ref(ctx);
        c->refcount = 1;
index 9ac80bc4f2d867c655830f0e1ea5f0a1f88e7f9a..dcb84a16a9db5a877cb2f2a06f8b356014ba7689 100644 (file)
@@ -173,7 +173,7 @@ static int loc_database_read_as_section_v1(struct loc_database* db,
                        MAP_SHARED, fileno(db->f), as_offset);
 
                if (db->as_v1 == MAP_FAILED)
-                       return -errno;
+                       return 1;
        }
 
        db->as_count = as_length / sizeof(*db->as_v1);
@@ -196,7 +196,7 @@ static int loc_database_read_network_nodes_section_v1(struct loc_database* db,
                        MAP_SHARED, fileno(db->f), network_nodes_offset);
 
                if (db->network_nodes_v1 == MAP_FAILED)
-                       return -errno;
+                       return 1;
        }
 
        db->network_nodes_count = network_nodes_length / sizeof(*db->network_nodes_v1);
@@ -219,7 +219,7 @@ static int loc_database_read_networks_section_v1(struct loc_database* db,
                        MAP_SHARED, fileno(db->f), networks_offset);
 
                if (db->networks_v1 == MAP_FAILED)
-                       return -errno;
+                       return 1;
        }
 
        db->networks_count = networks_length / sizeof(*db->networks_v1);
@@ -242,7 +242,7 @@ static int loc_database_read_countries_section_v1(struct loc_database* db,
                        MAP_SHARED, fileno(db->f), countries_offset);
 
                if (db->countries_v1 == MAP_FAILED)
-                       return -errno;
+                       return 1;
        }
 
        db->countries_count = countries_length / sizeof(*db->countries_v1);
index cf2d740a6cde39aabe486b9f347b7bee72ea983c..fb89b562e7fe7f826d5abc999ae0a927af371d7a 100644 (file)
@@ -21,7 +21,6 @@
 #include <stddef.h>
 #include <stdarg.h>
 #include <unistd.h>
-#include <errno.h>
 #include <string.h>
 #include <ctype.h>
 
@@ -77,7 +76,7 @@ static int log_priority(const char* priority) {
 LOC_EXPORT int loc_new(struct loc_ctx** ctx) {
        struct loc_ctx* c = calloc(1, sizeof(*c));
        if (!c)
-               return -ENOMEM;
+               return 1;
 
        c->refcount = 1;
        c->log_fn = log_stderr;
index e2bbf05a8c8011231c2820d581c4dd07fc6ffb8f..9aeafb5879a02b39c7922a4c7cf57753b57156c6 100644 (file)
@@ -44,7 +44,7 @@ static int loc_network_list_grow(struct loc_network_list* list) {
        struct loc_network** elements = reallocarray(list->elements,
                        list->elements_size + size, sizeof(*list->elements));
        if (!elements)
-               return -errno;
+               return 1;
 
        list->elements = elements;
        list->elements_size += size;
index 37a483eaf0b33b32406bd87e9adc8445fa84fdfa..12e78e782c65b15819e9f1f2408f24f9607f7456 100644 (file)
@@ -59,10 +59,8 @@ LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network
        }
 
        struct loc_network* n = calloc(1, sizeof(*n));
-       if (!n) {
-               errno = ENOMEM;
+       if (!n)
                return 1;
-       }
 
        n->ctx = loc_ref(ctx);
        n->refcount = 1;
@@ -580,7 +578,7 @@ int loc_network_new_from_database_v1(struct loc_ctx* ctx, struct loc_network** n
 
        int r = loc_network_new(ctx, network, address, prefix);
        if (r) {
-               ERROR(ctx, "Could not allocate a new network: %s", strerror(-r));
+               ERROR(ctx, "Could not allocate a new network: %m\n");
                return r;
        }
 
@@ -632,7 +630,7 @@ struct loc_network_tree_node {
 int loc_network_tree_new(struct loc_ctx* ctx, struct loc_network_tree** tree) {
        struct loc_network_tree* t = calloc(1, sizeof(*t));
        if (!t)
-               return -ENOMEM;
+               return 1;
 
        t->ctx = loc_ref(ctx);
        t->refcount = 1;
index a33c31f3621ceae2cbd40699015a77b9058f6062..f5797dbb31d8ef2b279d5dfffad17a7e6de02a6e 100644 (file)
@@ -353,7 +353,7 @@ static PyObject* Database_search_networks(DatabaseObject* self, PyObject* args,
                struct loc_as_list* asns;
                r = loc_as_list_new(loc_ctx, &asns);
                if (r) {
-                       PyErr_SetString(PyExc_SystemError, "Could not create AS list");
+                       PyErr_SetFromErrno(PyExc_OSError);
                        return NULL;
                }
 
@@ -372,7 +372,7 @@ static PyObject* Database_search_networks(DatabaseObject* self, PyObject* args,
                        struct loc_as* as;
                        r = loc_as_new(loc_ctx, &as, number);
                        if (r) {
-                               PyErr_SetString(PyExc_SystemError, "Could not create AS");
+                               PyErr_SetFromErrno(PyExc_OSError);
 
                                loc_as_list_unref(asns);
                                loc_as_unref(as);
@@ -381,7 +381,7 @@ static PyObject* Database_search_networks(DatabaseObject* self, PyObject* args,
 
                        r = loc_as_list_append(asns, as);
                        if (r) {
-                               PyErr_SetString(PyExc_SystemError, "Could not append AS to the list");
+                               PyErr_SetFromErrno(PyExc_OSError);
 
                                loc_as_list_unref(asns);
                                loc_as_unref(as);
@@ -393,7 +393,7 @@ static PyObject* Database_search_networks(DatabaseObject* self, PyObject* args,
 
                r = loc_database_enumerator_set_asns(enumerator, asns);
                if (r) {
-                       PyErr_SetFromErrno(PyExc_SystemError);
+                       PyErr_SetFromErrno(PyExc_OSError);
 
                        loc_as_list_unref(asns);
                        return NULL;
@@ -407,7 +407,7 @@ static PyObject* Database_search_networks(DatabaseObject* self, PyObject* args,
                r = loc_database_enumerator_set_flag(enumerator, flags);
 
                if (r) {
-                       PyErr_SetFromErrno(PyExc_SystemError);
+                       PyErr_SetFromErrno(PyExc_OSError);
                        return NULL;
                }
        }
@@ -417,7 +417,7 @@ static PyObject* Database_search_networks(DatabaseObject* self, PyObject* args,
                r = loc_database_enumerator_set_family(enumerator, family);
 
                if (r) {
-                       PyErr_SetFromErrno(PyExc_SystemError);
+                       PyErr_SetFromErrno(PyExc_OSError);
                        return NULL;
                }
        }
index 8911fb606996387c14ffd90924e7ff7cc5c3fae3..98ee7cca10a240298912346552ee10784e7d29c6 100644 (file)
@@ -45,11 +45,15 @@ struct loc_stringpool {
 };
 
 static off_t loc_stringpool_get_offset(struct loc_stringpool* pool, const char* pos) {
-       if (pos < pool->data)
-               return -EFAULT;
+       if (pos < pool->data) {
+               errno = EFAULT;
+               return -1;
+       }
 
-       if (pos > (pool->data + pool->length))
-               return -EFAULT;
+       if (pos > (pool->data + pool->length)) {
+               errno = EFAULT;
+               return -1;
+       }
 
        return pos - pool->data;
 }
@@ -70,7 +74,7 @@ static int loc_stringpool_grow(struct loc_stringpool* pool, size_t length) {
        // Reallocate data section
        pool->data = realloc(pool->data, length);
        if (!pool->data)
-               return -ENOMEM;
+               return 1;
 
        pool->length = length;
 
@@ -81,17 +85,17 @@ static int loc_stringpool_grow(struct loc_stringpool* pool, size_t length) {
 }
 
 static off_t loc_stringpool_append(struct loc_stringpool* pool, const char* string) {
-       if (!string)
-               return -EINVAL;
+       if (!string) {
+               errno = EINVAL;
+               return -1;
+       }
 
        DEBUG(pool->ctx, "Appending '%s' to string pool at %p\n", string, pool);
 
        // Make sure we have enough space
        int r = loc_stringpool_grow(pool, pool->length + strlen(string) + 1);
-       if (r) {
-               errno = r;
+       if (r)
                return -1;
-       }
 
        off_t offset = loc_stringpool_get_offset(pool, pool->pos);
 
@@ -146,8 +150,10 @@ int loc_stringpool_new(struct loc_ctx* ctx, struct loc_stringpool** pool) {
 }
 
 static int loc_stringpool_mmap(struct loc_stringpool* pool, FILE* f, size_t length, off_t offset) {
-       if (pool->mode != STRINGPOOL_MMAP)
-               return -EINVAL;
+       if (pool->mode != STRINGPOOL_MMAP) {
+               errno = EINVAL;
+               return 1;
+       }
 
        DEBUG(pool->ctx, "Reading string pool starting from %jd (%zu bytes)\n", (intmax_t)offset, length);
 
@@ -221,8 +227,10 @@ size_t loc_stringpool_get_size(struct loc_stringpool* pool) {
 }
 
 static off_t loc_stringpool_find(struct loc_stringpool* pool, const char* s) {
-       if (!s || !*s)
-               return -EINVAL;
+       if (!s || !*s) {
+               errno = EINVAL;
+               return -1;
+       }
 
        off_t offset = 0;
        while (offset < pool->length) {
@@ -237,7 +245,9 @@ static off_t loc_stringpool_find(struct loc_stringpool* pool, const char* s) {
                offset = loc_stringpool_get_next_offset(pool, offset);
        }
 
-       return -ENOENT;
+       // Nothing found
+       errno = ENOENT;
+       return -1;
 }
 
 off_t loc_stringpool_add(struct loc_stringpool* pool, const char* string) {
index 43052b4d88df0d881471019519c0721e64dd9417..b135c6b9ffd91d1369b1da94c99123c25a85ce0a 100644 (file)
@@ -61,7 +61,7 @@ int main(int argc, char** argv) {
 
        err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
        if (err) {
-               fprintf(stderr, "Could not write database: %s\n", strerror(-err));
+               fprintf(stderr, "Could not write database: %m\n");
                exit(EXIT_FAILURE);
        }
 
@@ -71,7 +71,7 @@ int main(int argc, char** argv) {
        struct loc_database* db;
        err = loc_database_new(ctx, &db, f);
        if (err) {
-               fprintf(stderr, "Could not open database: %s\n", strerror(-err));
+               fprintf(stderr, "Could not open database: %m\n");
                exit(EXIT_FAILURE);
        }
 
index e4edea0dabde149dac674dc559d5b9e041721dc0..9820e8b0f187ce0d38df2ea082da4c2c39032879 100644 (file)
@@ -130,7 +130,7 @@ int main(int argc, char** argv) {
 
        err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
        if (err) {
-               fprintf(stderr, "Could not write database: %s\n", strerror(-err));
+               fprintf(stderr, "Could not write database: %m\n");
                exit(EXIT_FAILURE);
        }
        loc_writer_unref(writer);
@@ -139,7 +139,7 @@ int main(int argc, char** argv) {
        struct loc_database* db;
        err = loc_database_new(ctx, &db, f);
        if (err) {
-               fprintf(stderr, "Could not open database: %s\n", strerror(-err));
+               fprintf(stderr, "Could not open database: %m\n");
                exit(EXIT_FAILURE);
        }
 
index 1d48661be45bb885f970637367320d38539acf02..8ba558a3eb4c82b34c0cfbb8787b5215c4613889 100644 (file)
@@ -173,7 +173,7 @@ int main(int argc, char** argv) {
 
        err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
        if (err) {
-               fprintf(stderr, "Could not write database: %s\n", strerror(err));
+               fprintf(stderr, "Could not write database: %m\n");
                exit(EXIT_FAILURE);
        }
        loc_writer_unref(writer);
@@ -182,7 +182,7 @@ int main(int argc, char** argv) {
        struct loc_database* db;
        err = loc_database_new(ctx, &db, f);
        if (err) {
-               fprintf(stderr, "Could not open database: %s\n", strerror(-err));
+               fprintf(stderr, "Could not open database: %m\n");
                exit(EXIT_FAILURE);
        }
 
index dcb389a6ff3eeb265527221d0b998eadfa7e37e9..866f49361955d905caa41154afb3206c5a826c1f 100644 (file)
@@ -266,7 +266,7 @@ int main(int argc, char** argv) {
 
        err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
        if (err) {
-               fprintf(stderr, "Could not write database: %s\n", strerror(-err));
+               fprintf(stderr, "Could not write database: %m\n");
                exit(EXIT_FAILURE);
        }
        loc_writer_unref(writer);
@@ -284,7 +284,7 @@ int main(int argc, char** argv) {
        struct loc_database* db;
        err = loc_database_new(ctx, &db, f);
        if (err) {
-               fprintf(stderr, "Could not open database: %s\n", strerror(-err));
+               fprintf(stderr, "Could not open database: %m\n");
                exit(EXIT_FAILURE);
        }
 
index 6c4d398c97111cd87e9ff5994a70b623164b1e9f..9af9236b86a7b54a8fd1eac75896db2948e2fc4e 100644 (file)
@@ -73,7 +73,7 @@ int main(int argc, char** argv) {
 
        err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
        if (err) {
-               fprintf(stderr, "Could not write database: %s\n", strerror(err));
+               fprintf(stderr, "Could not write database: %m\n");
                exit(EXIT_FAILURE);
        }
        loc_writer_unref(writer);
@@ -82,7 +82,7 @@ int main(int argc, char** argv) {
        struct loc_database* db;
        err = loc_database_new(ctx, &db, f);
        if (err) {
-               fprintf(stderr, "Could not open database: %s\n", strerror(-err));
+               fprintf(stderr, "Could not open database: %m\n");
                exit(EXIT_FAILURE);
        }
 
index 392aa29be85cc572fec52362d5726610b5375811..a94d8f8bd07815e65f6024ee6f5dc4f3d0c4c54b 100644 (file)
@@ -74,7 +74,7 @@ int main(int argc, char** argv) {
        // Append a string
        off_t pos = loc_stringpool_add(pool, "ABC");
        if (pos < 0) {
-               fprintf(stderr, "Could not add string: %s\n", strerror(-pos));
+               fprintf(stderr, "Could not add string: %m\n");
                exit(EXIT_FAILURE);
        }
 
@@ -108,7 +108,7 @@ int main(int argc, char** argv) {
                free(string);
 
                if (pos < 0) {
-                       fprintf(stderr, "Could not add string %d: %s\n", i, strerror(-pos));
+                       fprintf(stderr, "Could not add string %d: %m\n", i);
                        exit(EXIT_FAILURE);
                }
        }
index af9a36d7f2fcab04c34df59673b801ac398aecc4..7afba868299da084a3ec89a5f02a9e32de9d8c57 100644 (file)
@@ -91,7 +91,7 @@ LOC_EXPORT int loc_writer_new(struct loc_ctx* ctx, struct loc_writer** writer,
                FILE* fkey1, FILE* fkey2) {
        struct loc_writer* w = calloc(1, sizeof(*w));
        if (!w)
-               return -ENOMEM;
+               return 1;
 
        w->ctx = loc_ref(ctx);
        w->refcount = 1;
@@ -584,7 +584,7 @@ static int loc_writer_create_signature(struct loc_writer* writer,
 
                if (ferror(f)) {
                        ERROR(writer->ctx, "Error reading from file: %m\n");
-                       r = errno;
+                       r = 1;
                        goto END;
                }