public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix two Python calls that don't check for errors
@ 2023-06-29 13:12 Tom Tromey
  2023-07-03 16:28 ` Andrew Burgess
  0 siblings, 1 reply; 2+ messages in thread
From: Tom Tromey @ 2023-06-29 13:12 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

PyModule_AddObject steals a reference on success, but not on error,
which is why we have gdb_pymodule_addobject.  I found one spot still
calling the former, which could in theory leak memory on failure.
This patch fixes this.

In the same function I found an unchecked call to
PyDict_SetItemString.  This patch fixes this as well.
---
 gdb/python/py-disasm.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/gdb/python/py-disasm.c b/gdb/python/py-disasm.c
index 0af089f6b3e..6f0fed137e6 100644
--- a/gdb/python/py-disasm.c
+++ b/gdb/python/py-disasm.c
@@ -1647,11 +1647,15 @@ gdbpy_initialize_disasm ()
   gdb_disassembler_module = PyModule_Create (&python_disassembler_module_def);
   if (gdb_disassembler_module == nullptr)
     return -1;
-  PyModule_AddObject(gdb_module, "disassembler", gdb_disassembler_module);
+  if (gdb_pymodule_addobject (gdb_module, "disassembler",
+			      gdb_disassembler_module) < 0)
+    return -1;
 
   /* This is needed so that 'import _gdb.disassembler' will work.  */
   PyObject *dict = PyImport_GetModuleDict ();
-  PyDict_SetItemString (dict, "_gdb.disassembler", gdb_disassembler_module);
+  if (PyDict_SetItemString (dict, "_gdb.disassembler",
+			    gdb_disassembler_module) < 0)
+    return -1;
 
   for (int i = 0; i <= (int) dis_style_comment_start; ++i)
     {
-- 
2.40.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fix two Python calls that don't check for errors
  2023-06-29 13:12 [PATCH] Fix two Python calls that don't check for errors Tom Tromey
@ 2023-07-03 16:28 ` Andrew Burgess
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Burgess @ 2023-07-03 16:28 UTC (permalink / raw)
  To: Tom Tromey via Gdb-patches, gdb-patches; +Cc: Tom Tromey

Tom Tromey via Gdb-patches <gdb-patches@sourceware.org> writes:

> PyModule_AddObject steals a reference on success, but not on error,
> which is why we have gdb_pymodule_addobject.  I found one spot still
> calling the former, which could in theory leak memory on failure.
> This patch fixes this.
>
> In the same function I found an unchecked call to
> PyDict_SetItemString.  This patch fixes this as well.

Thanks for fixing this.

Approved-By: Andrew Burgess <aburgess@redhat.com>

Thanks,
Andrew

> ---
>  gdb/python/py-disasm.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/gdb/python/py-disasm.c b/gdb/python/py-disasm.c
> index 0af089f6b3e..6f0fed137e6 100644
> --- a/gdb/python/py-disasm.c
> +++ b/gdb/python/py-disasm.c
> @@ -1647,11 +1647,15 @@ gdbpy_initialize_disasm ()
>    gdb_disassembler_module = PyModule_Create (&python_disassembler_module_def);
>    if (gdb_disassembler_module == nullptr)
>      return -1;
> -  PyModule_AddObject(gdb_module, "disassembler", gdb_disassembler_module);
> +  if (gdb_pymodule_addobject (gdb_module, "disassembler",
> +			      gdb_disassembler_module) < 0)
> +    return -1;
>  
>    /* This is needed so that 'import _gdb.disassembler' will work.  */
>    PyObject *dict = PyImport_GetModuleDict ();
> -  PyDict_SetItemString (dict, "_gdb.disassembler", gdb_disassembler_module);
> +  if (PyDict_SetItemString (dict, "_gdb.disassembler",
> +			    gdb_disassembler_module) < 0)
> +    return -1;
>  
>    for (int i = 0; i <= (int) dis_style_comment_start; ++i)
>      {
> -- 
> 2.40.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-07-03 16:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-29 13:12 [PATCH] Fix two Python calls that don't check for errors Tom Tromey
2023-07-03 16:28 ` Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).