Merge branch 'gstrfuncs_param_check' into 'main'

gstrfuncs: Check parameter validity

See merge request GNOME/glib!4740
This commit is contained in:
Marco Trevisan
2025-08-12 10:46:01 +02:00
2 changed files with 24 additions and 0 deletions

View File

@@ -416,6 +416,8 @@ g_strndup (const gchar *str,
if (str) if (str)
{ {
g_return_val_if_fail (n < G_MAXSIZE, NULL);
new_str = g_new (gchar, n + 1); new_str = g_new (gchar, n + 1);
strncpy (new_str, str, n); strncpy (new_str, str, n);
new_str[n] = '\0'; new_str[n] = '\0';
@@ -441,6 +443,8 @@ g_strnfill (gsize length,
{ {
gchar *str; gchar *str;
g_return_val_if_fail (length < G_MAXSIZE, NULL);
str = g_new (gchar, length + 1); str = g_new (gchar, length + 1);
memset (str, (guchar)fill_char, length); memset (str, (guchar)fill_char, length);
str[length] = '\0'; str[length] = '\0';

View File

@@ -571,6 +571,16 @@ test_strndup (void)
g_assert_nonnull (str); g_assert_nonnull (str);
g_assert_cmpstr (str, ==, "aa"); g_assert_cmpstr (str, ==, "aa");
g_free (str); g_free (str);
if (g_test_undefined ())
{
/* Testing degenerated cases */
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"*assertion* < G_MAXSIZE*");
g_assert_null (
g_strndup ("aaaa", G_MAXSIZE));
g_test_assert_expected_messages ();
}
} }
/* Testing g_strdup_printf() function with various positive and negative cases */ /* Testing g_strdup_printf() function with various positive and negative cases */
@@ -616,6 +626,16 @@ test_strnfill (void)
g_assert_nonnull (str); g_assert_nonnull (str);
g_assert_cmpstr (str, ==, "aaaaa"); g_assert_cmpstr (str, ==, "aaaaa");
g_free (str); g_free (str);
if (g_test_undefined ())
{
/* Testing degenerated cases */
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"*assertion* < G_MAXSIZE*");
g_assert_null (
g_strnfill (G_MAXSIZE, 'a'));
g_test_assert_expected_messages ();
}
} }
/* Testing g_strconcat() function with various positive and negative cases */ /* Testing g_strconcat() function with various positive and negative cases */