[3/4] Clean up 0-length handling in gdbpy_create_lazy_string_object

Message ID 20241120-noop-string-printer-v1-3-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
  gdbpy_create_lazy_string_object will throw an exception if you pass it
a NULL pointer without also setting length=0 -- the default,
length==-1, will fail.  This seems bizarre.  Furthermore, it doesn't
make sense to do this check for array types, as an array can have a
zero length.  This patch cleans up the check and makes it specific to
TYPE_CODE_PTR.
---
 gdb/python/py-lazy-string.c                 | 25 +++++++++++++++++--------
 gdb/testsuite/gdb.python/py-lazy-string.exp |  2 ++
 2 files changed, 19 insertions(+), 8 deletions(-)
  

Patch

diff --git a/gdb/python/py-lazy-string.c b/gdb/python/py-lazy-string.c
index 30a171d4c0b02c7db3e3b8e885b95a7c2dbe7e4e..1226c280174ac5c9c2c26f6bdf36508a5047a0ab 100644
--- a/gdb/python/py-lazy-string.c
+++ b/gdb/python/py-lazy-string.c
@@ -182,14 +182,6 @@  gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
       return NULL;
     }
 
-  if (address == 0 && length != 0)
-    {
-      PyErr_SetString (gdbpy_gdb_memory_error,
-		       _("Cannot create a lazy string with address 0x0, " \
-			 "and a non-zero length."));
-      return NULL;
-    }
-
   if (!type)
     {
       PyErr_SetString (PyExc_RuntimeError,
@@ -216,6 +208,23 @@  gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
 	  }
 	break;
       }
+
+    case TYPE_CODE_PTR:
+      if (address == 0)
+	{
+	  if (length > 0)
+	    {
+	      PyErr_SetString (gdbpy_gdb_memory_error,
+			       _("Cannot create a lazy string with address 0x0, " \
+				 "and a non-zero length."));
+	      return nullptr;
+	    }
+	  length = 0;
+	}
+      break;
+
+    default:
+      gdb_assert_not_reached ("invalid type in gdbpy_create_lazy_string_object");
     }
 
   str_obj = PyObject_New (lazy_string_object, &lazy_string_object_type);
diff --git a/gdb/testsuite/gdb.python/py-lazy-string.exp b/gdb/testsuite/gdb.python/py-lazy-string.exp
index 0650c67d5e1cc75b00d2d3cb8d8ec3433979ee52..d212e613640bcbc01f40dd934e793ef5aff6ba05 100644
--- a/gdb/testsuite/gdb.python/py-lazy-string.exp
+++ b/gdb/testsuite/gdb.python/py-lazy-string.exp
@@ -48,6 +48,8 @@  gdb_test "python print(null.lazy_string(length=3).value())" \
 gdb_test "python print(null.lazy_string(length=-2))" \
     "ValueError.*: Invalid length.*Error occurred in Python.*" \
     "bad length"
+gdb_py_test_silent_cmd "python nullstr = null.lazy_string()" \
+    "create null lazy string with default length" 1
 
 foreach var_spec { { "ptr" "pointer" "const char \\*" -1 } \
 		   { "array" "array" "const char \\[6\\]" 6 } \