From 0f0829ef3e265cf8e7df9321e8a6bf7d71a8c7bf Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 5 Dec 2019 15:47:16 +0000 Subject: [PATCH] location-export: Validate country codes Signed-off-by: Michael Tremer --- src/country.c | 19 +++++++++++++++++++ src/libloc.sym | 1 + src/loc/country.h | 21 ++------------------- src/python/location-exporter.in | 8 ++++---- src/python/locationmodule.c | 18 ++++++++++++++++++ 5 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/country.c b/src/country.c index 9098950..45d26b8 100644 --- a/src/country.c +++ b/src/country.c @@ -172,3 +172,22 @@ int loc_country_to_database_v0(struct loc_country* country, return 0; } + +LOC_EXPORT int loc_country_code_is_valid(const char* cc) { + // It cannot be NULL + if (!cc || !*cc) + return 0; + + // It must be 2 characters long + if (strlen(cc) != 2) + return 0; + + // It must only contain A-Z + for (unsigned int i = 0; i < 2; i++) { + if (cc[i] < 'A' || cc[i] > 'Z') + return 0; + } + + // Looks valid + return 1; +} diff --git a/src/libloc.sym b/src/libloc.sym index b3d5b51..e9e8549 100644 --- a/src/libloc.sym +++ b/src/libloc.sym @@ -39,6 +39,7 @@ global: # Country loc_country_cmp; + loc_country_code_is_valid; loc_country_get_code; loc_country_get_continent_code; loc_country_get_name; diff --git a/src/loc/country.h b/src/loc/country.h index 6dfb47b..9757b2a 100644 --- a/src/loc/country.h +++ b/src/loc/country.h @@ -36,6 +36,8 @@ int loc_country_set_name(struct loc_country* country, const char* name); int loc_country_cmp(struct loc_country* country1, struct loc_country* country2); +int loc_country_code_is_valid(const char* cc); + #ifdef LIBLOC_PRIVATE #include @@ -45,25 +47,6 @@ int loc_country_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool* int loc_country_to_database_v0(struct loc_country* country, struct loc_stringpool* pool, struct loc_database_country_v0* dbobj); -static inline int loc_country_code_is_valid(const char* cc) { - // It cannot be NULL - if (!cc || !*cc) - return 0; - - // It must be 2 characters long - if (strlen(cc) != 2) - return 0; - - // It must only contain A-Z - for (unsigned int i = 0; i < 2; i++) { - if (cc[i] < 'A' || cc[i] > 'Z') - return 0; - } - - // Looks valid - return 1; -} - static inline void loc_country_code_copy(char* dst, const char* src) { for (unsigned int i = 0; i < 2; i++) { dst[i] = src[i]; diff --git a/src/python/location-exporter.in b/src/python/location-exporter.in index 1e3be26..12bc29a 100644 --- a/src/python/location-exporter.in +++ b/src/python/location-exporter.in @@ -280,12 +280,12 @@ class CLI(object): asns.append(object) - elif not len(object) == 2: - log.error("Invalid argument: %s" % object) - return 2 + elif location.country_code_is_valid(object): + countries.append(object) else: - countries.append(object) + log.error("Invalid argument: %s" % object) + return 2 # Open the database try: diff --git a/src/python/locationmodule.c b/src/python/locationmodule.c index 78cf075..6e7269b 100644 --- a/src/python/locationmodule.c +++ b/src/python/locationmodule.c @@ -65,7 +65,25 @@ static PyObject* discover_latest_version(PyObject* m, PyObject* args) { return PyLong_FromUnsignedLong(t); } +static PyObject* country_code_is_valid(PyObject* m, PyObject* args) { + const char* country_code = NULL; + + if (!PyArg_ParseTuple(args, "s", &country_code)) + return NULL; + + if (loc_country_code_is_valid(country_code)) + Py_RETURN_TRUE; + + Py_RETURN_FALSE; +} + static PyMethodDef location_module_methods[] = { + { + "country_code_is_valid", + (PyCFunction)country_code_is_valid, + METH_VARARGS, + NULL, + }, { "discover_latest_version", (PyCFunction)discover_latest_version, -- 2.39.2