guri: Preallocate a buffer for building URIs

Rather than reallocating the string buffer a few times as more
components are added, use a default buffer size which should hopefully
accommodate most average URIs.

The buffer size is a guess and can be tweaked in future.

This has the advantage of no longer passing a potentially-`NULL`
`scheme` to `g_string_new()`, which should placate the static analysers,
which think that `g_string_new()` shouldn’t accept `NULL`.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Coverity CID: #1474691
This commit is contained in:
Philip Withnall 2022-05-05 13:38:44 +01:00
parent 0c6a1af9d6
commit 17f608e382

View File

@ -1633,9 +1633,17 @@ g_uri_join_internal (GUriFlags flags,
g_return_val_if_fail (host == NULL || (path[0] == '\0' || path[0] == '/'), NULL);
g_return_val_if_fail (host != NULL || (path[0] != '/' || path[1] != '/'), NULL);
str = g_string_new (scheme);
/* Arbitrarily chosen default size which should handle most average length
* URIs. This should avoid a few reallocations of the buffer in most cases.
* Its 1B shorter than a power of two, since GString will add a
* nul-terminator byte. */
str = g_string_sized_new (127);
if (scheme)
g_string_append_c (str, ':');
{
g_string_append (str, scheme);
g_string_append_c (str, ':');
}
if (flags & G_URI_FLAGS_SCHEME_NORMALIZE && scheme && ((host && port != -1) || path[0] == '\0'))
normalized_scheme = g_ascii_strdown (scheme, -1);