Repository: jpype-project/jpype (language: python)

Issue:
Crash on JArray
Due to a typo rather than trying to construct an array of doubles with ``JDouble[100]`` I accidentally typed ``JArray[100]``.  Rather than giving an error it instead crashed with a segfault.

Something is clearly going wrong in this branch likely because the type of the unspecified array is null.   

Code to replicate:
```
import jpype
jpype.startJVM()
ja = jpype.JArray[100]
```

There is no obvious meaning for trying to create and unspecified array of arrays.  So it should have failed with a TypeError rather than crashing.


Discussion / hints:

Fix patch:
diff --git a/doc/CHANGELOG.rst b/doc/CHANGELOG.rst
index b41895c87..004106370 100644
--- a/doc/CHANGELOG.rst
+++ b/doc/CHANGELOG.rst
@@ -6,6 +6,8 @@ This changelog *only* contains changes from the *first* pypi release (0.5.4.3) o
 Latest Changes:
 - **1.4.2_dev0 - 2022-10-26**
 
+  - Fixed crash when calling subscript on JArray.
+
   - Fixed direct byte buffers not reporting nbytes correctly when cast to memoryview.
 
 - **1.4.1 - 2022-10-26**
diff --git a/native/python/pyjp_class.cpp b/native/python/pyjp_class.cpp
index 2b9bab9da..a04150dda 100644
--- a/native/python/pyjp_class.cpp
+++ b/native/python/pyjp_class.cpp
@@ -716,6 +716,12 @@ static PyObject *PyJPClass_array(PyJPClass *self, PyObject *item)
 	JPContext *context = PyJPModule_getContext();
 	JPJavaFrame frame = JPJavaFrame::outer(context);
 
+	if (self->m_Class == NULL)
+	{
+		PyErr_Format(PyExc_TypeError, "Cannot instantiate unspecified array type");
+		return NULL;
+	}
+
 	if (PyIndex_Check(item))
 	{
 		long sz = PyLong_AsLong(item);


Test patch:
diff --git a/test/jpypetest/test_array.py b/test/jpypetest/test_array.py
index 278cc1b10..6f0c60855 100644
--- a/test/jpypetest/test_array.py
+++ b/test/jpypetest/test_array.py
@@ -597,3 +597,7 @@ def testShortcut(self):
         # Test multidimensional
         self.assertEqual(JDouble[5, 5].getClass(), JArray(JDouble, 2)(5).getClass())
         self.assertEqual(JObject[5, 5].getClass(), JArray(JObject, 2)(5).getClass())
+
+    def testJArrayIndex(self):
+        with self.assertRaises(TypeError):
+            jpype.JArray[10]