From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gproxy1-pub.mail.unifiedlayer.com (gproxy1-pub.mail.unifiedlayer.com [69.89.25.95]) by sourceware.org (Postfix) with ESMTPS id A24E33858D28 for ; Wed, 27 Apr 2022 21:27:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A24E33858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw15.mail.unifiedlayer.com (unknown [10.0.90.130]) by progateway3.mail.pro1.eigbox.com (Postfix) with ESMTP id B214B1004754E for ; Wed, 27 Apr 2022 21:27:44 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id jpCKn3mz2kku4jpCKnJs0Q; Wed, 27 Apr 2022 21:27:44 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=ANANYO9+ c=1 sm=1 tr=0 ts=6269b550 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=z0gMJWrwH1QA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=ywfmg65ed7GKkbKQ2rkA:9 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=oZgltzupUtGzb8JAvUzOcenOWJN43tNrVrxaHTLPL5U=; b=D5vBlAM1X53R3WHDXaiH39tZMx qBwZSJiRliV/tgd6hv+q/lXpxcMYCfV15DCmTkaLqANIlfYaXs0x+wxJjp+5DF0tHcayc3jC3Q/Mi 44eMe2y5iWT1X6PdxULILXdHH; Received: from 71-211-158-194.hlrn.qwest.net ([71.211.158.194]:45570 helo=prentzel.Home) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1njpCJ-000zfi-R0; Wed, 27 Apr 2022 15:27:43 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Fix crash in gdbpy_parse_register_id Date: Wed, 27 Apr 2022 15:27:42 -0600 Message-Id: <20220427212742.4003557-1-tom@tromey.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 71.211.158.194 X-Source-L: No X-Exim-ID: 1njpCJ-000zfi-R0 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 71-211-158-194.hlrn.qwest.net (prentzel.Home) [71.211.158.194]:45570 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3029.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 27 Apr 2022 21:27:47 -0000 I noticed that gdbpy_parse_register_id would assert if passed a Python object of a type it was not expecting. The included test case shows this crash. This patch fixes the problem and also changes gdbpy_parse_register_id to be more "Python-like" -- it always ensures the Python error is set when it fails, and the callers now simply propagate the existing exception. --- gdb/python/py-frame.c | 5 +---- gdb/python/py-registers.c | 22 +++++++++++++++------- gdb/python/py-unwind.c | 10 ++-------- gdb/python/python-internal.h | 3 ++- gdb/testsuite/gdb.python/py-frame.exp | 5 +++++ 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c index d07158a5ec6..933bdc773eb 100644 --- a/gdb/python/py-frame.c +++ b/gdb/python/py-frame.c @@ -253,10 +253,7 @@ frapy_read_register (PyObject *self, PyObject *args) if (!gdbpy_parse_register_id (get_frame_arch (frame), pyo_reg_id, ®num)) - { - PyErr_SetString (PyExc_ValueError, "Bad register"); - return NULL; - } + return NULL; gdb_assert (regnum >= 0); val = value_of_register (regnum, frame); diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c index bbb322f068c..c7ea529bdde 100644 --- a/gdb/python/py-registers.c +++ b/gdb/python/py-registers.c @@ -386,21 +386,27 @@ gdbpy_parse_register_id (struct gdbarch *gdbarch, PyObject *pyo_reg_id, { *reg_num = user_reg_map_name_to_regnum (gdbarch, reg_name.get (), strlen (reg_name.get ())); - return *reg_num >= 0; + if (*reg_num >= 0) + return true; + PyErr_SetString (PyExc_ValueError, "Bad register"); } } /* The register could be its internal GDB register number. */ else if (PyLong_Check (pyo_reg_id)) { long value; - if (gdb_py_int_as_long (pyo_reg_id, &value) && (int) value == value) + if (gdb_py_int_as_long (pyo_reg_id, &value) == 0) { - if (user_reg_map_regnum_to_name (gdbarch, value) != NULL) - { - *reg_num = (int) value; - return true; - } + /* Nothing -- error. */ } + else if ((int) value == value + && user_reg_map_regnum_to_name (gdbarch, value) != NULL) + { + *reg_num = (int) value; + return true; + } + else + PyErr_SetString (PyExc_ValueError, "Bad register"); } /* The register could be a gdb.RegisterDescriptor object. */ else if (PyObject_IsInstance (pyo_reg_id, @@ -417,6 +423,8 @@ gdbpy_parse_register_id (struct gdbarch *gdbarch, PyObject *pyo_reg_id, PyErr_SetString (PyExc_ValueError, _("Invalid Architecture in RegisterDescriptor")); } + else + PyErr_SetString (PyExc_ValueError, _("Invalid type for register")); gdb_assert (PyErr_Occurred ()); return false; diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c index b2fd1402e93..e2cd67a0785 100644 --- a/gdb/python/py-unwind.c +++ b/gdb/python/py-unwind.c @@ -262,10 +262,7 @@ unwind_infopy_add_saved_register (PyObject *self, PyObject *args) &pyo_reg_id, &pyo_reg_value)) return NULL; if (!gdbpy_parse_register_id (pending_frame->gdbarch, pyo_reg_id, ®num)) - { - PyErr_SetString (PyExc_ValueError, "Bad register"); - return NULL; - } + return NULL; /* If REGNUM identifies a user register then *maybe* we can convert this to a real (i.e. non-user) register. The maybe qualifier is because we @@ -383,10 +380,7 @@ pending_framepy_read_register (PyObject *self, PyObject *args) if (!PyArg_UnpackTuple (args, "read_register", 1, 1, &pyo_reg_id)) return NULL; if (!gdbpy_parse_register_id (pending_frame->gdbarch, pyo_reg_id, ®num)) - { - PyErr_SetString (PyExc_ValueError, "Bad register"); - return NULL; - } + PyErr_SetString (PyExc_ValueError, "Bad register"); try { diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index d947b96033b..dffcd3f1b7f 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -799,7 +799,8 @@ typedef std::unique_ptr Py_buffer_up; If a register is parsed successfully then *REG_NUM will have been updated, and true is returned. Otherwise the contents of *REG_NUM are - undefined, and false is returned. + undefined, and false is returned. When false is returned, the + Python error is set. The PYO_REG_ID object can be a string, the name of the register. This is the slowest approach as GDB has to map the name to a number for each diff --git a/gdb/testsuite/gdb.python/py-frame.exp b/gdb/testsuite/gdb.python/py-frame.exp index b91ffe62a83..881219342e3 100644 --- a/gdb/testsuite/gdb.python/py-frame.exp +++ b/gdb/testsuite/gdb.python/py-frame.exp @@ -128,3 +128,8 @@ if { $pc != "" } { " = True" \ "test Frame.read_register($pc)" } + +# This previously caused a crash. +gdb_test "python print(gdb.selected_frame().read_register(list()))" \ + ".*Invalid type for register.*" \ + "test Frame.read_register with list" -- 2.34.1