From patchwork Wed Nov 20 16:31:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 101591 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A3FA83858415 for ; Wed, 20 Nov 2024 16:33:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A3FA83858415 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=Z1EPN8Mz X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12b.google.com (mail-il1-x12b.google.com [IPv6:2607:f8b0:4864:20::12b]) by sourceware.org (Postfix) with ESMTPS id 540B03858CDB for ; Wed, 20 Nov 2024 16:31:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 540B03858CDB Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 540B03858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::12b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732120272; cv=none; b=H22btX9XGM05PAPA90UIaz4QZKMQr2/C7T3ubFJq0amMsOtZnIsXmX4i9NnjGZV0TL7YvnXyKjch5UMzxqAyl+tKXWkMlpRRPFoxqddjVJiTH5/I2m6HNxSsxef9unq2hT6gTn05rUD+piaUDmGO7xtlNxA98lR9II1AGgaGSBc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732120272; c=relaxed/simple; bh=aFD2DAK7XFyLbs+BCDAsrOTPzgGPmuzNCNPlH3E4xxg=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=b/m0uKkUD0GOzj1VuIvqLXwDt/OXJcgrHW9Fc94cao5Ld63nAdMWG4s80u4ulHbxOqmdnY4Vn14wcS+pGDrKjxaZqutIBvKTyR+LQmcpZSnqWhDMH9/LAVHOOU1z2o6+x8FeqrFaWRusL8Ha5DFgRFXA/A2l8PZHyDKzSBD8Ix8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 540B03858CDB Received: by mail-il1-x12b.google.com with SMTP id e9e14a558f8ab-3a761e21ddeso15888485ab.3 for ; Wed, 20 Nov 2024 08:31:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1732120271; x=1732725071; darn=sourceware.org; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=5OfPF9Afrp0Aqx7Wk0u2zPHpyEcCoX3TNQsxyGd4kcw=; b=Z1EPN8Mz9ALXCuPn+gvdVANlYncig7bthkVlvcDGHNOUWM77mgA/1DSzLvAS8ZWOKi KUTE7eHQckII76UCwSP+cs2J778wpRaOws7KP6khqH6ON0x/1PRfHvIi8FvjbAyIE7UX e342tGjMI082aSDRO00Pg4vr1hP5eVwfSlx7oquVg/aPxzcgvIaUFhKTqKrrhZDGUG9c Rxz13544XvDPB0JmRFakFwf4VZl+yYXf4STli7CEJU93Bwf+PLf5sSN0D7OscM0EXGO7 GwAvRv13HG7y9/AGzOHufV3EhcVZcINXb7WTy8myNk/alWU661XfXOP4bR8oNNmyoQEk q6cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732120271; x=1732725071; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5OfPF9Afrp0Aqx7Wk0u2zPHpyEcCoX3TNQsxyGd4kcw=; b=aKiJi44z9tMZuOJGivOQfqZDZOLNioEylpQdJx/AQNYAluuB6iCbMUXzRlAzQhiKlx W/XpQ4pYwls53nkFaW4moDF7j+1bSNRjRQXDUt0ZMVK9p5tcjorojHpfoLsmBJjFfE7n p75Op6OfhS8jK8Huk3Xs9rVHzO+Glw0Ho2m7bWXYpH0ne5qLwtX4OSxQz5kJfmwKQDv1 knj9dU3tOH0Hz74wCDiNmLUjpHCFBHTjqgKggKMcKhzmHYVnWHxeO+yjwWvLw8HOgIuz 4/A2N9Z8LlV7fgcx0yUAnIx2IrCnQf54my0whiQNu0ds5Lx6Jm7CtSuHThGeDEa2mIVN vNgA== X-Gm-Message-State: AOJu0Yyn3EjjKIzrBPXje+EDc6rmz+jvCWTP+pISknTNYC4fuBxiS3Di Dmf3ZfYX7COllSrTQ6Hnlul7jnyxvSB8veizi/2HeridzhLvAaqNxFr1UL3c56em+3VNGtVPcm4 = X-Google-Smtp-Source: AGHT+IFGQLhB9e32iz/Ye/Yp1h7/ZTPLhjIaH0c/Xmz5mbBmCxem96NCnKpWoTriQe+jHB0yZBSIJA== X-Received: by 2002:a05:6e02:1c81:b0:3a7:7ded:53b9 with SMTP id e9e14a558f8ab-3a7865cdf20mr40900215ab.20.1732120271529; Wed, 20 Nov 2024 08:31:11 -0800 (PST) Received: from localhost.localdomain (97-122-113-155.hlrn.qwest.net. [97.122.113.155]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-3a78721480esm4132685ab.2.2024.11.20.08.31.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 08:31:11 -0800 (PST) From: Tom Tromey Date: Wed, 20 Nov 2024 09:31:07 -0700 Subject: [PATCH 2/4] Reject non-string types in gdb.Value.lazy_string MIME-Version: 1.0 Message-Id: <20241120-noop-string-printer-v1-2-40c44526fb4a@adacore.com> References: <20241120-noop-string-printer-v1-0-40c44526fb4a@adacore.com> In-Reply-To: <20241120-noop-string-printer-v1-0-40c44526fb4a@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.14.2 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_LOTSOFHASH, RCVD_IN_DNSWL_NONE, 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org Currently, gdb.Value.lazy_string will allow the conversion of any object to a "lazy string". However, this was never the intent and is weird besides. This patch changes this code to correctly throw an exception in the non-matching cases. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=20769 --- gdb/python/py-lazy-string.c | 4 +--- gdb/python/py-value.c | 13 ++----------- gdb/testsuite/gdb.python/py-lazy-string.c | 1 + gdb/testsuite/gdb.python/py-lazy-string.exp | 5 +++++ 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/gdb/python/py-lazy-string.c b/gdb/python/py-lazy-string.c index 4898a1f8299e1d70e4ca14162b3fc242e6dada3c..30a171d4c0b02c7db3e3b8e885b95a7c2dbe7e4e 100644 --- a/gdb/python/py-lazy-string.c +++ b/gdb/python/py-lazy-string.c @@ -264,9 +264,7 @@ stpy_lazy_string_elt_type (lazy_string_object *lazy) case TYPE_CODE_ARRAY: return check_typedef (realtype->target_type ()); default: - /* This is done to preserve existing behaviour. PR 20769. - E.g., gdb.parse_and_eval("my_int_variable").lazy_string().type. */ - return realtype; + gdb_assert_not_reached ("invalid lazy string"); } } diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index eef3841924fabf31d5b203c7227e020c3e27a0a1..2e4ff67044ceca9df489b328e8f10d2e6055ee53 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -520,14 +520,7 @@ valpy_get_dynamic_type (PyObject *self, void *closure) If LENGTH is provided then the length parameter is set to LENGTH. Otherwise if the value is an array of known length then the array's length is used. Otherwise the length will be set to -1 (meaning first null of - appropriate with). - - Note: In order to not break any existing uses this allows creating - lazy strings from anything. PR 20769. E.g., - gdb.parse_and_eval("my_int_variable").lazy_string(). - "It's easier to relax restrictions than it is to impose them after the - fact." So we should be flagging any unintended uses as errors, but it's - perhaps too late for that. */ + appropriate with). */ static PyObject * valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) @@ -595,9 +588,7 @@ valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) addr = value_as_address (value); break; default: - /* Should flag an error here. PR 20769. */ - addr = value->address (); - break; + error (_("Cannot make lazy string from this object")); } str_obj = gdbpy_create_lazy_string_object (addr, length, user_encoding, diff --git a/gdb/testsuite/gdb.python/py-lazy-string.c b/gdb/testsuite/gdb.python/py-lazy-string.c index 326523dc5f98a999cd3e54073d3605f120f73b82..805ba9297f471b7e7d6e17f2712f985d853fb8ba 100644 --- a/gdb/testsuite/gdb.python/py-lazy-string.c +++ b/gdb/testsuite/gdb.python/py-lazy-string.c @@ -24,6 +24,7 @@ main () const char array[] = "array"; pointer typedef_ptr = "typedef pointer"; const char *null = 0; + int not_a_string = 23; return 0; /* break here */ } diff --git a/gdb/testsuite/gdb.python/py-lazy-string.exp b/gdb/testsuite/gdb.python/py-lazy-string.exp index a00f47b1e2a2767da5fa9a0fe00d39ff6fa2a93e..0650c67d5e1cc75b00d2d3cb8d8ec3433979ee52 100644 --- a/gdb/testsuite/gdb.python/py-lazy-string.exp +++ b/gdb/testsuite/gdb.python/py-lazy-string.exp @@ -73,3 +73,8 @@ foreach var_spec { { "ptr" "pointer" "const char \\*" -1 } \ #gdb_test "python print ($var.lazy_string(length=0).value())" "\"\"" "empty lazy string value" } } + +gdb_py_test_silent_cmd "python nas = gdb.parse_and_eval('not_a_string')" \ + "get not_a_string" 1 +gdb_test "python print(nas.lazy_string())" \ + "Cannot make lazy string from this object"