From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-114.mimecast.com (us-smtp-delivery-114.mimecast.com [170.10.129.114]) by sourceware.org (Postfix) with ESMTPS id 35D513858421 for ; Fri, 19 May 2023 12:40:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 35D513858421 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=labware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=labware.com Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2105.outbound.protection.outlook.com [104.47.58.105]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-623-ZYsyFBU9NpSWW5Y7Vqp6qg-1; Fri, 19 May 2023 08:40:33 -0400 X-MC-Unique: ZYsyFBU9NpSWW5Y7Vqp6qg-1 Received: from SJ2PR17MB6568.namprd17.prod.outlook.com (2603:10b6:a03:4f7::10) by SJ0PR17MB5126.namprd17.prod.outlook.com (2603:10b6:a03:390::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.21; Fri, 19 May 2023 12:40:28 +0000 Received: from SJ2PR17MB6568.namprd17.prod.outlook.com ([fe80::fa1f:c830:6d52:12ab]) by SJ2PR17MB6568.namprd17.prod.outlook.com ([fe80::fa1f:c830:6d52:12ab%6]) with mapi id 15.20.6411.019; Fri, 19 May 2023 12:40:28 +0000 From: Jan Vrany To: gdb-patches@sourceware.org CC: Wenyan.Xin@windriver.com, Jan Vrany , Tom Tromey Subject: [pushed] gdb: fix post-hook execution for remote targets Date: Fri, 19 May 2023 13:40:07 +0100 Message-ID: <20230519124007.386152-1-jan.vrany@labware.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <871qje23e0.fsf@tromey.com> References: <871qje23e0.fsf@tromey.com> X-ClientProxiedBy: LO4P123CA0453.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1aa::8) To SJ2PR17MB6568.namprd17.prod.outlook.com (2603:10b6:a03:4f7::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ2PR17MB6568:EE_|SJ0PR17MB5126:EE_ X-MS-Office365-Filtering-Correlation-Id: e0444f0e-a36b-44c8-fd39-08db586639a7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0 X-Microsoft-Antispam-Message-Info: dp1WJBb6I1Ddbcv5ImfLzrMgpDvm4gpNYdp0ms/GkeeUB720dKLGKAYv6yuxq/8IHOiR8TPgNFDPhaj1qGAex4W+R9zgKNBbsSZRJDozUb8NKAbRLmX3rTGuSO6iE0OEV9TTHlhoNVCQX8lpVsMJcfHVuzKJbZ6yypmcTmA08WOFKdUSEYJ3D/Yg8+zCuxPEfnFncbqilb+6A9jgckdaBf5xj9dTML6cy6q36WoHH+L+Xdbafs8z1qlTugIxYahlKxggU3Wgveaep/ezP51SzDTMgzhcUQ/KvfHJiHMM9JYQftet0TktztolWKVp4YYwWwvc87jrSonQ3Wnvy70T2jWbqcvXEdLq4qCk8c1UbvInM33qomdohCSeJysRFK5DuwhmQlMgo+rVN1ZTMK31epottl/B1j0jO8oHjGxUO2GQnP3C0SUmawjlfLeUaL2/KkJlhEhhGW46S3EcYf9x89knv7QAtRt7WwDZYBIkmnrHI4labw0TUXBuCNccBGCP1Hw4LO64ARWSR84Et13PfjvtxxB8T37NglLr+Cx4MNM= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ2PR17MB6568.namprd17.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(366004)(396003)(136003)(39840400004)(376002)(346002)(451199021)(66556008)(66476007)(66946007)(6916009)(4326008)(84970400001)(8936002)(8676002)(316002)(54906003)(478600001)(41300700001)(6666004)(966005)(2906002)(86362001)(6512007)(6506007)(26005)(1076003)(6486002)(44832011)(38100700002)(186003)(83380400001)(5660300002)(36756003)(2616005);DIR:OUT;SFP:1101 X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?GXMJmQwVyCQmWxdrNq/s+7gSqzSm2RUdrEQuCDSrIRzlkErcXAJB4MebA8/x?= =?us-ascii?Q?lN02rSE9QBYXSoFRcX8YHh7T1rhLOjKwlpVtuaiImr5n0tMirHx0Lsedoaig?= =?us-ascii?Q?7W9FV6FasiBksJ8JJfyvZYehf07BM5el1p7sK3PSwN9nhf/WaZG2IyduLr5p?= =?us-ascii?Q?sdeE0SRE2SR1IvQ9xmHdokC+hh+P4xevO4aM/hCVXVYcK61cU3NKlkNvr1yf?= =?us-ascii?Q?Md3zZ+1e/pY9G/o26bEk2vOad6SvUIUIe1Q+MNgPFG9x94D25DNIRblqO8Pq?= =?us-ascii?Q?M33NlXqdt2IFtBFSytobnwihGg2p2v3+9Na3PdQ/9yk94EwQ++JCCEPG9bhJ?= =?us-ascii?Q?9oaKzDsQFFW8zMbHTzvuqAthSPhIWLaG1B3cSWRsMgIxY88vyXqCM7fW212b?= =?us-ascii?Q?7B8AOf4OZrccIbeZh50B2F18Npv7uzzDlC+16s7efTQvIp+nkiY8SYcmFQzK?= =?us-ascii?Q?Eil261XfHJkEvfWf8dVsssIL3HPa2wuwIsZGE9FidgVt9T/x4DK28o+a0+17?= =?us-ascii?Q?JnKOJW/PZH+RG4GiD/Oyjt+2p7r5axC3OL8i0f24JkhnfrrZGt81GyHsOjgG?= =?us-ascii?Q?t1+umV7ksThVjL/6Rf8gd/wKquzjYhkrkF9Xk0fttt310kzoj/Xl3km3eL4a?= =?us-ascii?Q?ZuGDA5TA8lqsN4UqunQMEVY2HIS4o8Ysa87fkkzezfvIbu5wKMGsdkfkZb7I?= =?us-ascii?Q?oImbCVvb0Qc+Hk9V/fx4M/Rk6J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ginatorOrg: labware.com X-MS-Exchange-CrossTenant-Network-Message-Id: e0444f0e-a36b-44c8-fd39-08db586639a7 X-MS-Exchange-CrossTenant-AuthSource: SJ2PR17MB6568.namprd17.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2023 12:40:28.1497 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b5db0322-1aa0-4c0a-859c-ad0f96966f4c X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: RvZlZbLy9lowcE0GD8E0xuxrvprWqx9ZuMKpZYrOeBigKkw9GN8yyWcxArKJo31K9dvAujA/OCtgp+6G76P8vQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR17MB5126 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: labware.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=WINDOWS-1252 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,RCVD_IN_DNSWL_NONE,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: Commit b5661ff2 ("gdb: fix possible use-after-free when executing commands") attempted to fix possible use-after-free in case command redefines itself. Commit 37e5833d ("gdb: fix command lookup in execute_command ()") updated the previous fix to handle subcommands as well by using the original command string to lookup the command again after its execution. This fixed the test in gdb.base/define.exp but it turned out that it does not work (at least) for "target remote" and "target extended-remote". The problem is that the command buffer P passed to execute_command () gets overwritten in dont_repeat () while executing "target remote" command itself: =09#0 dont_repeat () at top.c:822 =09#1 0x000055555730982a in target_preopen (from_tty=3D1) at target.c:2483 =09#2 0x000055555711e911 in remote_target::open_1 (name=3D0x55555881c7fe "= :1234", from_tty=3D1, extended_p=3D0) =09 at remote.c:5946 =09#3 0x000055555711d577 in remote_target::open (name=3D0x55555881c7fe ":1= 234", from_tty=3D1) at remote.c:5272 =09#4 0x00005555573062f2 in open_target (args=3D0x55555881c7fe ":1234", fr= om_tty=3D1, command=3D0x5555589d0490) =09 at target.c:853 =09#5 0x0000555556ad22fa in cmd_func (cmd=3D0x5555589d0490, args=3D0x55555= 881c7fe ":1234", from_tty=3D1) =09 at cli/cli-decode.c:2737 =09#6 0x00005555573487fd in execute_command (p=3D0x55555881c802 "4", from_= tty=3D1) at top.c:688 Therefore the second call to lookup_cmd () at line 697 fails to find command because the original command string is gone. This commit addresses this particular problem by creating a *copy* of original command string for the sole purpose of using it after command execution to lookup the command again. It may not be the most efficient way but it's safer given that command buffer is shared and overwritten in hard-to-foresee situations. Tested on x86_64-linux. PR 30249 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D30249 Approved-By: Tom Tromey --- gdb/top.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gdb/top.c b/gdb/top.c index 0b819091d11..92de30a1472 100644 --- a/gdb/top.c +++ b/gdb/top.c @@ -471,6 +471,8 @@ execute_command (const char *p, int from_tty) return; } =20 + std::string cmd_copy =3D p; + target_log_command (p); =20 while (*p =3D=3D ' ' || *p =3D=3D '\t') @@ -577,7 +579,7 @@ execute_command (const char *p, int from_tty) =09 We need to lookup the command again since during its execution, =09 a command may redefine itself. In this case, C pointer =09 becomes invalid so we need to look it up again. */ - const char *cmd2 =3D cmd_start; + const char *cmd2 =3D cmd_copy.c_str (); c =3D lookup_cmd (&cmd2, cmdlist, "", nullptr, 1, 1); if (c !=3D nullptr) =09execute_cmd_post_hook (c); --=20 2.39.2