From patchwork Wed Oct 31 18:18:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 29984 Received: (qmail 98281 invoked by alias); 31 Oct 2018 18:18:29 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 98271 invoked by uid 89); 31 Oct 2018 18:18:28 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=fd X-HELO: gateway34.websitewelcome.com Received: from gateway34.websitewelcome.com (HELO gateway34.websitewelcome.com) (192.185.148.214) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 31 Oct 2018 18:18:26 +0000 Received: from cm11.websitewelcome.com (cm11.websitewelcome.com [100.42.49.5]) by gateway34.websitewelcome.com (Postfix) with ESMTP id F07455A583F for ; Wed, 31 Oct 2018 13:18:24 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id Hv4Sgme0fRPojHv4Sg0Whi; Wed, 31 Oct 2018 13:18:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version :Content-Type:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=VDGYmAUNPBniEd4OuOlfd5bHPpfqABjFgf6gf1w5iQ0=; b=laqG2HYTLZ8A6lL/0vfR9PmL6h 9/IJgYe/e9REgHr6QMEOb7K2jAsOi8E3YM+i4kLfCjvbWWQ9LGCp4P3YylNeH9JinmKqJIYoQ3ZqC CrS06NEJBKtF6/aJurHm0d3R2; Received: from 97-122-190-66.hlrn.qwest.net ([97.122.190.66]:46780 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1gHv4S-004LgK-Nn; Wed, 31 Oct 2018 13:18:24 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2] Avoid crash when calling warning too early Date: Wed, 31 Oct 2018 12:18:20 -0600 Message-Id: <20181031181820.24308-1-tom@tromey.com> I noticed that if you pass the name of an existing file (not a directory) as the argument to --data-directory, gdb will crash: $ ./gdb -nx --data-directory ./gdb ../../binutils-gdb/gdb/target.c:590:56: runtime error: member call on null pointer of type 'struct target_ops' This was later reported as PR gdb/23838. This happens because warning ends up calling target_supports_terminal_ours, which calls current_top_target, which returns nullptr this early. This fixes the problem by handling this case specially in target_supports_terminal_ours. I also changed target_supports_terminal_ours to return bool. gdb/ChangeLog 2018-10-31 Tom Tromey PR gdb/23838: * target.h (target_supports_terminal_ours): Return bool. * target.c (target_supports_terminal_ours): Handle case where current_top_target returns nullptr. Return bool. gdb/testsuite/ChangeLog 2018-10-31 Tom Tromey PR gdb/23838: * gdb.base/warning.exp: New file. --- gdb/ChangeLog | 7 ++++++ gdb/target.c | 10 +++++++-- gdb/target.h | 2 +- gdb/testsuite/ChangeLog | 5 +++++ gdb/testsuite/gdb.base/warning.exp | 36 ++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 gdb/testsuite/gdb.base/warning.exp diff --git a/gdb/target.c b/gdb/target.c index 2d98954b54..9404025104 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -584,10 +584,16 @@ target_terminal::info (const char *arg, int from_tty) /* See target.h. */ -int +bool target_supports_terminal_ours (void) { - return current_top_target ()->supports_terminal_ours (); + /* This can be called before there is any target, so we must check + for nullptr here. */ + target_ops *top = current_top_target (); + + if (top == nullptr) + return false; + return top->supports_terminal_ours (); } static void diff --git a/gdb/target.h b/gdb/target.h index a3000c80c6..6f4b73bf00 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -1577,7 +1577,7 @@ extern int target_remove_breakpoint (struct gdbarch *gdbarch, /* Return true if the target stack has a non-default "terminal_ours" method. */ -extern int target_supports_terminal_ours (void); +extern bool target_supports_terminal_ours (void); /* Kill the inferior process. Make it go away. */ diff --git a/gdb/testsuite/gdb.base/warning.exp b/gdb/testsuite/gdb.base/warning.exp new file mode 100644 index 0000000000..53067f48d5 --- /dev/null +++ b/gdb/testsuite/gdb.base/warning.exp @@ -0,0 +1,36 @@ +# Copyright 2018 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 . + +# Test that an early warning does not cause a crash. + +if {[is_remote host]} { + unsupported "warning.exp can only run on local host" + return +} + +set tname [standard_temp_file warning] +set fd [open $tname w] +puts $fd "anything" +close $fd + +set save $INTERNAL_GDBFLAGS +set INTERNAL_GDBFLAGS "-nw -nx -data-directory $tname" + +gdb_start + +# Make sure gdb started up. +gdb_test "echo 23\\n" "23" + +set INTERNAL_GDBFLAGS $save