From: Michael Tremer Date: Mon, 15 Aug 2022 18:12:10 +0000 (+0000) Subject: Don't abuse errno as return code X-Git-Tag: 0.9.15~43 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=198e382c2590b94e39a608b9831cc0f434e0e04a;p=people%2Fms%2Flibloc.git Don't abuse errno as return code 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 --- diff --git a/src/as-list.c b/src/as-list.c index 7154fd2..cfb9392 100644 --- a/src/as-list.c +++ b/src/as-list.c @@ -14,7 +14,6 @@ Lesser General Public License for more details. */ -#include #include #include @@ -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; diff --git a/src/as.c b/src/as.c index 2cbd5c8..97c0a56 100644 --- 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; diff --git a/src/country-list.c b/src/country-list.c index b2aea8b..0ed7ef1 100644 --- a/src/country-list.c +++ b/src/country-list.c @@ -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); diff --git a/src/country.c b/src/country.c index 0f13319..a871c4c 100644 --- a/src/country.c +++ b/src/country.c @@ -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; diff --git a/src/database.c b/src/database.c index 9ac80bc..dcb84a1 100644 --- a/src/database.c +++ b/src/database.c @@ -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); diff --git a/src/libloc.c b/src/libloc.c index cf2d740..fb89b56 100644 --- a/src/libloc.c +++ b/src/libloc.c @@ -21,7 +21,6 @@ #include #include #include -#include #include #include @@ -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; diff --git a/src/network-list.c b/src/network-list.c index e2bbf05..9aeafb5 100644 --- a/src/network-list.c +++ b/src/network-list.c @@ -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; diff --git a/src/network.c b/src/network.c index 37a483e..12e78e7 100644 --- a/src/network.c +++ b/src/network.c @@ -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; diff --git a/src/python/database.c b/src/python/database.c index a33c31f..f5797db 100644 --- a/src/python/database.c +++ b/src/python/database.c @@ -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; } } diff --git a/src/stringpool.c b/src/stringpool.c index 8911fb6..98ee7cc 100644 --- a/src/stringpool.c +++ b/src/stringpool.c @@ -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) { diff --git a/src/test-as.c b/src/test-as.c index 43052b4..b135c6b 100644 --- a/src/test-as.c +++ b/src/test-as.c @@ -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); } diff --git a/src/test-country.c b/src/test-country.c index e4edea0..9820e8b 100644 --- a/src/test-country.c +++ b/src/test-country.c @@ -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); } diff --git a/src/test-database.c b/src/test-database.c index 1d48661..8ba558a 100644 --- a/src/test-database.c +++ b/src/test-database.c @@ -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); } diff --git a/src/test-network.c b/src/test-network.c index dcb389a..866f493 100644 --- a/src/test-network.c +++ b/src/test-network.c @@ -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); } diff --git a/src/test-signature.c b/src/test-signature.c index 6c4d398..9af9236 100644 --- a/src/test-signature.c +++ b/src/test-signature.c @@ -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); } diff --git a/src/test-stringpool.c b/src/test-stringpool.c index 392aa29..a94d8f8 100644 --- a/src/test-stringpool.c +++ b/src/test-stringpool.c @@ -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); } } diff --git a/src/writer.c b/src/writer.c index af9a36d..7afba86 100644 --- a/src/writer.c +++ b/src/writer.c @@ -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; }