Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Jan Vrany <jan@vrany.io>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan@vrany.io>
Subject: [PATCH] gdb/python: prevent conversion from pointer to bool in to_python()
Date: Wed, 30 Sep 2026 09:36:09 +0000	[thread overview]
Message-ID: <20260930093550.1288712-1-jan@vrany.io> (raw)

While converting some Python code to use new "safety" APIs I realized
that calling to_python() with a pointer like (for example):

   to_python (symtab->compunit ().objfile ())

calls to_python(bool) which is unlikely to be what the user wants.
This commit changes the code to make it a compile-time error.

After trying several approaches, the least intrusive seemed to be
handling bool specially in to_python() template for integral types and
removing to_python(bool) altogether. This way, the above example fails
with:

    error: no matching function for call to ‘to_python(objfile*)’

Tested on x86_64-linux.
---
 gdb/python/py-safety.h | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/gdb/python/py-safety.h b/gdb/python/py-safety.h
index 3294f38c8b6..9414951b9ba 100644
--- a/gdb/python/py-safety.h
+++ b/gdb/python/py-safety.h
@@ -71,17 +71,13 @@ namespace safety_details
    used in gdb, these are all Python-facing and will return NULL on
    error.  */
 
-static inline PyObject *
-to_python (bool value)
-{
-  return PyBool_FromLong (value);
-}
-
 template<typename T, typename = gdb::Requires<std::is_integral<T>>>
 static inline PyObject *
 to_python (T value)
 {
-  if constexpr (std::is_signed<T>::value)
+  if constexpr (std::is_same<T, bool>::value)
+    return PyBool_FromLong (value);
+  else if constexpr (std::is_signed<T>::value)
     return gdb_py_object_from_longest (value).release ();
   else
     return gdb_py_object_from_ulongest (value).release ();
-- 
2.53.0



             reply	other threads:[~2026-09-30  9:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-30  9:36 Jan Vrany [this message]
2026-09-30 14:18 ` 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=20260930093550.1288712-1-jan@vrany.io \
    --to=jan@vrany.io \
    --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