From 0618f5eb82efc3233e8a592975f452f3db3f162e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Fri, 2 Sep 2022 21:31:34 +0200 Subject: [PATCH] g_strsplit: Use a pre-allocated GArray when max_tokens is provided In case max_tokens is provided, we can safely pre-allocate the GArray to the max_tokens value plus one for the NULL terminating value. --- glib/gstrfuncs.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c index 0386e8e45..ee934e5d7 100644 --- a/glib/gstrfuncs.c +++ b/glib/gstrfuncs.c @@ -2425,9 +2425,15 @@ g_strsplit (const gchar *string, g_return_val_if_fail (delimiter[0] != '\0', NULL); if (max_tokens < 1) - max_tokens = G_MAXINT; + { + max_tokens = G_MAXINT; + string_list = g_ptr_array_new (); + } + else + { + string_list = g_ptr_array_new_full (max_tokens + 1, NULL); + } - string_list = g_ptr_array_new (); remainder = string; s = strstr (remainder, delimiter); if (s)