From aded15c9d10dbf3ec218e25793e3467240585511 Mon Sep 17 00:00:00 2001 From: Ravi Sankar Guntur Date: Mon, 19 Mar 2012 21:17:32 +0530 Subject: [PATCH] glib: fix memory leaks in gutils, protocol, and strfuncs tests https://bugzilla.gnome.org/show_bug.cgi?id=672329 Signed-off-by: Ravi Sankar Guntur --- glib/gutils.c | 26 ++++++++++++++++++++------ glib/tests/protocol.c | 6 +++++- glib/tests/strfuncs.c | 42 +++++++++++++++++++++++++++++++++--------- 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/glib/gutils.c b/glib/gutils.c index 4992ecc04..7765d5ace 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -660,19 +660,32 @@ g_get_any_init_do (void) gchar hostname[100]; g_tmp_dir = g_strdup (g_getenv ("TMPDIR")); + if (g_tmp_dir == NULL || *g_tmp_dir == '\0') - g_tmp_dir = g_strdup (g_getenv ("TMP")); + { + g_free (g_tmp_dir); + g_tmp_dir = g_strdup (g_getenv ("TMP")); + } + if (g_tmp_dir == NULL || *g_tmp_dir == '\0') - g_tmp_dir = g_strdup (g_getenv ("TEMP")); + { + g_free (g_tmp_dir); + g_tmp_dir = g_strdup (g_getenv ("TEMP")); + } #ifdef G_OS_WIN32 if (g_tmp_dir == NULL || *g_tmp_dir == '\0') - g_tmp_dir = get_windows_directory_root (); -#else + { + g_free (g_tmp_dir); + g_tmp_dir = get_windows_directory_root (); + } +#else + #ifdef P_tmpdir if (g_tmp_dir == NULL || *g_tmp_dir == '\0') { - gsize k; + gsize k; + g_free (g_tmp_dir); g_tmp_dir = g_strdup (P_tmpdir); k = strlen (g_tmp_dir); if (k > 1 && G_IS_DIR_SEPARATOR (g_tmp_dir[k - 1])) @@ -682,7 +695,8 @@ g_get_any_init_do (void) if (g_tmp_dir == NULL || *g_tmp_dir == '\0') { - g_tmp_dir = g_strdup ("/tmp"); + g_free (g_tmp_dir); + g_tmp_dir = g_strdup (g_getenv ("/tmp")); } #endif /* !G_OS_WIN32 */ diff --git a/glib/tests/protocol.c b/glib/tests/protocol.c index c40d2c0b9..6aff1c13c 100644 --- a/glib/tests/protocol.c +++ b/glib/tests/protocol.c @@ -90,7 +90,7 @@ test_message_cb1 (GIOChannel * channel, GIOStatus status; guchar buf[512]; gsize read_bytes = 0; - + g_assert_cmpuint (condition, ==, G_IO_IN); for (status = g_io_channel_read_chars (channel, (gchar*)buf, sizeof (buf), &read_bytes, NULL); @@ -214,6 +214,7 @@ test_message (void) default: g_error ("unexpected log message type: %s", g_test_log_type_name (msg->log_type)); } + g_test_log_msg_free (msg); } g_assert_cmpint (passed, ==, 3); @@ -221,6 +222,7 @@ test_message (void) g_free (argv[1]); g_main_loop_unref (loop); + g_test_log_buffer_free (tlb); } static void @@ -319,10 +321,12 @@ test_error (void) default: g_error ("unexpected log message type: %s", g_test_log_type_name (msg->log_type)); } + g_test_log_msg_free (msg); } g_free (argv[1]); g_main_loop_unref (loop); + g_test_log_buffer_free (tlb); } g_assert_cmpint (messages, ==, 3); diff --git a/glib/tests/strfuncs.c b/glib/tests/strfuncs.c index 358bc58d5..9b2d1d652 100644 --- a/glib/tests/strfuncs.c +++ b/glib/tests/strfuncs.c @@ -1122,12 +1122,21 @@ test_bounds (void) g_strrstr_len (string, 4096, "."); g_strrstr_len (string, 4096, ""); - g_ascii_strdown (string, 4096); - g_ascii_strdown (string, 4096); - g_ascii_strup (string, 4096); - g_ascii_strup (string, 4096); + tmp = g_ascii_strup (string, 4096); + tmp2 = g_ascii_strup (tmp, 4096); + g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0); + g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0); + g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0); + g_free (tmp); + g_free (tmp2); - g_ascii_strncasecmp (string, string, 4096); + tmp = g_ascii_strdown (string, 4096); + tmp2 = g_ascii_strdown (tmp, 4096); + g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0); + g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0); + g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0); + g_free (tmp); + g_free (tmp2); tmp = g_markup_escape_text (string, 4096); g_free (tmp); @@ -1180,10 +1189,25 @@ test_bounds (void) g_assert_cmpint (strlen (tmp), ==, 4095 + 2); g_free (tmp); - g_ascii_strdown (string, -1); - g_ascii_strdown (string, -1); - g_ascii_strup (string, -1); - g_ascii_strup (string, -1); + tmp = g_ascii_strdown (string, -1); + tmp2 = g_ascii_strdown (tmp, -1); + g_assert_cmpint (strlen(tmp), ==, strlen(tmp2)); + g_assert_cmpint (strlen(string), ==, strlen(tmp)); + g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0); + g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0); + g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0); + g_free (tmp); + g_free (tmp2); + + tmp = g_ascii_strup (string, -1); + tmp2 = g_ascii_strup (string, -1); + g_assert_cmpint (strlen(tmp), ==, strlen(tmp2)); + g_assert_cmpint (strlen(string), ==, strlen(tmp)); + g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0); + g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0); + g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0); + g_free (tmp); + g_free (tmp2); g_ascii_strcasecmp (string, string); g_ascii_strncasecmp (string, string, 10000);