Blame SOURCES/binutils-CVE-2019-9071.patch

0c74e2
--- binutils.orig/libiberty/cp-demangle.c	2019-04-10 10:31:27.854997707 +0100
0c74e2
+++ binutils-2.31.1/libiberty/cp-demangle.c	2019-04-10 16:00:35.820350978 +0100
0c74e2
@@ -858,7 +858,7 @@ CP_STATIC_IF_GLIBCPP_V3
0c74e2
 int
0c74e2
 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
0c74e2
 {
0c74e2
-  if (p == NULL || s == NULL || len == 0)
0c74e2
+  if (p == NULL || s == NULL || len <= 0)
0c74e2
     return 0;
0c74e2
   p->d_printing = 0;
0c74e2
   p->type = DEMANGLE_COMPONENT_NAME;
0c74e2
@@ -4032,7 +4032,7 @@ d_growable_string_callback_adapter (cons
0c74e2
    are larger than the actual numbers encountered.  */
0c74e2
 
0c74e2
 static void
0c74e2
-d_count_templates_scopes (int *num_templates, int *num_scopes,
0c74e2
+d_count_templates_scopes (struct d_print_info *dpi,
0c74e2
 			  const struct demangle_component *dc)
0c74e2
 {
0c74e2
   if (dc == NULL)
0c74e2
@@ -4052,13 +4052,13 @@ d_count_templates_scopes (int *num_templ
0c74e2
       break;
0c74e2
 
0c74e2
     case DEMANGLE_COMPONENT_TEMPLATE:
0c74e2
-      (*num_templates)++;
0c74e2
+      dpi->num_copy_templates++;
0c74e2
       goto recurse_left_right;
0c74e2
 
0c74e2
     case DEMANGLE_COMPONENT_REFERENCE:
0c74e2
     case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
0c74e2
       if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
0c74e2
-	(*num_scopes)++;
0c74e2
+	dpi->num_saved_scopes++;
0c74e2
       goto recurse_left_right;
0c74e2
 
0c74e2
     case DEMANGLE_COMPONENT_QUAL_NAME:
0c74e2
@@ -4122,42 +4122,42 @@ d_count_templates_scopes (int *num_templ
0c74e2
     case DEMANGLE_COMPONENT_TAGGED_NAME:
0c74e2
     case DEMANGLE_COMPONENT_CLONE:
0c74e2
     recurse_left_right:
0c74e2
-      d_count_templates_scopes (num_templates, num_scopes,
0c74e2
-				d_left (dc));
0c74e2
-      d_count_templates_scopes (num_templates, num_scopes,
0c74e2
-				d_right (dc));
0c74e2
+      /* PR 89394 - Check for too much recursion.  */
0c74e2
+      if (dpi->recursion > DEMANGLE_RECURSION_LIMIT)
0c74e2
+	/* FIXME: There ought to be a way to report to the
0c74e2
+	   user that the recursion limit has been reached.  */
0c74e2
+	return;
0c74e2
+
0c74e2
+      ++ dpi->recursion;
0c74e2
+      d_count_templates_scopes (dpi, d_left (dc));
0c74e2
+      d_count_templates_scopes (dpi, d_right (dc));
0c74e2
+      -- dpi->recursion;
0c74e2
       break;
0c74e2
 
0c74e2
     case DEMANGLE_COMPONENT_CTOR:
0c74e2
-      d_count_templates_scopes (num_templates, num_scopes,
0c74e2
-				dc->u.s_ctor.name);
0c74e2
+      d_count_templates_scopes (dpi, dc->u.s_ctor.name);
0c74e2
       break;
0c74e2
 
0c74e2
     case DEMANGLE_COMPONENT_DTOR:
0c74e2
-      d_count_templates_scopes (num_templates, num_scopes,
0c74e2
-				dc->u.s_dtor.name);
0c74e2
+      d_count_templates_scopes (dpi, dc->u.s_dtor.name);
0c74e2
       break;
0c74e2
 
0c74e2
     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
0c74e2
-      d_count_templates_scopes (num_templates, num_scopes,
0c74e2
-				dc->u.s_extended_operator.name);
0c74e2
+      d_count_templates_scopes (dpi, dc->u.s_extended_operator.name);
0c74e2
       break;
0c74e2
 
0c74e2
     case DEMANGLE_COMPONENT_FIXED_TYPE:
0c74e2
-      d_count_templates_scopes (num_templates, num_scopes,
0c74e2
-                                dc->u.s_fixed.length);
0c74e2
+      d_count_templates_scopes (dpi, dc->u.s_fixed.length);
0c74e2
       break;
0c74e2
 
0c74e2
     case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
0c74e2
     case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
0c74e2
-      d_count_templates_scopes (num_templates, num_scopes,
0c74e2
-				d_left (dc));
0c74e2
+      d_count_templates_scopes (dpi, d_left (dc));
0c74e2
       break;
0c74e2
 
0c74e2
     case DEMANGLE_COMPONENT_LAMBDA:
0c74e2
     case DEMANGLE_COMPONENT_DEFAULT_ARG:
0c74e2
-      d_count_templates_scopes (num_templates, num_scopes,
0c74e2
-				dc->u.s_unary_num.sub);
0c74e2
+      d_count_templates_scopes (dpi, dc->u.s_unary_num.sub);
0c74e2
       break;
0c74e2
     }
0c74e2
 }
0c74e2
@@ -4192,8 +4192,12 @@ d_print_init (struct d_print_info *dpi,
0c74e2
   dpi->next_copy_template = 0;
0c74e2
   dpi->num_copy_templates = 0;
0c74e2
 
0c74e2
-  d_count_templates_scopes (&dpi->num_copy_templates,
0c74e2
-			    &dpi->num_saved_scopes, dc);
0c74e2
+  d_count_templates_scopes (dpi, dc);
0c74e2
+  /* If we did not reach the recursion limit, then reset the
0c74e2
+     current recursion value back to 0, so that we can print
0c74e2
+     the templates.  */
0c74e2
+  if (dpi->recursion < DEMANGLE_RECURSION_LIMIT)
0c74e2
+    dpi->recursion = 0;
0c74e2
   dpi->num_copy_templates *= dpi->num_saved_scopes;
0c74e2
 
0c74e2
   dpi->current_template = NULL;