PR fortran/95828 - Buffer overflows with SELECT RANK
authorHarald Anlauf <anlauf@gmx.de>
Thu, 25 Jun 2020 18:34:48 +0000 (20:34 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Thu, 25 Jun 2020 18:34:48 +0000 (20:34 +0200)
With SELECT RANK, name mangling results in long internal symbols that
overflows internal buffers.  Fix that.

gcc/fortran/
PR fortran/95828
* match.c (select_rank_set_tmp): Enlarge internal buffer used in
generating a mangled name.
* resolve.c (resolve_select_rank): Likewise.

gcc/fortran/match.c
gcc/fortran/resolve.c
gcc/testsuite/gfortran.dg/pr95828.f90 [new file with mode: 0644]

index 8063fcad295a32a77b0b590c5ba75035f398195d..b011634792e5c6b00d5bce5f29835c8b4ac19f7d 100644 (file)
@@ -6496,7 +6496,7 @@ static void
 select_rank_set_tmp (gfc_typespec *ts, int *case_value)
 {
   char name[2 * GFC_MAX_SYMBOL_LEN];
-  char tname[GFC_MAX_SYMBOL_LEN];
+  char tname[GFC_MAX_SYMBOL_LEN + 7];
   gfc_symtree *tmp;
   gfc_symbol *selector = select_type_stack->selector;
   gfc_symbol *sym;
index 8c602daf1cb45bd8dcc2eaf490982b9eda81ea67..6fa34caec5499ff514df9e93a8ffa22092b589d1 100644 (file)
@@ -9635,7 +9635,7 @@ resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
   gfc_namespace *ns;
   gfc_code *body, *new_st, *tail;
   gfc_case *c;
-  char tname[GFC_MAX_SYMBOL_LEN];
+  char tname[GFC_MAX_SYMBOL_LEN + 7];
   char name[2 * GFC_MAX_SYMBOL_LEN];
   gfc_symtree *st;
   gfc_expr *selector_expr = NULL;
diff --git a/gcc/testsuite/gfortran.dg/pr95828.f90 b/gcc/testsuite/gfortran.dg/pr95828.f90
new file mode 100644 (file)
index 0000000..e85b2f1
--- /dev/null
@@ -0,0 +1,21 @@
+! { dg-do compile }
+! { dg-options "-fsecond-underscore" }
+! PR fortran/95828 - ICE in resolve_select_rank, at fortran/resolve.c:9774
+
+module m2345678901234567890123456789012345678901234567890123456789_123
+  type t2345678901234567890123456789012345678901234567890123456789_123
+  end type
+contains
+  subroutine s2345678901234567890123456789012345678901234567890123456789_123 &
+            (x2345678901234567890123456789012345678901234567890123456789_123)
+    type    (t2345678901234567890123456789012345678901234567890123456789_123) :: &
+             x2345678901234567890123456789012345678901234567890123456789_123(..)
+
+    select rank (y2345678901234567890123456789012345678901234567890123456789_123 &
+              => x2345678901234567890123456789012345678901234567890123456789_123)
+    rank (2)
+    rank (3)
+    rank default
+    end select
+  end
+end