From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 14E203857806 for ; Tue, 17 May 2022 10:51:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 14E203857806 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-649-8O5dSLnAMg-et013qngDpg-1; Tue, 17 May 2022 06:51:52 -0400 X-MC-Unique: 8O5dSLnAMg-et013qngDpg-1 Received: by mail-wm1-f69.google.com with SMTP id n26-20020a1c721a000000b003941ea1ced7so792109wmc.7 for ; Tue, 17 May 2022 03:51:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fyoUV9marqU5/wjiqb7cncYXuD9SOcoayYiCB/ZIDo8=; b=QBRBc+Z8ZmvBYBZ0UnX0bU/3pQ8kiXGNa56lNYpwcVXt45x0+FgZah1eSnwu5LtI9v 1jzDncmbPU2ZM0z8la9Zrw7kdQ87yrYmTYaCdHwau+0FMfd5j2aVQoERQuf0Rmb65YHm RG3r+dg2cDm3KYgTNFL/c+dMSOkiGtv0qcOstA2SfWnLDIRJ9ftxig/Eimn1EXUQEFLP AXwj149RK96GZ9W9owehzeC31dtAlNttDa64hxmAPY6bnqYh5k1z4je7o4i4Td0dGozf cc4xcYVedqaJLk0/FqtddYyFBgghKY/t3rMg+t4WTWlRV1i2KRfFZIZ14R9uIrMdsMKb v6iQ== X-Gm-Message-State: AOAM530sE659TNjj5BInquQe0MDquXsJsZCLTVwnj4BcLem6DP85fJna iCn3K8Zg9UXHihlyRkbI8RRwo5oWso8VpHL5iS6I4RBvB8jzQLf/xoK0fcCwE+OsNgpZMFnQMno ktlv8OZ95Zl6ykfTsVkyFY/6HD4KcqNF3NySZotKIC0P7fUT9vAdPkZVFzG2wq+huBfLoOjR32A == X-Received: by 2002:a5d:5964:0:b0:20e:58a8:5eaa with SMTP id e36-20020a5d5964000000b0020e58a85eaamr2220581wri.182.1652784710995; Tue, 17 May 2022 03:51:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzh9KxhfZP3yYhJztUro4JYx+wm58jnxpz6i0PLfFn4fstOzJMBknTSBSNXdOa6JCa7h0piTg== X-Received: by 2002:a5d:5964:0:b0:20e:58a8:5eaa with SMTP id e36-20020a5d5964000000b0020e58a85eaamr2220539wri.182.1652784710452; Tue, 17 May 2022 03:51:50 -0700 (PDT) Received: from localhost (host81-136-113-48.range81-136.btcentralplus.com. [81.136.113.48]) by smtp.gmail.com with ESMTPSA id a26-20020a1cf01a000000b003942a244ee6sm1562027wmb.43.2022.05.17.03.51.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 May 2022 03:51:50 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 1/2] gdb: use gdb::unique_xmalloc_ptr for docs in cmdpy_init Date: Tue, 17 May 2022 11:51:44 +0100 Message-Id: <31151a0451266a6dd2a4c01e4a60795596ad2077.1652784658.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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 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: Tue, 17 May 2022 10:51:58 -0000 Make use of gdb::unique_xmalloc_ptr to hold the documentation string in cmdpy_init (when creating a custom GDB command in Python). I think this is all pretty straight forward, the only slight weirdness is the removal of the call to free toward the end of this function. Prior to this commit, if an exception was thrown after the GDB command was created then we would (I think) end up freeing the documentation string even though the command would remain registered with GDB, which would surely lead to undefined behaviour. After this commit we release the doc string at the point that we hand it over to the command creation routines. If we throw _after_ the command has been created within GDB then the doc string will be left live. If we throw during the command creation itself (either from add_prefix_cmd or add_cmd) then it is up to those functions to free the doc string (I suspect we don't, but I think in general the commands are pretty bad at cleaning up after themselves, so I don't think this is a huge problem). --- gdb/python/py-cmd.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c index c0a98544619..bc48c2ea9f9 100644 --- a/gdb/python/py-cmd.c +++ b/gdb/python/py-cmd.c @@ -430,7 +430,6 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kw) const char *name; int cmdtype; int completetype = -1; - char *docstring = NULL; struct cmd_list_element **cmd_list; static const char *keywords[] = { "name", "command_class", "completer_class", "prefix", NULL }; @@ -484,19 +483,20 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kw) is_prefix = cmp > 0; } + gdb::unique_xmalloc_ptr docstring = nullptr; if (PyObject_HasAttr (self, gdbpy_doc_cst)) { gdbpy_ref<> ds_obj (PyObject_GetAttr (self, gdbpy_doc_cst)); if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ())) { - docstring = python_string_to_host_string (ds_obj.get ()).release (); - if (docstring == NULL) + docstring = python_string_to_host_string (ds_obj.get ()); + if (docstring == nullptr) return -1; } } - if (! docstring) - docstring = xstrdup (_("This command is not documented.")); + if (docstring == nullptr) + docstring = make_unique_xstrdup (_("This command is not documented.")); gdbpy_ref<> self_ref = gdbpy_ref<>::new_reference (self); @@ -513,12 +513,12 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kw) allow_unknown = PyObject_HasAttr (self, invoke_cst); cmd = add_prefix_cmd (cmd_name.get (), (enum command_class) cmdtype, - NULL, docstring, &obj->sub_list, + NULL, docstring.release (), &obj->sub_list, allow_unknown, cmd_list); } else cmd = add_cmd (cmd_name.get (), (enum command_class) cmdtype, - docstring, cmd_list); + docstring.release (), cmd_list); /* If successful, the above takes ownership of the name, since we set name_allocated, so release it. */ @@ -540,7 +540,6 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kw) } catch (const gdb_exception &except) { - xfree (docstring); gdbpy_convert_exception (except); return -1; } -- 2.25.4