]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/python/py-inferior.c
[gdb/python] Throw MemoryError in inferior.read_memory if malloc fails
[thirdparty/binutils-gdb.git] / gdb / python / py-inferior.c
index 795ac655ddd5748bad9cb29254026b54da364170..a1042ee72ac733091f7572bc04b072546d3c1519 100644 (file)
@@ -555,6 +555,18 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
       || get_addr_from_python (length_obj, &length) < 0)
     return NULL;
 
+  if (length == 0)
+    {
+      PyErr_SetString (PyExc_ValueError,
+                      _("Argument 'count' should be greater than zero"));
+      return NULL;
+    }
+
+  void *p = malloc (length);
+  if (p == nullptr)
+    return PyErr_NoMemory ();
+  buffer.reset ((gdb_byte *) p);
+
   try
     {
       /* Use this scoped-restore because we want to be able to read
@@ -562,8 +574,6 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
       scoped_restore_current_inferior_for_memory restore_inferior
        (inf->inferior);
 
-      buffer.reset ((gdb_byte *) xmalloc (length));
-
       read_memory (addr, buffer.get (), length);
     }
   catch (const gdb_exception &except)