From ca56ca94ee32839cb0f474e4dfb6e0bd33b0abb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20R=C3=BChsen?= Date: Fri, 14 Feb 2020 16:00:05 +0100 Subject: [PATCH] * src/convert.c (local_quote_string): Remove use of alloca --- src/convert.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/convert.c b/src/convert.c index 1a768bc9..1fa1fc56 100644 --- a/src/convert.c +++ b/src/convert.c @@ -734,7 +734,9 @@ static char * local_quote_string (const char *file, bool no_html_quote) { const char *from; - char *newname, *to; + char *newname, *to, *res; + char buf[1024]; + size_t tolen; char *any = strpbrk (file, "?#%;"); if (!any) @@ -742,8 +744,12 @@ local_quote_string (const char *file, bool no_html_quote) /* Allocate space assuming the worst-case scenario, each character having to be quoted. */ - to = newname = (char *)alloca (3 * strlen (file) + 1); - newname[0] = '\0'; + tolen = 3 * strlen (file); + if (tolen < sizeof (buf)) + to = newname = buf; + else + to = newname = xmalloc (tolen + 1); + for (from = file; *from; from++) switch (*from) { @@ -776,7 +782,15 @@ local_quote_string (const char *file, bool no_html_quote) } *to = '\0'; - return no_html_quote ? strdup (newname) : html_quote_string (newname); + if (newname == buf) + return no_html_quote ? strdup (newname) : html_quote_string (newname); + + if (no_html_quote) + return newname; + + res = html_quote_string (newname); + xfree (newname); + return res; } /* Book-keeping code for dl_file_url_map, dl_url_file_map,