Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Hannes Domani via Gdb-patches <gdb-patches@sourceware.org>
To: gdb-patches@sourceware.org
Subject: [PATCH v2] Don't compare types of enum fields
Date: Thu, 17 Dec 2020 20:29:12 +0100	[thread overview]
Message-ID: <20201217192912.1981-1-ssbssa@yahoo.de> (raw)
In-Reply-To: <20201217192912.1981-1-ssbssa.ref@yahoo.de>

Comparing types of enum fields results in a crash, because they don't
have a type.

It can be reproduced by comparing the types of 2 instances of the same
enum type in different objects:

enum.h:
enum e
{
  zero,
  one,
};

enum-1.c:
int func();
enum e e1;
int main()
{
  return e1 + func();
}

enum-2.c:
enum e e2;
int func()
{
  return e2;
}

$ gcc -g -oenum enum-1.c enum-2.c
$ gdb -q enum.exe
Reading symbols from enum.exe...
(gdb) py print(gdb.parse_and_eval("e1").type==gdb.parse_and_eval("e2").type)

Thread 1 received signal SIGSEGV, Segmentation fault.
[Switching to Thread 6184.0x1cc4]
check_typedef (type=0x0) at C:/src/repos/binutils-gdb.git/gdb/gdbtypes.c:2745
2745      while (type->code () == TYPE_CODE_TYPEDEF)

gdb/ChangeLog:

2020-12-17  Hannes Domani  <ssbssa@yahoo.de>

	PR exp/27070
	* gdbtypes.c (check_types_equal): Don't compare types of enum fields.

gdb/testsuite/ChangeLog:

2020-12-17  Hannes Domani  <ssbssa@yahoo.de>

	PR exp/27070
	* gdb.base/pr27070-a.c: New test.
	* gdb.base/pr27070-b.c: New test.
	* gdb.base/pr27070.exp: New file.
	* gdb.base/pr27070.h: New test.
---
v2:
- Add detailed problem description in commit message and test case.
---
 gdb/gdbtypes.c                     |  4 +++-
 gdb/testsuite/gdb.base/pr27070-a.c | 27 +++++++++++++++++++++++++++
 gdb/testsuite/gdb.base/pr27070-b.c | 25 +++++++++++++++++++++++++
 gdb/testsuite/gdb.base/pr27070.exp | 29 +++++++++++++++++++++++++++++
 gdb/testsuite/gdb.base/pr27070.h   | 22 ++++++++++++++++++++++
 5 files changed, 106 insertions(+), 1 deletion(-)
 create mode 100644 gdb/testsuite/gdb.base/pr27070-a.c
 create mode 100644 gdb/testsuite/gdb.base/pr27070-b.c
 create mode 100644 gdb/testsuite/gdb.base/pr27070.exp
 create mode 100644 gdb/testsuite/gdb.base/pr27070.h

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index a40ae5f30e..2207613eef 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -4036,7 +4036,9 @@ check_types_equal (struct type *type1, struct type *type2,
 	    case FIELD_LOC_KIND_ENUMVAL:
 	      if (FIELD_ENUMVAL (*field1) != FIELD_ENUMVAL (*field2))
 		return false;
-	      break;
+	      /* Don't compare types of enum fields, because they don't
+		 have a type.  */
+	      continue;
 	    case FIELD_LOC_KIND_PHYSADDR:
 	      if (FIELD_STATIC_PHYSADDR (*field1)
 		  != FIELD_STATIC_PHYSADDR (*field2))
diff --git a/gdb/testsuite/gdb.base/pr27070-a.c b/gdb/testsuite/gdb.base/pr27070-a.c
new file mode 100644
index 0000000000..f3850803ea
--- /dev/null
+++ b/gdb/testsuite/gdb.base/pr27070-a.c
@@ -0,0 +1,27 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 Free Software Foundation, Inc.
+
+   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/>.  */
+
+#include "pr27070.h"
+
+int func();
+
+enum e e1;
+
+int main()
+{
+  return e1 + func();
+}
diff --git a/gdb/testsuite/gdb.base/pr27070-b.c b/gdb/testsuite/gdb.base/pr27070-b.c
new file mode 100644
index 0000000000..39962c671a
--- /dev/null
+++ b/gdb/testsuite/gdb.base/pr27070-b.c
@@ -0,0 +1,25 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 Free Software Foundation, Inc.
+
+   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/>.  */
+
+#include "pr27070.h"
+
+enum e e2;
+
+int func()
+{
+  return e2;
+}
diff --git a/gdb/testsuite/gdb.base/pr27070.exp b/gdb/testsuite/gdb.base/pr27070.exp
new file mode 100644
index 0000000000..3f565da661
--- /dev/null
+++ b/gdb/testsuite/gdb.base/pr27070.exp
@@ -0,0 +1,29 @@
+# Copyright 2020 Free Software Foundation, Inc.
+
+# 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/>.
+
+set testname pr27070
+set sources "pr27070-a.c pr27070-b.c"
+
+if {[build_executable ${testname}.exp $testname $sources {debug}] == -1} {
+    return -1
+}
+
+# Start with a fresh gdb.
+
+clean_restart ${testname}
+
+if { [skip_python_tests] } { continue }
+
+gdb_test "py print(gdb.parse_and_eval('e1').type == gdb.parse_and_eval('e2').type)" "True"
diff --git a/gdb/testsuite/gdb.base/pr27070.h b/gdb/testsuite/gdb.base/pr27070.h
new file mode 100644
index 0000000000..f149fc1a7e
--- /dev/null
+++ b/gdb/testsuite/gdb.base/pr27070.h
@@ -0,0 +1,22 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 Free Software Foundation, Inc.
+
+   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/>.  */
+
+enum e
+{
+  zero,
+  one,
+};
-- 
2.29.2


       reply	other threads:[~2020-12-17 19:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20201217192912.1981-1-ssbssa.ref@yahoo.de>
2020-12-17 19:29 ` Hannes Domani via Gdb-patches [this message]
2020-12-18 20:30   ` Tom Tromey
2020-12-18 21:27   ` Simon Marchi via Gdb-patches
2020-12-18 22:00     ` Hannes Domani via Gdb-patches
2020-12-18 22:14       ` Simon Marchi via Gdb-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201217192912.1981-1-ssbssa@yahoo.de \
    --to=gdb-patches@sourceware.org \
    --cc=ssbssa@yahoo.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox