diff options
author | Will Thompson <wjt@endlessos.org> | 2022-08-06 13:50:20 +0100 |
---|---|---|
committer | Will Thompson <wjt@endlessos.org> | 2022-08-06 13:50:20 +0100 |
commit | 72285d203963c118a05f810016b584210663ae80 (patch) | |
tree | c7469e5b7a22943e86eeee39b26272cca7e9515f | |
parent | c670fb9afe886f26448a8a4654a3b52a90386c1f (diff) | |
download | geocode-glib-72285d203963c118a05f810016b584210663ae80.tar.gz |
nominatim: Correctly read response data from soup3
Previously, the soup3 branch would call g_bytes_unref_to_data() on the
response data, with a NULL out parameter for the size, and then proceed
to use the data pointer as a NULL-terminated string. This is wrong for
two reasons:
- g_bytes_unref_to_data()'s size parameter may not be NULL
- The response data is not NULL-terminated
Instead, do what the soup2 branch does: use g_strndup() to get a
NULL-terminated string.
No automatic test, but the async branch at least can be tested by
running the test binary with an argument:
_build/geocode-glib/tests/geocode-glib aoeuaoeuaoeu
Note that you must change the search term each time, since the result is
cached.
Fixes: https://gitlab.gnome.org/GNOME/geocode-glib/-/issues/30
-rw-r--r-- | geocode-glib/geocode-nominatim.c | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/geocode-glib/geocode-nominatim.c b/geocode-glib/geocode-nominatim.c index 2a458da..d18e71f 100644 --- a/geocode-glib/geocode-nominatim.c +++ b/geocode-glib/geocode-nominatim.c @@ -890,9 +890,8 @@ on_query_data_loaded (GObject *object, SoupSession *session = SOUP_SESSION (object); SoupMessage *query = soup_session_get_async_result_message (session, result); GError *error = NULL; - GBytes *body = soup_session_send_and_read_finish (session, result, &error); + g_autoptr(GBytes) body = soup_session_send_and_read_finish (session, result, &error); GTask *task = user_data; - char *contents; if (!body) { g_task_return_new_error (task, @@ -903,14 +902,16 @@ on_query_data_loaded (GObject *object, g_clear_error (&error); } else if (soup_message_get_status (query) != SOUP_STATUS_OK) { const char *reason_phrase = soup_message_get_reason_phrase (query); - g_bytes_unref (body); g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "%s", reason_phrase ? reason_phrase : "Query failed"); } else { - contents = g_bytes_unref_to_data (body, NULL); + gsize size = 0; + gconstpointer data = g_bytes_get_data (body, &size); + gchar *contents = g_strndup (data, size); + _geocode_glib_cache_save (query, contents); g_task_return_pointer (task, contents, g_free); } @@ -1059,7 +1060,7 @@ geocode_nominatim_query (GeocodeNominatim *self, if (_geocode_glib_cache_load (soup_query, &contents) == FALSE) { #if SOUP_CHECK_VERSION (2, 99, 2) GError *serror = NULL; - GBytes *body = soup_session_send_and_read (soup_session, soup_query, NULL, &serror); + g_autoptr(GBytes) body = soup_session_send_and_read (soup_session, soup_query, NULL, &serror); if (!body) { g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, serror->message); @@ -1067,12 +1068,14 @@ geocode_nominatim_query (GeocodeNominatim *self, contents = NULL; } else if (soup_message_get_status (soup_query) != SOUP_STATUS_OK) { const char *reason_phrase = soup_message_get_reason_phrase (soup_query); - g_bytes_unref (body); g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, reason_phrase ? reason_phrase : "Query failed"); contents = NULL; } else { - contents = g_bytes_unref_to_data (body, NULL); + gsize size = 0; + gconstpointer data = g_bytes_get_data (body, &size); + contents = g_strndup (data, size); + _geocode_glib_cache_save (soup_query, contents); } #else |