From 589b3743fa929bbca2deef6d44994f1c0434b023 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim=20R=C3=BChsen?= <tim.ruehsen@gmx.de>
Date: Fri, 14 Feb 2020 12:49:46 +0100
Subject: [PATCH] * src/cookies.c (cookie_header): Remove use of alloca

---
 src/cookies.c | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)

diff --git a/src/cookies.c b/src/cookies.c
index 2c966765..97d12b10 100644
--- a/src/cookies.c
+++ b/src/cookies.c
@@ -728,17 +728,6 @@ check_path_match (const char *cookie_path, const char *path)
   return path_matches (path, cookie_path) != 0;
 }
 
-/* Prepend '/' to string S.  S is copied to fresh stack-allocated
-   space and its value is modified to point to the new location.  */
-
-#define PREPEND_SLASH(s) do {                                   \
-  char *PS_newstr = (char *) alloca (1 + strlen (s) + 1);       \
-  *PS_newstr = '/';                                             \
-  strcpy (PS_newstr + 1, s);                                    \
-  s = PS_newstr;                                                \
-} while (0)
-
-
 /* Process the HTTP `Set-Cookie' header.  This results in storing the
    cookie or discarding a matching one, or ignoring it completely, all
    depending on the contents.  */
@@ -1065,7 +1054,7 @@ char *
 cookie_header (struct cookie_jar *jar, const char *host,
                int port, const char *path, bool secflag)
 {
-  struct cookie **chains;
+  struct cookie *chains[32];
   int chain_count;
 
   struct cookie *cookie;
@@ -1079,8 +1068,10 @@ cookie_header (struct cookie_jar *jar, const char *host,
 
   /* Allocate room for find_chains_of_host to write to.  The number of
      chains can at most equal the number of subdomains, hence
-     1+<number of dots>.  */
-  chains = alloca_array (struct cookie *, 1 + count_char (host, '.'));
+     1+<number of dots>.  We ignore cookies with more than 32 labels. */
+  chain_count = 1 + count_char (host, '.');
+  if (chain_count > (int) countof (chains))
+    return NULL;
   chain_count = find_chains_of_host (jar, host, chains);
 
   /* No cookies for this host. */