From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id C6AA33858D20 for ; Thu, 2 May 2024 15:48:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C6AA33858D20 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 C6AA33858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714664922; cv=none; b=tnhGAL7nJHUS3eSJ/OSYoj5KQtKa/7ZKe0JDGsL4UTmBREr3Y66ai2JR9lwnh8UqcblhcAQsflHNBabAZii9j7HrdwkQx/Jh0XpsoMrIIywvwYubB5IZFJrytzka3eeBh7exWVkS7xxIo/JMcSjVpnE2Lx8XtZhvrVfNt9VuZFc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714664922; c=relaxed/simple; bh=TvO4eEOv0DoW8MfqoMe0ZwE0jjKzXozwWTv0W5wF98E=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-Id:MIME-Version; b=bCR+Ff0Ull+RIX8O1d8BBVr1fnA1Sw0DIGwNaHYNCajReEkdOJbq92YVBYgSELz7PvtIjFLPRILDeBpznr+sBhlrIJUvyhNWuwFAEzSdS2zjak8QWjYivG8SxXe+2kXX2ttxNSj8LOwzTy/H6NMNZ7mhuj4RMdaKAEUm2dOJtz0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id A7CD137241 for ; Thu, 2 May 2024 15:48:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1714664919; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=PI/xEYBBXPtOj1hUHdxZmG9cMTiLQIupql7kg81d9mA=; b=TyHsND/NS4YnxzeAaQk6JugnnNkLc9ToGfbrNyDmFZFmpbQTEUPYjrJ3cJm43kO/A5mO5o tCx1nD4qFP7JQ5RZZR6jm+nNEB/C3YWLXiutgA42vgdVA02hmh8MxNI7vXN0XbXWB5J1Ch tjEUIlLHkUkb1JzThFW3AZ5Ssb3F3uU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1714664919; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=PI/xEYBBXPtOj1hUHdxZmG9cMTiLQIupql7kg81d9mA=; b=sihw3gRZoPTjQK3pN8mSXixwR83LHy1pYxrlbsR/soTGyrp0aZcSpoI8J98vPZvsZYVjfU Un7fqQK6y8CI9DBQ== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b="TyHsND/N"; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=sihw3gRZ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1714664919; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=PI/xEYBBXPtOj1hUHdxZmG9cMTiLQIupql7kg81d9mA=; b=TyHsND/NS4YnxzeAaQk6JugnnNkLc9ToGfbrNyDmFZFmpbQTEUPYjrJ3cJm43kO/A5mO5o tCx1nD4qFP7JQ5RZZR6jm+nNEB/C3YWLXiutgA42vgdVA02hmh8MxNI7vXN0XbXWB5J1Ch tjEUIlLHkUkb1JzThFW3AZ5Ssb3F3uU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1714664919; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=PI/xEYBBXPtOj1hUHdxZmG9cMTiLQIupql7kg81d9mA=; b=sihw3gRZoPTjQK3pN8mSXixwR83LHy1pYxrlbsR/soTGyrp0aZcSpoI8J98vPZvsZYVjfU Un7fqQK6y8CI9DBQ== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 903FF13957 for ; Thu, 2 May 2024 15:48:39 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id DpfFIde1M2alfgAAD6G6ig (envelope-from ) for ; Thu, 02 May 2024 15:48:39 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH] [gdb/exp] Fix cast handling for indirection Date: Thu, 2 May 2024 17:49:02 +0200 Message-Id: <20240502154902.22575-1-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Level: X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; RCVD_TLS_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:dkim]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; TO_DN_NONE(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; MIME_TRACE(0.00)[0:+]; DKIM_TRACE(0.00)[suse.de:+] X-Rspamd-Action: no action X-Rspamd-Queue-Id: A7CD137241 X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: -3.01 X-Spam-Status: No, score=-12.3 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 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: Consider a test-case compiled without debug info, containing: ... char a = 'a'; char * a_loc (void) { return &a; } ... We get: ... (gdb) p (char)*a_loc () Cannot access memory at address 0x10 ... There's a bug in unop_ind_base_operation::evaluate that evaluates "(char)*a_loc ()" the same as: ... (gdb) p (char)*(char)a_loc () Cannot access memory at address 0x10 ... Fix this by instead evaluating it the same as: ... (gdb) p (char)*(char *)a_loc () $1 = 97 'a' ... Tested on x86_64-linux. PR exp/31693 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31693 --- gdb/expop.h | 8 +++-- gdb/testsuite/gdb.base/cast-indirection.c | 31 ++++++++++++++++ gdb/testsuite/gdb.base/cast-indirection.exp | 40 +++++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 gdb/testsuite/gdb.base/cast-indirection.c create mode 100644 gdb/testsuite/gdb.base/cast-indirection.exp diff --git a/gdb/expop.h b/gdb/expop.h index b81e228c07e..1967d9779b7 100644 --- a/gdb/expop.h +++ b/gdb/expop.h @@ -1513,9 +1513,11 @@ class unop_ind_base_operation struct expression *exp, enum noside noside) override { - if (expect_type != nullptr && expect_type->code () == TYPE_CODE_PTR) - expect_type = check_typedef (expect_type)->target_type (); - value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside); + struct type *pointer_to_expect_type = (expect_type != nullptr + ? lookup_pointer_type (expect_type) + : nullptr); + value *val + = std::get<0> (m_storage)->evaluate (pointer_to_expect_type, exp, noside); return eval_op_ind (expect_type, exp, noside, val); } diff --git a/gdb/testsuite/gdb.base/cast-indirection.c b/gdb/testsuite/gdb.base/cast-indirection.c new file mode 100644 index 00000000000..d59c66ead35 --- /dev/null +++ b/gdb/testsuite/gdb.base/cast-indirection.c @@ -0,0 +1,31 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +char a = 'a'; + +char * +a_loc (void) +{ + return &a; +} + +int +main (void) +{ + int res = *a_loc () == 'a'; + return !res; +} diff --git a/gdb/testsuite/gdb.base/cast-indirection.exp b/gdb/testsuite/gdb.base/cast-indirection.exp new file mode 100644 index 00000000000..d2c6d58e3ca --- /dev/null +++ b/gdb/testsuite/gdb.base/cast-indirection.exp @@ -0,0 +1,40 @@ +# Copyright (C) 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Check that "p (char)*a_loc ()" is handled as "p (char)*(char *)a_loc ()". + +standard_testfile + +if { [prepare_for_testing "failed to prepare" $testfile $srcfile \ + {nodebug}] == -1} { + return -1 +} + +if ![runto_main] { + return -1 +} + +gdb_test "p a_loc ()" \ + "'a_loc' has unknown return type; cast the call to its declared return type" + +gdb_test "p *a_loc ()" \ + "'a_loc' has unknown return type; cast the call to its declared return type" + +gdb_test "p *(char *)a_loc ()" " = 97 'a'" + +gdb_test "p (char)*(char *)a_loc ()" " = 97 'a'" + +# Regression test for PR31693. +gdb_test "p (char)*a_loc ()" " = 97 'a'" base-commit: 5ce0e02478cc79a260c7e29822450284a32b9b12 -- 2.35.3