public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: George Barrett <bob@bob131.so>
To: gdb-patches@sourceware.org
Cc: George Barrett <bob@bob131.so>
Subject: [PATCH] guile: fix make-value with pointer type
Date: Thu, 20 May 2021 08:26:28 +1000	[thread overview]
Message-ID: <mg5&5oczhoi92axcoavjf/q50rsheimopwi/wynci&5/xqwsjyki@mail.bob131.so> (raw)

Calling the `make-value' procedure with an integer value and a pointer
type for the #:type argument triggers a failed assertion in
`get_unsigned_type_max', as that function doesn't consider pointers to
be an unsigned type. This commit fixes the issue by adding a separate
code path for pointers.

gdb/ChangeLog:

2021-05-20  George Barrett  <bob@bob131.so>

	* guile/scm-math.c (vlscm_convert_typed_number): Add code path
	for handling conversions to pointer types without failing an
	assert.

gdb/testsuite/ChangeLog:

2021-05-20  George Barrett  <bob@bob131.so>

	* gdb.guile/scm-math.exp (test_value_numeric_ops): Add test
	for creating pointers with make-value.
---
 gdb/guile/scm-math.c                 | 18 ++++++++++++++++--
 gdb/testsuite/gdb.guile/scm-math.exp |  9 +++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/gdb/guile/scm-math.c b/gdb/guile/scm-math.c
index d9fd6718196..9fc4ad505b4 100644
--- a/gdb/guile/scm-math.c
+++ b/gdb/guile/scm-math.c
@@ -524,8 +524,7 @@ vlscm_convert_typed_number (const char *func_name, int obj_arg_pos, SCM obj,
 			    int type_arg_pos, SCM type_scm, struct type *type,
 			    struct gdbarch *gdbarch, SCM *except_scmp)
 {
-  if (is_integral_type (type)
-      || type->code () == TYPE_CODE_PTR)
+  if (is_integral_type (type))
     {
       if (type->is_unsigned ())
 	{
@@ -558,6 +557,21 @@ vlscm_convert_typed_number (const char *func_name, int obj_arg_pos, SCM obj,
 	  return value_from_longest (type, gdbscm_scm_to_longest (obj));
 	}
     }
+  else if (type->code () == TYPE_CODE_PTR)
+    {
+      gdb_mpz max (1);
+      mpz_mul_2exp (max.val, max.val, gdbarch_addr_bit (type->arch ()));
+      mpz_sub_ui (max.val, max.val, 1);
+      if (!scm_is_unsigned_integer (obj, 0, max.as_integer<CORE_ADDR> ()))
+	{
+	  *except_scmp
+	    = gdbscm_make_out_of_range_error (func_name,
+					      obj_arg_pos, obj,
+					_("value out of range for type"));
+	  return NULL;
+	}
+      return value_from_pointer (type, gdbscm_scm_to_ulongest (obj));
+    }
   else if (type->code () == TYPE_CODE_FLT)
     return value_from_host_double (type, scm_to_double (obj));
   else
diff --git a/gdb/testsuite/gdb.guile/scm-math.exp b/gdb/testsuite/gdb.guile/scm-math.exp
index ba975454ce5..c128b708d61 100644
--- a/gdb/testsuite/gdb.guile/scm-math.exp
+++ b/gdb/testsuite/gdb.guile/scm-math.exp
@@ -137,6 +137,15 @@ proc test_value_numeric_ops {} {
     gdb_test "gu (print (value-sub b a))" \
 	"= 3" "subtract two pointer values"
 
+    # Test pointer creation.
+
+    gdb_test_no_output "gu (define void-pointer-type (type-pointer (arch-void-type (current-arch))))"
+    gdb_scm_test_silent_cmd "gu (define null-pointer (make-value 0 #:type void-pointer-type))" \
+	"test make-value with pointer type"
+    gdb_test "gu (print null-pointer)" "= 0x0"
+    gdb_test "gu (print (equal? (value-type null-pointer) void-pointer-type))" \
+	"= #t"
+
     # Test some invalid operations.
 
     gdb_test_multiple "gu (print (value-add i '()))" "catch error in guile type conversion" {
-- 
2.31.1

             reply	other threads:[~2021-05-19 22:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-19 22:26 George Barrett [this message]
2021-06-03 11:42 ` Andrew Burgess
2021-06-03 19:12   ` George Barrett

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='mg5&5oczhoi92axcoavjf/q50rsheimopwi/wynci&5/xqwsjyki@mail.bob131.so' \
    --to=bob@bob131.so \
    --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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).