From 62a8ac3240acf74c2be336920820a92cf7bf86c3 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 13 Apr 2023 13:03:01 +0100 Subject: tests: Fix warnings about unused results from g_string_free() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GLib 2.76 emits a warning if `g_string_free (_, FALSE)` is used without using its return value, as it typically signifies a leak: ``` warning: ignoring return value of ‘g_string_free_and_steal’ declared with attribute ‘warn_unused_result’ [-Wunused-result] ``` In this case, the libsoup example and test code did *not* leak because it had already stolen the string data from `GString`. That’s a bit of a layering violation (the string data briefly has two owners), so rearrange the code to fix that and silence the warning. Signed-off-by: Philip Withnall --- examples/simple-httpd.c | 7 +++++-- tests/forms-test.c | 14 ++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/examples/simple-httpd.c b/examples/simple-httpd.c index ac2c2e54..10afda65 100644 --- a/examples/simple-httpd.c +++ b/examples/simple-httpd.c @@ -88,6 +88,8 @@ do_get (SoupServer *server, SoupServerMessage *msg, const char *path) if (g_file_test (path, G_FILE_TEST_IS_DIR)) { GString *listing; char *index_path; + char *listing_str; + gsize listing_len; slash = strrchr (path, '/'); if (!slash || slash[1]) { @@ -109,11 +111,12 @@ do_get (SoupServer *server, SoupServerMessage *msg, const char *path) g_free (index_path); listing = get_directory_listing (path); + listing_len = listing->len; + listing_str = g_string_free (g_steal_pointer (&listing), FALSE); soup_server_message_set_response (msg, "text/html", SOUP_MEMORY_TAKE, - listing->str, listing->len); + g_steal_pointer (&listing_str), listing_len); soup_server_message_set_status (msg, SOUP_STATUS_OK, NULL); - g_string_free (listing, FALSE); return; } diff --git a/tests/forms-test.c b/tests/forms-test.c index 201ad454..a1af7b30 100644 --- a/tests/forms-test.c +++ b/tests/forms-test.c @@ -311,6 +311,8 @@ hello_callback (SoupServer *server, const char *content_type; GString *buf; const char *method; + char *buf_str; + gsize buf_len; method = soup_server_message_get_method (msg); if (method != SOUP_METHOD_GET && method != SOUP_METHOD_HEAD) { @@ -351,10 +353,11 @@ hello_callback (SoupServer *server, } } + buf_len = buf->len; + buf_str = g_string_free (g_steal_pointer (&buf), FALSE); soup_server_message_set_response (msg, content_type, SOUP_MEMORY_TAKE, - buf->str, buf->len); - g_string_free (buf, FALSE); + g_steal_pointer (&buf_str), buf_len); soup_server_message_set_status (msg, SOUP_STATUS_OK, NULL); } @@ -368,6 +371,8 @@ md5_get_callback (SoupServer *server, const char *file = NULL, *md5sum = NULL, *fmt; const char *content_type; GString *buf; + char *buf_str; + gsize buf_len; if (query) { file = g_hash_table_lookup (query, "file"); @@ -397,10 +402,11 @@ md5_get_callback (SoupServer *server, g_string_append_printf (buf, "%s", md5sum); } + buf_len = buf->len; + buf_str = g_string_free (g_steal_pointer (&buf), FALSE); soup_server_message_set_response (msg, content_type, SOUP_MEMORY_TAKE, - buf->str, buf->len); - g_string_free (buf, FALSE); + g_steal_pointer (&buf_str), buf_len); soup_server_message_set_status (msg, SOUP_STATUS_OK, NULL); } -- cgit v1.2.1