check possible recursive _as_parameter_ to prevent segfault (closes #1838)
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 04b07cc..6a2b7ce 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -2004,10 +2004,14 @@
     PyCArgObject *parg;
     struct fielddesc *fd;
     PyObject *as_parameter;
+    int res;
 
     /* If the value is already an instance of the requested type,
        we can use it as is */
-    if (1 == PyObject_IsInstance(value, type)) {
+    res = PyObject_IsInstance(value, type);
+    if (res == -1)
+        return NULL;
+    if (res) {
         Py_INCREF(value);
         return value;
     }
@@ -2036,7 +2040,12 @@
 
     as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
     if (as_parameter) {
+        if (Py_EnterRecursiveCall("while processing _as_parameter_")) {
+            Py_DECREF(as_parameter);
+            return NULL;
+        }
         value = PyCSimpleType_from_param(type, as_parameter);
+        Py_LeaveRecursiveCall();
         Py_DECREF(as_parameter);
         return value;
     }