From 8530d77c68cf772cd5d3455574c7973232df846c Mon Sep 17 00:00:00 2001
From: Paul Smith <psmith@gnu.org>
Date: Sun, 28 Feb 2016 21:32:18 -0500
Subject: [PATCH] * strcache.c (add_string): [SV 47071] Handle huge initial
 string.

If the very first string added to the string cache is more than
half the maximum size, we failed when moving the only strcache
buffer to the full list.
---
 strcache.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/strcache.c b/strcache.c
index ec59eeb7..ec0f0a57 100644
--- a/strcache.c
+++ b/strcache.c
@@ -110,13 +110,13 @@ add_string (const char *str, unsigned int len)
   for (; *spp != NULL; spp = &(*spp)->next)
     if ((*spp)->bytesfree > sz)
       break;
+  sp = *spp;
 
   /* If nothing is big enough, make a new cache at the front.  */
-  sp = *spp;
   if (sp == NULL)
     {
       sp = new_cache (&strcache, BUFSIZE);
-      spp = &sp;
+      spp = &strcache;
     }
 
   /* Add the string to this cache.  */
@@ -124,9 +124,9 @@ add_string (const char *str, unsigned int len)
 
   /* If the amount free in this cache is less than the average string size,
      consider it full and move it to the full list.  */
-  if (sp->bytesfree < (total_size / total_strings) + 1)
+  if (total_strings > 20 && sp->bytesfree < (total_size / total_strings) + 1)
     {
-      *spp = (*spp)->next;
+      *spp = sp->next;
       sp->next = fullcache;
       fullcache = sp;
     }