Commit 198e382c authored by Michael Tremer's avatar Michael Tremer

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: default avatarMichael Tremer <michael.tremer@ipfire.org>
parent 6cd02f1d
......@@ -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;
......
......@@ -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;
......
......@@ -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);
......
......@@ -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;
......
......@@ -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);
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
}
}
......
......@@ -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) {
......
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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);
}
}
......
......@@ -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;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment