58725c
diff -rup binutils.orig/include/demangle.h binutils-2.27/include/demangle.h
58725c
--- binutils.orig/include/demangle.h	2019-01-08 13:52:32.990714670 +0000
58725c
+++ binutils-2.27/include/demangle.h	2019-01-08 13:58:42.269743403 +0000
58725c
@@ -64,9 +64,20 @@ extern "C" {
58725c
 #define DMGL_GNAT	 (1 << 15)
58725c
 #define DMGL_DLANG	 (1 << 16)
58725c
 
58725c
+/* Disable a limit on the depth of recursion in mangled strings.
58725c
+   Note if this limit is disabled then stack exhaustion is possible when
58725c
+   demangling pathologically complicated strings.  Bug reports about stack
58725c
+   exhaustion when the option is enabled will be rejected.  */  
58725c
+#define DMGL_NO_RECURSE_LIMIT (1 << 17)	
58725c
+
58725c
 /* If none of these are set, use 'current_demangling_style' as the default. */
58725c
 #define DMGL_STYLE_MASK (DMGL_AUTO|DMGL_GNU|DMGL_LUCID|DMGL_ARM|DMGL_HP|DMGL_EDG|DMGL_GNU_V3|DMGL_JAVA|DMGL_GNAT|DMGL_DLANG)
58725c
 
58725c
+/* If DMGL_NO_RECURSE_LIMIT is not enabled, then this is the value used as
58725c
+   the maximum depth of recursion allowed.  It should be enough for any
58725c
+   real-world mangled name.  */
58725c
+#define DEMANGLE_RECURSION_LIMIT 2048
58725c
+  
58725c
 /* Enumeration of possible demangling styles.
58725c
 
58725c
    Lucid and ARM styles are still kept logically distinct, even though
58725c
Only in binutils-2.27/include: demangle.h.orig
58725c
Only in binutils-2.27/include: #demangle.h.rej#
58725c
Only in binutils-2.27/include: .#demangle.h.rej
58725c
Only in binutils-2.27/include: demangle.h.rej
58725c
diff -rup binutils.orig/libiberty/cp-demangle.c binutils-2.27/libiberty/cp-demangle.c
58725c
--- binutils.orig/libiberty/cp-demangle.c	2019-01-08 13:52:33.161713294 +0000
58725c
+++ binutils-2.27/libiberty/cp-demangle.c	2019-01-08 13:53:30.002255949 +0000
58725c
@@ -2725,21 +2725,35 @@ d_ref_qualifier (struct d_info *di, stru
58725c
 static struct demangle_component *
58725c
 d_function_type (struct d_info *di)
58725c
 {
58725c
-  struct demangle_component *ret;
58725c
+  struct demangle_component *ret = NULL;
58725c
 
58725c
-  if (! d_check_char (di, 'F'))
58725c
-    return NULL;
58725c
-  if (d_peek_char (di) == 'Y')
58725c
+  if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
58725c
     {
58725c
-      /* Function has C linkage.  We don't print this information.
58725c
-	 FIXME: We should print it in verbose mode.  */
58725c
-      d_advance (di, 1);
58725c
+      if (di->recursion_level > DEMANGLE_RECURSION_LIMIT)
58725c
+	/* FIXME: There ought to be a way to report
58725c
+	   that the recursion limit has been reached.  */
58725c
+	return NULL;
58725c
+
58725c
+      di->recursion_level ++;
58725c
     }
58725c
-  ret = d_bare_function_type (di, 1);
58725c
-  ret = d_ref_qualifier (di, ret);
58725c
 
58725c
-  if (! d_check_char (di, 'E'))
58725c
-    return NULL;
58725c
+  if (d_check_char (di, 'F'))
58725c
+    {
58725c
+      if (d_peek_char (di) == 'Y')
58725c
+	{
58725c
+	  /* Function has C linkage.  We don't print this information.
58725c
+	     FIXME: We should print it in verbose mode.  */
58725c
+	  d_advance (di, 1);
58725c
+	}
58725c
+      ret = d_bare_function_type (di, 1);
58725c
+      ret = d_ref_qualifier (di, ret);
58725c
+      
58725c
+      if (! d_check_char (di, 'E'))
58725c
+	ret = NULL;
58725c
+    }
58725c
+
58725c
+  if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
58725c
+    di->recursion_level --;
58725c
   return ret;
58725c
 }
58725c
 
58725c
@@ -5898,6 +5912,7 @@ cplus_demangle_init_info (const char *ma
58725c
   di->expansion = 0;
58725c
   di->is_expression = 0;
58725c
   di->is_conversion = 0;
58725c
+  di->recursion_level = 0;
58725c
 }
58725c
 
58725c
 /* Internal implementation for the demangler.  If MANGLED is a g++ v3 ABI
58725c
@@ -5937,6 +5952,20 @@ d_demangle_callback (const char *mangled
58725c
 
58725c
   cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
58725c
 
58725c
+  /* PR 87675 - Check for a mangled string that is so long
58725c
+     that we do not have enough stack space to demangle it.  */
58725c
+  if (((options & DMGL_NO_RECURSE_LIMIT) == 0)
58725c
+      /* This check is a bit arbitrary, since what we really want to do is to
58725c
+	 compare the sizes of the di.comps and di.subs arrays against the
58725c
+	 amount of stack space remaining.  But there is no portable way to do
58725c
+	 this, so instead we use the recursion limit as a guide to the maximum
58725c
+	 size of the arrays.  */
58725c
+      && (unsigned long) di.num_comps > DEMANGLE_RECURSION_LIMIT)
58725c
+    {
58725c
+      /* FIXME: We need a way to indicate that a stack limit has been reached.  */
58725c
+      return 0;
58725c
+    }
58725c
+
58725c
   {
58725c
 #ifdef CP_DYNAMIC_ARRAYS
58725c
     __extension__ struct demangle_component comps[di.num_comps];
58725c
Only in binutils-2.27/libiberty: cp-demangle.c.orig
58725c
diff -rup binutils.orig/libiberty/cp-demangle.h binutils-2.27/libiberty/cp-demangle.h
58725c
--- binutils.orig/libiberty/cp-demangle.h	2019-01-08 13:52:33.161713294 +0000
58725c
+++ binutils-2.27/libiberty/cp-demangle.h	2019-01-08 13:53:30.003255941 +0000
58725c
@@ -127,6 +127,9 @@ struct d_info
58725c
   /* Non-zero if we are parsing the type operand of a conversion
58725c
      operator, but not when in an expression.  */
58725c
   int is_conversion;
58725c
+  /* If DMGL_NO_RECURSE_LIMIT is not active then this is set to
58725c
+     the current recursion level.  */
58725c
+  unsigned int recursion_level;
58725c
 };
58725c
 
58725c
 /* To avoid running past the ending '\0', don't:
58725c
Only in binutils-2.27/libiberty: cp-demangle.h.orig
58725c
diff -rup binutils.orig/libiberty/cplus-dem.c binutils-2.27/libiberty/cplus-dem.c
58725c
--- binutils.orig/libiberty/cplus-dem.c	2019-01-08 13:52:33.161713294 +0000
58725c
+++ binutils-2.27/libiberty/cplus-dem.c	2019-01-08 14:10:00.057340821 +0000
58725c
@@ -137,6 +137,7 @@ struct work_stuff
58725c
   string* previous_argument; /* The last function argument demangled.  */
58725c
   int nrepeats;         /* The number of times to repeat the previous
58725c
 			   argument.  */
58725c
+  unsigned int recursion_level;
58725c
 };
58725c
 
58725c
 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
58725c
@@ -1237,11 +1238,15 @@ squangle_mop_up (struct work_stuff *work
58725c
     {
58725c
       free ((char *) work -> btypevec);
58725c
       work->btypevec = NULL;
58725c
+      work->bsize = 0;
58725c
+      work->numb = 0;
58725c
     }
58725c
   if (work -> ktypevec != NULL)
58725c
     {
58725c
       free ((char *) work -> ktypevec);
58725c
       work->ktypevec = NULL;
58725c
+      work->ksize = 0;
58725c
+      work->numk = 0;
58725c
     }
58725c
 }
58725c
 
58725c
@@ -1275,8 +1280,15 @@ work_stuff_copy_to_from (struct work_stu
58725c
 
58725c
   for (i = 0; i < from->numk; i++)
58725c
     {
58725c
-      int len = strlen (from->ktypevec[i]) + 1;
58725c
+      int len;
58725c
 
58725c
+      if (from->ktypevec[i] == NULL)
58725c
+	{
58725c
+	  to->ktypevec[i] = NULL;
58725c
+	  continue;
58725c
+	}
58725c
+
58725c
+      len = strlen (from->ktypevec[i]) + 1;
58725c
       to->ktypevec[i] = XNEWVEC (char, len);
58725c
       memcpy (to->ktypevec[i], from->ktypevec[i], len);
58725c
     }
58725c
@@ -1286,8 +1298,15 @@ work_stuff_copy_to_from (struct work_stu
58725c
 
58725c
   for (i = 0; i < from->numb; i++)
58725c
     {
58725c
-      int len = strlen (from->btypevec[i]) + 1;
58725c
+      int len;
58725c
 
58725c
+      if (from->btypevec[i] == NULL)
58725c
+	{
58725c
+	  to->btypevec[i] = NULL;
58725c
+	  continue;
58725c
+	}
58725c
+
58725c
+      len = strlen (from->btypevec[i]) + 1;
58725c
       to->btypevec[i] = XNEWVEC (char , len);
58725c
       memcpy (to->btypevec[i], from->btypevec[i], len);
58725c
     }
58725c
@@ -1335,6 +1354,7 @@ delete_non_B_K_work_stuff (struct work_s
58725c
 
58725c
       free ((char*) work->tmpl_argvec);
58725c
       work->tmpl_argvec = NULL;
58725c
+      work->ntmpl_args = 0;
58725c
     }
58725c
   if (work->previous_argument)
58725c
     {
58725c
@@ -3347,6 +3367,20 @@ demangle_qualified (struct work_stuff *w
58725c
       success = 0;
58725c
     }
58725c
 
58725c
+  if ((work->options & DMGL_NO_RECURSE_LIMIT) == 0)
58725c
+    {
58725c
+      /* PR 87241: Catch malicious input that will try to trick this code into
58725c
+	 allocating a ridiculous amount of memory via the remember_Ktype()
58725c
+	 function.
58725c
+	 The choice of DEMANGLE_RECURSION_LIMIT is somewhat arbitrary.  Possibly
58725c
+	 a better solution would be to track how much memory remember_Ktype
58725c
+	 allocates and abort when some upper limit is reached.  */
58725c
+      if (qualifiers > DEMANGLE_RECURSION_LIMIT)
58725c
+	/* FIXME: We ought to have some way to tell the user that
58725c
+	   this limit has been reached.  */
58725c
+	success = 0;
58725c
+    }
58725c
+
58725c
   if (!success)
58725c
     return success;
58725c
 
58725c
@@ -4335,6 +4369,7 @@ remember_Btype (struct work_stuff *work,
58725c
 }
58725c
 
58725c
 /* Lose all the info related to B and K type codes. */
58725c
+
58725c
 static void
58725c
 forget_B_and_K_types (struct work_stuff *work)
58725c
 {
58725c
@@ -4360,6 +4395,7 @@ forget_B_and_K_types (struct work_stuff
58725c
 	}
58725c
     }
58725c
 }
58725c
+
58725c
 /* Forget the remembered types, but not the type vector itself.  */
58725c
 
58725c
 static void
58725c
@@ -4551,6 +4587,16 @@ demangle_nested_args (struct work_stuff
58725c
   int result;
58725c
   int saved_nrepeats;
58725c
 
58725c
+  if ((work->options & DMGL_NO_RECURSE_LIMIT) == 0)
58725c
+    {
58725c
+      if (work->recursion_level > DEMANGLE_RECURSION_LIMIT)
58725c
+	/* FIXME: There ought to be a way to report
58725c
+	   that the recursion limit has been reached.  */
58725c
+	return 0;
58725c
+
58725c
+      work->recursion_level ++;
58725c
+    }
58725c
+
58725c
   /* The G++ name-mangling algorithm does not remember types on nested
58725c
      argument lists, unless -fsquangling is used, and in that case the
58725c
      type vector updated by remember_type is not used.  So, we turn
58725c
@@ -4577,6 +4623,9 @@ demangle_nested_args (struct work_stuff
58725c
   --work->forgetting_types;
58725c
   work->nrepeats = saved_nrepeats;
58725c
 
58725c
+  if ((work->options & DMGL_NO_RECURSE_LIMIT) == 0)
58725c
+    --work->recursion_level;
58725c
+
58725c
   return result;
58725c
 }
58725c
 
58725c
Only in binutils-2.27/libiberty: cplus-dem.c.orig
58725c
Only in binutils-2.27/libiberty: cplus-dem.c.rej