From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 60FDB3858D1E for ; Thu, 9 Nov 2023 12:03:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 60FDB3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 60FDB3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.220.29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699531426; cv=none; b=uevHD49SVhpRnbu09KqdZP4jxOmpsX+Svx10QqhGUqtlmXEUXdtr9OmK/Ug31Z+w30mLqfROqCA/Xa68V2eoA+nAXxVP+GDKhu0gQ2gVNnzdXKhqY+/qh06Uz+hLLq1ZULqUtM0PZkcVHfDXkByQFqr1AntZTawN+1BSmL0XjRo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699531426; c=relaxed/simple; bh=F9cyEVe/V7T6zsUHq/0+WgtDXwXVDt9BQnm9y8IelNI=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=I/dhvavUYaI+8HU8PCunrDz/xDhRKslrl5e2MzBzq/vCd0nmdZ5mYsxW1YHhiP8o3E4u36zT8Vg9DeoSeCB1sEZssmW1X543MZBns1vr+89DYniQwtzXZc/Gcvb3PfoigTnVI3rQ37dCbl07W7RYSB3arBZIRBkiYITpYoKJdYg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 6417F1F8AF for ; Thu, 9 Nov 2023 12:03:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1699531415; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GmHM7sfRX47r2gMTwn8xLZmB78LL3KJVlyjdtW3UPkg=; b=uouomehp8kZVraJe4JVXy0gJX7dpj7f04PS+lDDavhyL0xzTeSRLkv7V54lC6mhF0l1w1l lUJHjk9EFgueNq1mc/2BexSmP+RwzMEwfbxlAEd9cyIVogeq6bD++M0r2+PkEf2PeW9NZl di2LAm7UmWXYWBDovy4qLTIm8daRRIc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1699531415; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GmHM7sfRX47r2gMTwn8xLZmB78LL3KJVlyjdtW3UPkg=; b=RbxcFHxZggb3tiMQgUkZd2iR0mxN5JSI1dzu5o/JZ53NPWPR1JuVkWqGR70CAx/MgdXqIO CKcF/ul0MdY/dZAA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 51B4E13907 for ; Thu, 9 Nov 2023 12:03:35 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id gE3KEpfKTGWcJgAAMHmgww (envelope-from ) for ; Thu, 09 Nov 2023 12:03:35 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH 2/2] [gdb/tui] Fix resizing of terminal to 1 or 2 lines Date: Thu, 9 Nov 2023 13:04:55 +0100 Message-Id: <20231109120455.4741-2-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231109120455.4741-1-tdevries@suse.de> References: <20231109120455.4741-1-tdevries@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: When starting TUI in a terminal with 3 lines: ... $ echo $LINES 3 $ gdb -q -tui ... and resizing the terminal to 2 lines we run into a segfault. The problem is that for the source window: - the minimum height is 3 (the default), but - the maximum height is only 2 because there are only 2 lines. This discrepancy eventually leads to a call to newwin in make_window with: ... (gdb) p height $1 = 3 (gdb) p width $2 = 56 (gdb) p y $3 = -1 (gdb) p x $4 = 0 ... which results in a nullptr. This violates the assumption here in tui_apply_current_layout: .... /* Get the new list of currently visible windows. */ std::vector new_tui_windows; applied_layout->get_windows (&new_tui_windows); ... that get_windows only returns visible windows, which leads to tui_windows holding a dangling pointer, which results in the segfault. Fix this by: - making sure get_windows only returns visible windows, and - detecting the situation and dropping windows from the layout if there's no room for them. Tested on x86_64-linux. PR tui/31044 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31044 --- gdb/testsuite/gdb.tui/resize.exp | 9 +++++++++ gdb/tui/tui-layout.c | 22 ++++++++++++++++++++-- gdb/tui/tui-layout.h | 6 +++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/gdb/testsuite/gdb.tui/resize.exp b/gdb/testsuite/gdb.tui/resize.exp index 60d5116886b..e15f8a2f507 100644 --- a/gdb/testsuite/gdb.tui/resize.exp +++ b/gdb/testsuite/gdb.tui/resize.exp @@ -39,3 +39,12 @@ Term::check_contents "source at startup" "\\|.*21 *return 0" Term::resize 40 90 Term::check_box "source box after resize" 0 0 90 26 + +# Check that resizing to less than 3 lines doesn't cause problems. +foreach lines { 2 1 } { + with_test_prefix lines=$lines { + Term::resize $lines 90 0 + Term::wait_for "" + Term::check_region_contents "has prompt" 0 0 90 $lines "$gdb_prompt" + } +} diff --git a/gdb/tui/tui-layout.c b/gdb/tui/tui-layout.c index 159445dc520..4f8a4bc3bcc 100644 --- a/gdb/tui/tui-layout.c +++ b/gdb/tui/tui-layout.c @@ -452,6 +452,13 @@ tui_layout_window::apply (int x_, int y_, int width_, int height_, width = width_; height = height_; gdb_assert (m_window != nullptr); + if (width == 0 || height == 0) + { + /* The window was dropped, so it's going to be deleted, reset the + soon to be dangling pointer. */ + m_window = nullptr; + return; + } m_window->resize (height, width, x, y); } @@ -833,6 +840,7 @@ tui_layout_split::apply (int x_, int y_, int width_, int height_, int available_size = m_vertical ? height : width; int last_index = -1; int total_weight = 0; + int prev = -1; for (int i = 0; i < m_splits.size (); ++i) { bool cmd_win_already_exists = TUI_CMD_WIN != nullptr; @@ -864,6 +872,14 @@ tui_layout_split::apply (int x_, int y_, int width_, int height_, info[i].max_size = info[i].min_size; } + if (info[i].min_size > info[i].max_size) + { + /* There is not enough room for this window, drop it. */ + info[i].min_size = 0; + info[i].max_size = 0; + continue; + } + if (info[i].min_size == info[i].max_size) available_size -= info[i].min_size; else @@ -874,10 +890,12 @@ tui_layout_split::apply (int x_, int y_, int width_, int height_, /* Two adjacent boxed windows will share a border, making a bit more size available. */ - if (i > 0 - && m_splits[i - 1].layout->last_edge_has_border_p () + if (prev != -1 + && m_splits[prev].layout->last_edge_has_border_p () && m_splits[i].layout->first_edge_has_border_p ()) info[i].share_box = true; + + prev = i; } /* If last_index is set then we have a window that is not of a fixed diff --git a/gdb/tui/tui-layout.h b/gdb/tui/tui-layout.h index a6d34851bef..4d29c0570ac 100644 --- a/gdb/tui/tui-layout.h +++ b/gdb/tui/tui-layout.h @@ -189,7 +189,11 @@ class tui_layout_window : public tui_layout_base /* See tui_layout_base::get_windows. */ void get_windows (std::vector *windows) override { - windows->push_back (m_window); + if (m_window != nullptr && m_window->is_visible ()) + { + /* Only get visible windows. */ + windows->push_back (m_window); + } } protected: -- 2.35.3