From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 5/6] Safe wrappers for some string conversions
Date: Fri, 23 Oct 2026 13:15:50 -0600 [thread overview]
Message-ID: <20261023-python-safety-fixmes-v1-5-055b9e1d237b@tromey.com> (raw)
In-Reply-To: <20261023-python-safety-fixmes-v1-0-055b9e1d237b@tromey.com>
This adds a couple of new Python wrappers for "safe-style" string
conversions. This allows some FIXME removal.
Note that I've added a new gdbpy_borrowed_ref constructor, from
gdbpy_opt_borrowed_ref. This is checked at runtime to preserve the
invariant.
---
gdb/python/py-frame.c | 9 ++-------
gdb/python/py-ref.h | 7 +++++++
gdb/python/py-tui.c | 7 +------
gdb/python/py-utils.c | 20 ++++++++++++++++++++
gdb/python/python-internal.h | 4 ++++
5 files changed, 34 insertions(+), 13 deletions(-)
diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c
index e4dd9e9252e..e34d4b5d2e9 100644
--- a/gdb/python/py-frame.c
+++ b/gdb/python/py-frame.c
@@ -355,13 +355,8 @@ frame_object::read_var (gdbpy_borrowed_ref<> args,
var = symbol_object_to_symbol (sym_obj);
else if (gdbpy_is_string (sym_obj))
{
- gdb::unique_xmalloc_ptr<char>
- var_name (python_string_to_target_string (sym_obj));
-
- /* FIXME: Python safety. python_string_to_target_string should
- throw on error. */
- if (var_name == nullptr)
- throw gdb_python_exception ();
+ gdb::unique_xmalloc_ptr<char> var_name
+ = safe_python_string_to_target_string (sym_obj);
if (block_obj != nullptr)
{
diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h
index 3d0373b7001..7cbfb03b103 100644
--- a/gdb/python/py-ref.h
+++ b/gdb/python/py-ref.h
@@ -108,6 +108,13 @@ class gdbpy_borrowed_ref : public gdbpy_opt_borrowed_ref<T>
gdb_assert (this->m_obj != nullptr);
}
+ template<typename U>
+ gdbpy_borrowed_ref (gdbpy_opt_borrowed_ref<U> obj)
+ : gdbpy_opt_borrowed_ref<T> (obj)
+ {
+ gdb_assert (this->m_obj != nullptr);
+ }
+
gdbpy_borrowed_ref (std::nullptr_t) = delete;
/* Allow a (checked) conversion to any subclass of T. */
diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c
index 388e441ed55..be8c22aac27 100644
--- a/gdb/python/py-tui.c
+++ b/gdb/python/py-tui.c
@@ -506,13 +506,8 @@ gdbpy_tui_window::set_title (gdbpy_opt_borrowed_ref<> new_title)
gdbpy_err_format (PyExc_TypeError,
_("Cannot delete \"title\" attribute."));
- /* FIXME: Python safety. Ideally python_string_to_host_string would
- throw on error, but this can't be done until more of the code has
- been converted. */
gdb::unique_xmalloc_ptr<char> value
- = python_string_to_host_string (new_title);
- if (value == nullptr)
- throw gdb_python_exception ();
+ = safe_python_string_to_host_string (new_title);
window->set_title (value.get ());
}
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index 1c68b206601..40433256c62 100644
--- a/gdb/python/py-utils.c
+++ b/gdb/python/py-utils.c
@@ -119,6 +119,16 @@ python_string_to_target_string (PyObject *obj)
return unicode_to_target_string (str.get ());
}
+/* Wrapper for python_string_to_target_string that throws on failure. */
+gdb::unique_xmalloc_ptr<char>
+safe_python_string_to_target_string (gdbpy_borrowed_ref<> obj)
+{
+ gdb::unique_xmalloc_ptr<char> result = python_string_to_target_string (obj);
+ if (result == nullptr)
+ throw gdb_python_exception ();
+ return result;
+}
+
/* Converts a python string (8-bit or unicode) to a target string in the
target's charset. Returns NULL on error, with a python exception
set.
@@ -147,6 +157,16 @@ python_string_to_host_string (PyObject *obj)
return unicode_to_encoded_string (str.get (), host_charset ());
}
+/* Wrapper for python_string_to_host_string that throws on failure. */
+gdb::unique_xmalloc_ptr<char>
+safe_python_string_to_host_string (gdbpy_borrowed_ref<> obj)
+{
+ gdb::unique_xmalloc_ptr<char> result = python_string_to_host_string (obj);
+ if (result == nullptr)
+ throw gdb_python_exception ();
+ return result;
+}
+
/* See python/python-internal.h. */
gdbpy_ref<>
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 5515820411b..ee1a19131bd 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -1017,8 +1017,12 @@ void gdbpy_print_stack_or_quit ();
gdbpy_ref<> python_string_to_unicode (PyObject *obj);
gdb::unique_xmalloc_ptr<char> unicode_to_target_string (PyObject *unicode_str);
gdb::unique_xmalloc_ptr<char> python_string_to_target_string (PyObject *obj);
+gdb::unique_xmalloc_ptr<char> safe_python_string_to_target_string
+ (gdbpy_borrowed_ref<> obj);
gdbpy_ref<> python_string_to_target_python_string (PyObject *obj);
gdb::unique_xmalloc_ptr<char> python_string_to_host_string (PyObject *obj);
+gdb::unique_xmalloc_ptr<char> safe_python_string_to_host_string
+ (gdbpy_borrowed_ref<> obj);
/* Convert a host string STR to a python string. */
--
2.49.0
next prev parent reply other threads:[~2026-10-23 19:18 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-23 19:15 [PATCH 0/6] Remove some Python safety FIXME comments Tom Tromey
2026-10-23 19:15 ` [PATCH 1/6] Add safe_target_to_connection_object Tom Tromey
2026-10-23 19:15 ` [PATCH 2/6] Add safety wrappers for from_{longest,ulongest} Tom Tromey
2026-10-23 19:15 ` [PATCH 3/6] Add gdbpy_safe_invalid_object_repr Tom Tromey
2026-10-23 19:15 ` [PATCH 4/6] Some symbol-related Python safety fixes Tom Tromey
2026-10-23 19:15 ` Tom Tromey [this message]
2026-10-23 19:15 ` [PATCH 6/6] Some frame-related " Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261023-python-safety-fixmes-v1-5-055b9e1d237b@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox