public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Sergio Durigan Junior <sergiodj@redhat.com>
To: GDB Patches <gdb-patches@sourceware.org>
Cc: Eli Zaretskii <eliz@gnu.org>,	Pedro Alves <palves@redhat.com>,
	Sergio Durigan Junior <sergiodj@redhat.com>
Subject: [PATCH] Fix segfault when using 'set print object on' + whatis <struct>
Date: Sat, 20 Jan 2018 01:03:00 -0000	[thread overview]
Message-ID: <20180120010334.7694-1-sergiodj@redhat.com> (raw)
In-Reply-To: <20180116203239.27787-1-sergiodj@redhat.com>

This problem was hidden behind a "maybe-uninitialized" warning
generated when compiling GDB with a recent GCC.  The warning is:

  ../../gdb/typeprint.c: In function 'void whatis_exp(const char*, int)':
  ../../gdb/typeprint.c:515:12: warning: 'val' may be used uninitialized in this function [-Wmaybe-uninitialized]
    real_type = value_rtti_type (val, &full, &top, &using_enc);
    ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I submitted a patch fixing this by initializing "val" to NULL, but it
was the wrong fix, as Pedro pointed out on
<https://sourceware.org/ml/gdb-patches/2018-01/msg00346.html>:

  (gdb) set print object on
  (gdb) whatis some_structure_type

  Thread 1 "gdb" received signal SIGSEGV, Segmentation fault.
  0x00000000005dda90 in check_typedef (type=0x6120736573756170) at src/gdb/gdbtypes.c:2388
  2388      int instance_flags = TYPE_INSTANCE_FLAGS (type);
  ...

So I set off to find the cause of the problem.  It turns out that a
recent-ish refactoring of the code on 'whatis_exp', introduced by:

  commit c973d0aa4a2c737ab527ae44a617f1c357e07364
  Date:   Mon Aug 21 11:34:32 2017 +0100

      Fix type casts losing typedefs and reimplement "whatis" typedef stripping

was the reason of the failure.  After investigating what 'set print
object on' was supposed to do to the output of 'whatis', if made sense
initialize "val = evaluate_type (expr.get ());" all the time, not only
when we're dealing with the 'ptype' command.

I've regtested this on the BuildBot, without seeing any regressions.
I've also extended 'gdb.base/whatis.exp' to check if the segfault is
not there anymore.

gdb/ChangeLog:
yyyy-mm-dd  Sergio Durigan Junior  <sergiodj@redhat.com>

	* typeprint.c (whatis_exp): Move initialization of "val"
	outside of "if".

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Sergio Durigan Junior  <sergiodj@redhat.com>

	* gdb.base/whatis.exp: Test that 'set print object on' +
	'whatis <struct>' doesn't segfault.
---
 gdb/testsuite/gdb.base/whatis.exp | 7 +++++++
 gdb/typeprint.c                   | 3 ++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/gdb/testsuite/gdb.base/whatis.exp b/gdb/testsuite/gdb.base/whatis.exp
index dd6aeb02f9..8207dab59c 100644
--- a/gdb/testsuite/gdb.base/whatis.exp
+++ b/gdb/testsuite/gdb.base/whatis.exp
@@ -566,3 +566,10 @@ gdb_test "whatis int (*)(void, int, int)" \
 gdb_test "whatis int (*)(int, void, int)" \
     "'void' invalid as parameter type" \
     "whatis applied to function with 'void' parameter type"
+
+# Test that 'set print object on' + whatis doesn't segfault.
+clean_restart $binfile
+gdb_test_no_output "set print object on"
+gdb_test "whatis v_struct1" \
+    "type = struct t_struct" \
+    "whatis + set print object on doesn't segfault"
diff --git a/gdb/typeprint.c b/gdb/typeprint.c
index 9a125076a1..dd6e75bd4f 100644
--- a/gdb/typeprint.c
+++ b/gdb/typeprint.c
@@ -471,6 +471,8 @@ whatis_exp (const char *exp, int show)
 
       expression_up expr = parse_expression (exp);
 
+      val = evaluate_type (expr.get ());
+
       /* The behavior of "whatis" depends on whether the user
 	 expression names a type directly, or a language expression
 	 (including variable names).  If the former, then "whatis"
@@ -495,7 +497,6 @@ whatis_exp (const char *exp, int show)
 	  /* The user expression names a type indirectly by naming an
 	     object or expression of that type.  Find that
 	     indirectly-named type.  */
-	  val = evaluate_type (expr.get ());
 	  type = value_type (val);
 	}
     }
-- 
2.14.3

  parent reply	other threads:[~2018-01-20  1:03 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <announce.20180105041805.3FC35808E9@joel.gnat.com>
2018-01-16 17:31 ` [ANNOUNCEMENT] GDB 8.1 release branch created! Eli Zaretskii
2018-01-16 19:02   ` Sergio Durigan Junior
2018-01-16 19:46     ` [PATCH] Fix warning on gdb/compile/compile.c (C++-ify "triplet_rx") Sergio Durigan Junior
2018-01-17 15:33       ` Eli Zaretskii
2018-01-17 17:17       ` Simon Marchi
2018-01-17 23:07         ` Sergio Durigan Junior
2018-01-17 23:42           ` Simon Marchi
2018-01-17 23:48             ` Sergio Durigan Junior
2018-01-16 20:32     ` [PATCH] Fix unitialized warning on gdb/typeprint.c:whatis_exp Sergio Durigan Junior
2018-01-17 15:34       ` Eli Zaretskii
2018-01-17 16:48       ` Pedro Alves
2018-01-17 18:03         ` Sergio Durigan Junior
2018-01-20  1:03       ` Sergio Durigan Junior [this message]
2018-01-22 17:42         ` [PATCH v2] Fix segfault when using 'set print object on' + whatis <struct> (Re: [PATCH] Fix segfault when using 'set print object on' + whatis <struct>) Pedro Alves
2018-01-22 18:04           ` Sergio Durigan Junior
2018-01-22 19:53             ` Pedro Alves
2018-01-22 20:11               ` Sergio Durigan Junior
2018-01-16 20:36     ` [ANNOUNCEMENT] GDB 8.1 release branch created! Sergio Durigan Junior
2018-01-17  3:36       ` Eli Zaretskii
2018-01-17 16:46         ` Sergio Durigan Junior
2018-01-17 11:04       ` Pedro Alves
2018-01-17 16:38         ` Sergio Durigan Junior
2018-01-17 16:46           ` Eli Zaretskii
2018-01-17 16:50             ` Pedro Alves
2018-01-17 18:21               ` Eli Zaretskii
2018-01-18 15:53   ` Eli Zaretskii
2018-01-25 16:58     ` Eli Zaretskii
2018-01-26 14:18       ` Eli Zaretskii
2018-01-26 15:37         ` Simon Marchi
2018-01-26 18:53           ` Eli Zaretskii
2018-01-27 16:42             ` Eli Zaretskii
2018-02-01 15:12               ` Yao Qi
2018-02-01 16:27                 ` Eli Zaretskii
2018-02-01 16:51                   ` Yao Qi
2018-02-01 17:33                     ` Eli Zaretskii
2018-02-01 21:32                       ` Yao Qi
2018-02-02 15:23                         ` Eli Zaretskii
2018-02-02 15:53                           ` Joel Brobecker
2018-02-02 16:27                             ` Simon Marchi
2018-02-02 17:42                             ` Joseph Myers

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=20180120010334.7694-1-sergiodj@redhat.com \
    --to=sergiodj@redhat.com \
    --cc=eliz@gnu.org \
    --cc=gdb-patches@sourceware.org \
    --cc=palves@redhat.com \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).