[geocode-glib/wip/wjt/30-nominatim-soup3] nominatim: Validate response as UTF-8



commit eda1578540c9105e9bf55e77ddb639710cc97d55
Author: Will Thompson <wjt endlessos org>
Date:   Sat Aug 6 20:37:31 2022 +0100

    nominatim: Validate response as UTF-8
    
    Previously, the bytes read from the server's response were assumed to be
    valid UTF-8.  Particularly since we are copying it anyway, validate it
    at the same time.
    
    (In practice this is probably a non-issue because it just ends up
    getting passed to json_parser_load_from_data() which validates its input
    as UTF-8 anyway.)

 geocode-glib/geocode-nominatim.c  |  8 ++++----
 geocode-glib/tests/geocode-glib.c | 20 +++++++++++++++++++-
 2 files changed, 23 insertions(+), 5 deletions(-)
---
diff --git a/geocode-glib/geocode-nominatim.c b/geocode-glib/geocode-nominatim.c
index d18e71f..4f62460 100644
--- a/geocode-glib/geocode-nominatim.c
+++ b/geocode-glib/geocode-nominatim.c
@@ -910,7 +910,7 @@ on_query_data_loaded (GObject      *object,
        } else {
                gsize size = 0;
                gconstpointer data = g_bytes_get_data (body, &size);
-               gchar *contents = g_strndup (data, size);
+               gchar *contents = g_utf8_make_valid (data, size);
 
                _geocode_glib_cache_save (query, contents);
                g_task_return_pointer (task, contents, g_free);
@@ -933,7 +933,7 @@ on_query_data_loaded (SoupSession *session,
                                         "%s",
                                         query->reason_phrase ? query->reason_phrase : "Query failed");
        else {
-               contents = g_strndup (query->response_body->data, query->response_body->length);
+               contents = g_utf8_make_valid (query->response_body->data, query->response_body->length);
                _geocode_glib_cache_save (query, contents);
                g_task_return_pointer (task, contents, g_free);
        }
@@ -1074,7 +1074,7 @@ geocode_nominatim_query (GeocodeNominatim  *self,
                } else {
                        gsize size = 0;
                        gconstpointer data = g_bytes_get_data (body, &size);
-                       contents = g_strndup (data, size);
+                       contents = g_utf8_make_valid (data, size);
 
                        _geocode_glib_cache_save (soup_query, contents);
                }
@@ -1084,7 +1084,7 @@ geocode_nominatim_query (GeocodeNominatim  *self,
                                             soup_query->reason_phrase ? soup_query->reason_phrase : "Query 
failed");
                        contents = NULL;
                } else {
-                       contents = g_strndup (soup_query->response_body->data, 
soup_query->response_body->length);
+                       contents = g_utf8_make_valid (soup_query->response_body->data, 
soup_query->response_body->length);
                        _geocode_glib_cache_save (soup_query, contents);
                }
 #endif
diff --git a/geocode-glib/tests/geocode-glib.c b/geocode-glib/tests/geocode-glib.c
index 48cb708..fe81e06 100644
--- a/geocode-glib/tests/geocode-glib.c
+++ b/geocode-glib/tests/geocode-glib.c
@@ -80,6 +80,7 @@ got_geocode_cb (GObject *source_object,
        exit (0);
 }
 
+#if 0
 static void
 got_geocode_search_cb (GObject *source_object,
                       GAsyncResult *res,
@@ -109,6 +110,7 @@ got_geocode_search_cb (GObject *source_object,
 
        exit (0);
 }
+#endif
 
 static gboolean
 bbox_includes_location (GeocodeBoundingBox *bbox,
@@ -984,11 +986,27 @@ int main (int argc, char **argv)
 
        if (do_rev_geocoding == FALSE) {
                GeocodeForward *forward;
+               GError *error = NULL;
 
                forward = geocode_forward_new_for_string (command_line_params[0]);
                if (answer_count != DEFAULT_ANSWER_COUNT)
                        geocode_forward_set_answer_count (forward, answer_count);
-               geocode_forward_search_async (forward, NULL, got_geocode_search_cb, NULL);
+               GList *results = geocode_forward_search (forward, &error);
+       if (results == NULL) {
+               g_message ("Failed to search geocode: %s", error->message);
+               g_error_free (error);
+               exit (1);
+       }
+
+       GList *l;
+       for (l = results; l != NULL; l = l->next) {
+               GeocodePlace *place = l->data;
+
+               g_print ("Got geocode search answer:\n");
+               print_place (place);
+               g_object_unref (place);
+       }
+               //geocode_forward_search_async (forward, NULL, got_geocode_search_cb, NULL);
        } else {
                GeocodeReverse *reverse;
                GeocodeLocation *loc;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]