[2/4] Reject non-string types in gdb.Value.lazy_string

Message ID 20241120-noop-string-printer-v1-2-40c44526fb4a@adacore.com
State New
Headers
Series Fix string display in DAP |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Tom Tromey Nov. 20, 2024, 4:31 p.m. UTC
  Currently, gdb.Value.lazy_string will allow the conversion of any
object to a "lazy string".  However, this was never the intent and is
weird besides.  This patch changes this code to correctly throw an
exception in the non-matching cases.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=20769
---
 gdb/python/py-lazy-string.c                 |  4 +---
 gdb/python/py-value.c                       | 13 ++-----------
 gdb/testsuite/gdb.python/py-lazy-string.c   |  1 +
 gdb/testsuite/gdb.python/py-lazy-string.exp |  5 +++++
 4 files changed, 9 insertions(+), 14 deletions(-)
  

Patch

diff --git a/gdb/python/py-lazy-string.c b/gdb/python/py-lazy-string.c
index 4898a1f8299e1d70e4ca14162b3fc242e6dada3c..30a171d4c0b02c7db3e3b8e885b95a7c2dbe7e4e 100644
--- a/gdb/python/py-lazy-string.c
+++ b/gdb/python/py-lazy-string.c
@@ -264,9 +264,7 @@  stpy_lazy_string_elt_type (lazy_string_object *lazy)
     case TYPE_CODE_ARRAY:
       return check_typedef (realtype->target_type ());
     default:
-      /* This is done to preserve existing behaviour.  PR 20769.
-	 E.g., gdb.parse_and_eval("my_int_variable").lazy_string().type.  */
-      return realtype;
+      gdb_assert_not_reached ("invalid lazy string");
     }
 }
 
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index eef3841924fabf31d5b203c7227e020c3e27a0a1..2e4ff67044ceca9df489b328e8f10d2e6055ee53 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -520,14 +520,7 @@  valpy_get_dynamic_type (PyObject *self, void *closure)
    If LENGTH is provided then the length parameter is set to LENGTH.
    Otherwise if the value is an array of known length then the array's length
    is used.  Otherwise the length will be set to -1 (meaning first null of
-   appropriate with).
-
-   Note: In order to not break any existing uses this allows creating
-   lazy strings from anything.  PR 20769.  E.g.,
-   gdb.parse_and_eval("my_int_variable").lazy_string().
-   "It's easier to relax restrictions than it is to impose them after the
-   fact."  So we should be flagging any unintended uses as errors, but it's
-   perhaps too late for that.  */
+   appropriate with).  */
 
 static PyObject *
 valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
@@ -595,9 +588,7 @@  valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
 	  addr = value_as_address (value);
 	  break;
 	default:
-	  /* Should flag an error here.  PR 20769.  */
-	  addr = value->address ();
-	  break;
+	  error (_("Cannot make lazy string from this object"));
 	}
 
       str_obj = gdbpy_create_lazy_string_object (addr, length, user_encoding,
diff --git a/gdb/testsuite/gdb.python/py-lazy-string.c b/gdb/testsuite/gdb.python/py-lazy-string.c
index 326523dc5f98a999cd3e54073d3605f120f73b82..805ba9297f471b7e7d6e17f2712f985d853fb8ba 100644
--- a/gdb/testsuite/gdb.python/py-lazy-string.c
+++ b/gdb/testsuite/gdb.python/py-lazy-string.c
@@ -24,6 +24,7 @@  main ()
   const char array[] = "array";
   pointer typedef_ptr = "typedef pointer";
   const char *null = 0;
+  int not_a_string = 23;
 
   return 0; /* break here */
 }
diff --git a/gdb/testsuite/gdb.python/py-lazy-string.exp b/gdb/testsuite/gdb.python/py-lazy-string.exp
index a00f47b1e2a2767da5fa9a0fe00d39ff6fa2a93e..0650c67d5e1cc75b00d2d3cb8d8ec3433979ee52 100644
--- a/gdb/testsuite/gdb.python/py-lazy-string.exp
+++ b/gdb/testsuite/gdb.python/py-lazy-string.exp
@@ -73,3 +73,8 @@  foreach var_spec { { "ptr" "pointer" "const char \\*" -1 } \
 	#gdb_test "python print ($var.lazy_string(length=0).value())" "\"\"" "empty lazy string value"
     }
 }
+
+gdb_py_test_silent_cmd "python nas = gdb.parse_and_eval('not_a_string')" \
+    "get not_a_string" 1
+gdb_test "python print(nas.lazy_string())" \
+    "Cannot make lazy string from this object"