From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1726) id 8B4F8385703E; Sat, 28 May 2022 10:07:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8B4F8385703E Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Andrew Burgess To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb: use gdb::unique_xmalloc_ptr for docs in cmdpy_init X-Act-Checkin: binutils-gdb X-Git-Author: Andrew Burgess X-Git-Refname: refs/heads/master X-Git-Oldrev: 6094a48ec821a52731172f3471dce4923a482cd1 X-Git-Newrev: 0e77ff2c86a163fe11135633837c8f19aee31ca0 Message-Id: <20220528100740.8B4F8385703E@sourceware.org> Date: Sat, 28 May 2022 10:07:40 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 28 May 2022 10:07:40 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D0e77ff2c86a1= 63fe11135633837c8f19aee31ca0 commit 0e77ff2c86a163fe11135633837c8f19aee31ca0 Author: Andrew Burgess Date: Sat May 14 10:55:59 2022 +0100 gdb: use gdb::unique_xmalloc_ptr for docs in cmdpy_init =20 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. =20 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. =20 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). Diff: --- 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 *k= w) const char *name; int cmdtype; int completetype =3D -1; - char *docstring =3D NULL; struct cmd_list_element **cmd_list; static const char *keywords[] =3D { "name", "command_class", "completer_= class", "prefix", NULL }; @@ -484,19 +483,20 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject = *kw) is_prefix =3D cmp > 0; } =20 + gdb::unique_xmalloc_ptr docstring =3D nullptr; if (PyObject_HasAttr (self, gdbpy_doc_cst)) { gdbpy_ref<> ds_obj (PyObject_GetAttr (self, gdbpy_doc_cst)); =20 if (ds_obj !=3D NULL && gdbpy_is_string (ds_obj.get ())) { - docstring =3D python_string_to_host_string (ds_obj.get ()).release (); - if (docstring =3D=3D NULL) + docstring =3D python_string_to_host_string (ds_obj.get ()); + if (docstring =3D=3D nullptr) return -1; } } - if (! docstring) - docstring =3D xstrdup (_("This command is not documented.")); + if (docstring =3D=3D nullptr) + docstring =3D make_unique_xstrdup (_("This command is not documented."= )); =20 gdbpy_ref<> self_ref =3D gdbpy_ref<>::new_reference (self); =20 @@ -513,12 +513,12 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject = *kw) allow_unknown =3D PyObject_HasAttr (self, invoke_cst); cmd =3D 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 =3D add_cmd (cmd_name.get (), (enum command_class) cmdtype, - docstring, cmd_list); + docstring.release (), cmd_list); =20 /* 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 *k= w) } catch (const gdb_exception &except) { - xfree (docstring); gdbpy_convert_exception (except); return -1; }