From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 1659B3858421 for ; Mon, 24 Oct 2022 08:49:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1659B3858421 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de 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 559D81FB8B for ; Mon, 24 Oct 2022 08:49:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1666601354; 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=1lO92+vL1+mbkBxJDpZMfwDUu71oxcdKnNaSSO5XSLE=; b=XGOJfw9qvpD4hWhrf5Yp7LZv1NH+1V/ff0keB/CB5hf5QXRoQ5ehLWK1q7+8DOybtj4mA/ fnaQZkToHzb+79UNvyIIvEzqktbXdremPLU4kOBgSbiwytLJCI0d2Kn47tmerZZ6zbjEr7 t5NW1B36fYXE8ueC2nfdI4HGRhCECi0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1666601354; 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=1lO92+vL1+mbkBxJDpZMfwDUu71oxcdKnNaSSO5XSLE=; b=LoFlsmVCW3EqfLHL2HzpGPSMaSnxQGHa398zCDBPoBevQPkr1/xPfOy802DJyoerVpYKv/ r9gsUzn8rx5o6lBg== 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 4225C13357 for ; Mon, 24 Oct 2022 08:49:14 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id +PwAD4pRVmMsWgAAMHmgww (envelope-from ) for ; Mon, 24 Oct 2022 08:49:14 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH 2/2] [gdb] Fix rethrow exception slicing in insert_bp_location Date: Mon, 24 Oct 2022 10:49:13 +0200 Message-Id: <20221024084913.19429-3-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20221024084913.19429-1-tdevries@suse.de> References: <20221024084913.19429-1-tdevries@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_PASS,TXREP 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: The preferred way of rethrowing an exception is by using throw without expression, because it avoids object slicing of the exception [1]. Fix this in insert_bp_location. [1] https://en.cppreference.com/w/cpp/language/throw --- gdb/breakpoint.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index a0653f189b9..a001e78cfb4 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -2647,6 +2647,24 @@ breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr) return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr); } +#define RETHROW_ON_TARGET_CLOSE_ERROR(E) \ + do \ + { \ + if ((E).reason != 0) \ + { \ + /* Can't set the breakpoint. */ \ + \ + if ((E).error == TARGET_CLOSE_ERROR) \ + /* If the target has closed then it will have deleted any \ + breakpoints inserted within the target inferior, as a \ + result any further attempts to interact with the \ + breakpoint objects is not possible. Just rethrow the \ + error. Don't use E to rethrow, to prevent object \ + slicing of the exception. */ \ + throw; \ + } \ + } while (0) + /* Insert a low-level "breakpoint" of some type. BL is the breakpoint location. Any error messages are printed to TMP_ERROR_STREAM; and DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems. @@ -2734,6 +2752,7 @@ insert_bp_location (struct bp_location *bl, } catch (gdb_exception &e) { + RETHROW_ON_TARGET_CLOSE_ERROR (e); bp_excpt = std::move (e); } } @@ -2773,6 +2792,7 @@ insert_bp_location (struct bp_location *bl, } catch (gdb_exception &e) { + RETHROW_ON_TARGET_CLOSE_ERROR (e); bp_excpt = std::move (e); } @@ -2797,6 +2817,7 @@ insert_bp_location (struct bp_location *bl, } catch (gdb_exception &e) { + RETHROW_ON_TARGET_CLOSE_ERROR (e); bp_excpt = std::move (e); } } @@ -2811,13 +2832,6 @@ insert_bp_location (struct bp_location *bl, if (bp_excpt.reason != 0) { /* Can't set the breakpoint. */ - - /* If the target has closed then it will have deleted any - breakpoints inserted within the target inferior, as a result - any further attempts to interact with the breakpoint objects - is not possible. Just rethrow the error. */ - if (bp_excpt.error == TARGET_CLOSE_ERROR) - throw bp_excpt; gdb_assert (bl->owner != nullptr); /* In some cases, we might not be able to insert a -- 2.35.3