From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id 7F7663857C5B; Mon, 24 Oct 2022 12:20:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7F7663857C5B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1666614058; bh=l0l85kYJB6BEBRiqO+Th9GcSqstHhGBCCEjmqcevawU=; h=From:To:Subject:Date:From; b=sXBQUcrj5IwWSy/jIUPYANdJwVdPtOxzVtsQTBDiuDSmpudfOQkgJGRRnkGJH+Upe 0EKqAFvy2HkicwIOn4Y4P46T/h0fd6HwTCQCSzALrA2b7UWF7nO1fZGF70YAQLKQV8 /E6DhWd9/PoCzexlYnsfxDrbMx7hUQDxsSi2gIUs= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] [gdb] Fix rethrow exception slicing in insert_bp_location X-Act-Checkin: binutils-gdb X-Git-Author: Tom de Vries X-Git-Refname: refs/heads/master X-Git-Oldrev: 0a9c805dfd0dd9a284b30ec0563e4dc1ab5b16c4 X-Git-Newrev: b2829fcf9b594ad9933d649cb089efa5b63a2b89 Message-Id: <20221024122058.7F7663857C5B@sourceware.org> Date: Mon, 24 Oct 2022 12:20:58 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Db2829fcf9b59= 4ad9933d649cb089efa5b63a2b89 commit b2829fcf9b594ad9933d649cb089efa5b63a2b89 Author: Tom de Vries Date: Mon Oct 24 14:20:49 2022 +0200 [gdb] Fix rethrow exception slicing in insert_bp_location =20 The preferred way of rethrowing an exception is by using throw without expression, because it avoids object slicing of the exception [1]. =20 Fix this in insert_bp_location. =20 Tested on x86_64-linux. =20 [1] https://en.cppreference.com/w/cpp/language/throw =20 Approved-By: Andrew Burgess Diff: --- 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); } =20 +#define RETHROW_ON_TARGET_CLOSE_ERROR(E) \ + do \ + { \ + if ((E).reason !=3D 0) \ + { \ + /* Can't set the breakpoint. */ \ + \ + if ((E).error =3D=3D 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 =3D 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 =3D std::move (e); } =20 @@ -2797,6 +2817,7 @@ insert_bp_location (struct bp_location *bl, } catch (gdb_exception &e) { + RETHROW_ON_TARGET_CLOSE_ERROR (e); bp_excpt =3D std::move (e); } } @@ -2811,13 +2832,6 @@ insert_bp_location (struct bp_location *bl, if (bp_excpt.reason !=3D 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 =3D=3D TARGET_CLOSE_ERROR) - throw bp_excpt; gdb_assert (bl->owner !=3D nullptr); =20 /* In some cases, we might not be able to insert a