From patchwork Wed Jul 10 23:46:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergio Durigan Junior X-Patchwork-Id: 33666 Received: (qmail 59695 invoked by alias); 10 Jul 2019 23:46:27 -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 59685 invoked by uid 89); 10 Jul 2019 23:46:27 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-15.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=corefile, 23613, our, HContent-Transfer-Encoding:8bit X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 10 Jul 2019 23:46:26 +0000 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B3CC530ADC7C for ; Wed, 10 Jul 2019 23:46:24 +0000 (UTC) Received: from psique.yyz.redhat.com (unused-10-15-17-196.yyz.redhat.com [10.15.17.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id 17FD119C71; Wed, 10 Jul 2019 23:46:22 +0000 (UTC) From: Sergio Durigan Junior To: GDB Patches Cc: Sergio Durigan Junior Subject: [PATCH] Guard against 'current_directory == NULL' on gdb_abspath (PR gdb/23613) Date: Wed, 10 Jul 2019 19:46:15 -0400 Message-Id: <20190710234615.14800-1-sergiodj@redhat.com> MIME-Version: 1.0 X-IsSubscribed: yes Ref.: https://bugzilla.redhat.com/show_bug.cgi?id=1728147 Ref.: https://sourceware.org/bugzilla/show_bug.cgi?id=23613 Hi, This bug has been reported a few days ago against Fedora GDB. The problem reported is that GDB segfaults when the working directory is deleted. It's pretty use to reproduce it: mkdir bla cd bla rmdir ../bla gdb echo Debugging the problem is a bit tricky, because, since the current directory doesn't exist anymore, a corefile cannot be saved there. After a few attempts, I came up with the following: gdb -ex 'shell mkdir bla' -ex 'cd bla' -ex 'shell rmdir ../bla' -ex 'r echo' ./gdb/gdb This assumes that you're inside a build directory which contains ./gdb/gdb, of course. After investigating it, I found that the problem happens at gdb_abspath, where we're dereferencing 'current_directory' without checking if it's NULL: ... (concat (current_directory, IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1]) ? "" : SLASH_STRING, ... So I fixed the problem with the patch below. The idea is that, if 'current_directory' is NULL, then the final string returned should be just the "path". After fixing the bug, I found a similar one reported against our bugzilla: PR gdb/23613. The problem is the same, but the reproducer is a bit different. I really tried writing a testcase for this, but unfortunately it's apparently not possible to start GDB inside a non-existent directory with DejaGNU. I regression tested this patch on the BuildBot, and no regressions were found. gdb/ChangeLog: 2019-07-10 Sergio Durigan Junior https://bugzilla.redhat.com/show_bug.cgi?id=1728147 PR gdb/23613 * gdbsupport/pathstuff.c (gdb_abspath): Guard against 'current_directory == NULL' case. --- gdb/gdbsupport/pathstuff.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdb/gdbsupport/pathstuff.c b/gdb/gdbsupport/pathstuff.c index fafecd543d..aa51e8f36e 100644 --- a/gdb/gdbsupport/pathstuff.c +++ b/gdb/gdbsupport/pathstuff.c @@ -134,7 +134,7 @@ gdb_abspath (const char *path) if (path[0] == '~') return gdb_tilde_expand_up (path); - if (IS_ABSOLUTE_PATH (path)) + if (IS_ABSOLUTE_PATH (path) || current_directory == NULL) return make_unique_xstrdup (path); /* Beware the // my son, the Emacs barfs, the botch that catch... */