Merge branch 'gstring-free-warning' into 'main'

Make g_string_free (_, FALSE) warn on unused result

See merge request GNOME/glib!3226
This commit is contained in:
Marco Trevisan 2023-02-01 16:28:02 +00:00
commit 155e44652e
8 changed files with 53 additions and 21 deletions

View File

@ -709,8 +709,8 @@ key_state_serialise (KeyState *state)
gsize size;
gsize i;
data = state->strinfo->str;
size = state->strinfo->len;
data = g_string_free_and_steal (g_steal_pointer (&state->strinfo));
words = data;
for (i = 0; i < size / sizeof (guint32); i++)
@ -720,9 +720,6 @@ key_state_serialise (KeyState *state)
data, size, TRUE,
g_free, data);
g_string_free (state->strinfo, FALSE);
state->strinfo = NULL;
g_variant_builder_add (&builder, "(y@au)",
state->is_flags ? 'f' :
state->is_enum ? 'e' : 'c',

View File

@ -263,14 +263,12 @@ test_pollable_unix_file (void)
static void
test_pollable_unix_nulldev (void)
{
int fd;
g_test_summary ("Test that /dev/null is not considered pollable, but only if "
"on a system where we are able to tell it apart from devices "
"that actually implement poll");
#if defined (HAVE_EPOLL_CREATE) || defined (HAVE_KQUEUE)
fd = g_open ("/dev/null", O_RDWR, 0);
int fd = g_open ("/dev/null", O_RDWR, 0);
g_assert_cmpint (fd, !=, -1);
g_assert_not_pollable (fd);

View File

@ -3546,10 +3546,7 @@ g_key_file_get_key_comment (GKeyFile *key_file,
}
if (string != NULL)
{
comment = string->str;
g_string_free (string, FALSE);
}
comment = g_string_free_and_steal (g_steal_pointer (&string));
else
comment = NULL;

View File

@ -206,8 +206,8 @@ g_string_new_len (const gchar *init,
* (i.e. %NULL if @free_segment is %TRUE)
*/
gchar *
g_string_free (GString *string,
gboolean free_segment)
(g_string_free) (GString *string,
gboolean free_segment)
{
gchar *segment;
@ -242,7 +242,7 @@ g_string_free (GString *string,
gchar *
g_string_free_and_steal (GString *string)
{
return g_string_free (string, FALSE);
return (g_string_free) (string, FALSE);
}
/**

View File

@ -57,10 +57,23 @@ GString* g_string_new_len (const gchar *init,
GLIB_AVAILABLE_IN_ALL
GString* g_string_sized_new (gsize dfl_size);
GLIB_AVAILABLE_IN_ALL
gchar* g_string_free (GString *string,
gchar* (g_string_free) (GString *string,
gboolean free_segment);
GLIB_AVAILABLE_IN_2_76
gchar* g_string_free_and_steal (GString *string) G_GNUC_WARN_UNUSED_RESULT;
#if G_GNUC_CHECK_VERSION (2, 0) && (GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_76)
#define g_string_free(str, free_segment) \
(__builtin_constant_p (free_segment) ? \
((free_segment) ? \
(g_string_free) ((str), (free_segment)) : \
g_string_free_and_steal (str)) \
: \
(g_string_free) ((str), (free_segment)))
#endif /* G_GNUC_CHECK_VERSION (2, 0) && (GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_76) */
GLIB_AVAILABLE_IN_2_34
GBytes* g_string_free_to_bytes (GString *string);
GLIB_AVAILABLE_IN_ALL

View File

@ -21,6 +21,7 @@
#include "config.h"
#include <stdint.h>
/* gwakeup.c is special -- GIO and some test cases include it. As such,
* it cannot include other glib headers without triggering the single

View File

@ -472,6 +472,35 @@ test_string_append (void)
g_string_free (string, TRUE);
}
static void
test_string_free (void)
{
GString *str;
gchar *data;
g_test_message ("Test that g_string_free() macro compiles and doesnt "
"cause any compiler warnings in C++ mode");
/* Test that g_string_free (_, TRUE) does not cause a warning if
* its return value is unused. */
str = g_string_new ("test");
g_string_free (str, TRUE);
/* Test that g_string_free (_, FALSE) does not emit a warning if
* its return value is used. */
str = g_string_new ("test");
data = g_string_free (str, FALSE);
g_free (data);
/* Test that g_string_free () with an expression that is always FALSE
* at runtime, but the compiler can't know it, does not cause any
* warnings if its return value is unused. */
str = g_string_new ("test");
data = str->str;
g_string_free (str, g_test_get_path ()[0] == 0);
g_free (data);
}
int
main (int argc, char *argv[])
{
@ -502,6 +531,7 @@ main (int argc, char *argv[])
g_test_add_func ("/C++/str-has-suffix", test_str_has_suffix);
g_test_add_func ("/C++/str-has-suffix/macro", test_str_has_suffix_macro);
g_test_add_func ("/C++/string-append", test_string_append);
g_test_add_func ("/C++/string-free", test_string_free);
return g_test_run ();
}

View File

@ -83,9 +83,7 @@ subparser_end (GMarkupParseContext *ctx,
char *result;
string = g_markup_parse_context_pop (ctx);
result = string->str;
g_string_free (string, FALSE);
result = g_string_free_and_steal (g_steal_pointer (&string));
strings_allocated--;
if (result == NULL || result[0] == '\0')
@ -156,9 +154,7 @@ replay_parser_end (GMarkupParseContext *ctx,
return NULL;
}
result = string->str;
g_string_free (string, FALSE);
result = g_string_free_and_steal (g_steal_pointer (&string));
strings_allocated--;
if (result == NULL || result[0] == '\0')