This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 2/2] Fix PR gdb/15326 infinite recursion when printing Fortran types


From: Frank Penczek <frank.penczek@intel.com>

[PATCH PR/15326] The name of a user-defined data type in Fortran is
stored as "tag_name" in the "main_type" struct. When printing out
type information via "f_type_print_base" the "show" argument passed
to the recursive call of "f_type_print_base" is correctly set 0
when following a pointer, however, the check at the beginning of the
function only checks if "type_name" is non-NULL: in the case of a
user-defined data-type the name is stored as "tag_name" with a NULL
"type_name", which causes the check to fail. The consequence is an
infinite recursion when printing a recursively nested user-defined
type. Fixed by extending the check to also inspect "tag_name".

2013-09-24  Frank Penczek  <frank.penczek@intel.com>
	    Christoph Weinmann  <christoph.t.weinmann@intel.com>

	* f-typeprint.c (f_type_print_base): Include TYPE_TAG_NAME
	in conditional that ends recursive descend.

testsuite/
	* gdb.fortran/type-with-ptr.f90: Add Fortran example
	to test recursion caused by pointers in user-defined types.
	* gdb.fortran/type-with-ptr.exp: Add test case for
	recursion.

Change-Id: I0b7e346562d6264370156469acaf0fcbd47b5f5c
Signed-off-by: Frank Penczek <frank.penczek@intel.com>
---
 gdb/f-typeprint.c                           |   19 ++++++++---
 gdb/testsuite/gdb.fortran/type-with-ptr.exp |   45 +++++++++++++++++++++++++++
 gdb/testsuite/gdb.fortran/type-with-ptr.f90 |   34 ++++++++++++++++++++
 3 files changed, 93 insertions(+), 5 deletions(-)
 create mode 100644 gdb/testsuite/gdb.fortran/type-with-ptr.exp
 create mode 100644 gdb/testsuite/gdb.fortran/type-with-ptr.f90

diff --git a/gdb/f-typeprint.c b/gdb/f-typeprint.c
index 71fe869..3be5911 100644
--- a/gdb/f-typeprint.c
+++ b/gdb/f-typeprint.c
@@ -266,13 +266,22 @@ f_type_print_base (struct type *type, struct ui_file *stream, int show,
       return;
     }
 
-  /* When SHOW is zero or less, and there is a valid type name, then always
-     just print the type name directly from the type.  */
+  /* If SHOW is zero or less, and there is a valid type name or tag name
+     (tags hold names of user-defined types), then always just print the name
+     directly without any further recursive application of this function.  */
 
-  if ((show <= 0) && (TYPE_NAME (type) != NULL))
+  if (show <= 0)
     {
-      fputs_filtered (TYPE_NAME (type), stream);
-      return;
+      const char *name_to_print = TYPE_NAME (type);
+
+      if (name_to_print == NULL)
+          name_to_print = TYPE_TAG_NAME (type);
+
+      if (name_to_print != NULL)
+        {
+          fputs_filtered (name_to_print, stream);
+          return;
+        }
     }
 
   if (TYPE_CODE (type) != TYPE_CODE_TYPEDEF)
diff --git a/gdb/testsuite/gdb.fortran/type-with-ptr.exp b/gdb/testsuite/gdb.fortran/type-with-ptr.exp
new file mode 100644
index 0000000..bb54282
--- /dev/null
+++ b/gdb/testsuite/gdb.fortran/type-with-ptr.exp
@@ -0,0 +1,45 @@
+# Copyright 2013 Free Software Foundation, Inc.
+#
+# Contributed by Intel Corp.<christoph.t.weinmann@intel.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+if {[skip_fortran_tests]} {
+    return -1
+}
+
+standard_testfile .f90
+
+if {[prepare_for_testing $testfile.exp $testfile $srcfile {debug f90}]} {
+    return -1
+}
+
+if {![runto MAIN__]} then {
+    perror "couldn't run to breakpoint MAIN__"
+    continue
+}
+
+# Depending on the compiler version being used, the name of the 4-byte integer
+# can be printed differently.  For instance, gfortran-4.1 uses "int4" whereas
+# gfortran-4.3 uses "int(kind=4)".
+set int4 "(int4|integer\\(kind=4\\))"
+
+gdb_breakpoint [gdb_get_line_number "BP1"]
+gdb_continue_to_breakpoint "BP1"
+
+# Check for recursion when printing pointers in user-defined types
+gdb_test "ptype tinsta" \
+         ".*\r\n *${int4} :: i\r\n *PTR TO \-\> \\( tuserdef :: ptr\\)\r\n.*" \
+         "Print only the type with pointer, not types pointed-to by the pointer."
+
diff --git a/gdb/testsuite/gdb.fortran/type-with-ptr.f90 b/gdb/testsuite/gdb.fortran/type-with-ptr.f90
new file mode 100644
index 0000000..47f0ab8
--- /dev/null
+++ b/gdb/testsuite/gdb.fortran/type-with-ptr.f90
@@ -0,0 +1,34 @@
+! Test program for printing user-defined types with pointers.
+!
+! Copyright 2013 Free Software Foundation, Inc.
+!
+! Contributed by Intel Corp. <christoph.t.weinmann@intel.com>
+!
+! This program is free software; you can redistribute it and/or modify
+! it under the terms of the GNU General Public License as published by
+! the Free Software Foundation; either version 3 of the License, or
+! (at your option) any later version.
+!
+! This program is distributed in the hope that it will be useful,
+! but WITHOUT ANY WARRANTY; without even the implied warranty of
+! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+! GNU General Public License for more details.
+!
+! You should have received a copy of the GNU General Public License
+! along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+program main
+  implicit none
+
+  type tuserdef
+    integer :: i
+    type (tuserdef), pointer :: ptr
+  end type tuserdef
+
+  type(tuserdef), target:: tinsta,tinstb
+
+  tinsta%ptr => tinstb
+  tinstb%ptr => tinsta                      !BP1
+
+end program main
+
-- 
1.7.0.7


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]