This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 2/7] Preserve sign when converting gdb.Value to Python int


PR python/20126 points out that sometimes the conversion of a
gdb.Value can result in a negative Python integer.  This happens
because valpy_int does not examine the signedness of the value's type.

gdb/ChangeLog
2018-09-14  Tom Tromey  <tom@tromey.com>

	PR python/20126:
	* python/py-value.c (valpy_int): Respect type sign.

gdb/testsuite/ChangeLog
2018-09-14  Tom Tromey  <tom@tromey.com>

	PR python/20126:
	* gdb.python/py-value.exp (test_value_numeric_ops): Add
	signed-ness conversion tests.
---
 gdb/ChangeLog                         | 5 +++++
 gdb/python/py-value.c                 | 5 ++++-
 gdb/testsuite/ChangeLog               | 6 ++++++
 gdb/testsuite/gdb.python/py-value.exp | 5 +++++
 4 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 9abcf9212e6..5c6792f85fc 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1514,7 +1514,10 @@ valpy_int (PyObject *self)
     }
   END_CATCH
 
-  return gdb_py_object_from_longest (l);
+  if (TYPE_UNSIGNED (type))
+    return gdb_py_object_from_ulongest (l);
+  else
+    return gdb_py_object_from_longest (l);
 }
 #endif
 
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index 9e8fa15c28e..aed50d1c8cf 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -140,6 +140,11 @@ proc test_value_numeric_ops {} {
   gdb_test "python print ('result = ' + str(\[1,2,3\]\[gdb.Value(0)\]))" \
     "result = 1" "use value as array index"
 
+  gdb_test "python print('%x' % int(gdb.parse_and_eval('-1ull')))" \
+      "f+" "int conversion respect type sign"
+  gdb_test "python print('%x' % long(gdb.parse_and_eval('-1ull')))" \
+      "f+" "long conversion respect type sign"
+
   # Test some invalid operations.
 
   gdb_test_multiple "python print ('result = ' + str(i+'foo'))" "catch error in python type conversion" {
-- 
2.17.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]