Commit d9638802e6c0f3353f3430e561b97485863c82de

Authored by Perry Werneck
1 parent 20b6613c
Exists in master

Fixing segfault when the action create method fails.

Showing 2 changed files with 24 additions and 7 deletions   Show diff stats
src/action/new.cc
... ... @@ -42,13 +42,27 @@
42 42 DLL_PRIVATE PyObject * py3270_action_new_from_session(PyObject *session, void *action) {
43 43  
44 44 pyAction * pObj = (pyAction *) _PyObject_New(&py3270_action_type);
45   -
  45 + pObj->action = nullptr;
46 46 pObj->session = (pySession *) session;
47   - pObj->action = pObj->session->host->getAction((const LIB3270_ACTION *) action);
  47 + Py_INCREF(pObj->session);
48 48  
49   - debug("%s: ob_refcnt@%p=%ld",__FUNCTION__,pObj,((PyObject *) pObj)->ob_refcnt);
  49 + try {
50 50  
51   - Py_INCREF(pObj->session);
  51 + pObj->action = pObj->session->host->getAction((const LIB3270_ACTION *) action);
  52 +
  53 + } catch(const std::exception &e) {
  54 +
  55 + Py_DECREF((PyObject *) pObj);
  56 + PyErr_SetString(PyExc_RuntimeError, e.what());
  57 + return NULL;
  58 +
  59 + } catch(...) {
  60 +
  61 + Py_DECREF((PyObject *) pObj);
  62 + PyErr_SetString(PyExc_RuntimeError, "Unexpected error creating action object");
  63 + return NULL;
  64 +
  65 + }
52 66  
53 67 return (PyObject *) pObj;
54 68  
... ... @@ -62,7 +76,9 @@ void py3270_action_dealloc(PyObject * self) {
62 76  
63 77 Py_DECREF(pObj->session);
64 78  
65   - delete pObj->action;
  79 + if(pObj->action)
  80 + delete pObj->action;
  81 +
66 82 pObj->action = nullptr;
67 83  
68 84 }
... ...
testprograms/sample.py
... ... @@ -7,7 +7,7 @@ import tn3270
7 7 print("Using TN3270 Version " + tn3270.version())
8 8 print(tn3270.revision())
9 9  
10   -session = tn3270.Session("")
  10 +session = tn3270.Session(":a")
11 11  
12 12 print("Using tn3270 version " + session.version + " revision " + session.revision)
13 13  
... ... @@ -16,6 +16,8 @@ print("Using tn3270 version " + session.version + " revision " + session.revisio
16 16 #print(session.connected)
17 17 #print(session.url)
18 18  
  19 +print(session.reconnect)
  20 +
19 21 #
20 22 # Can reconnect? If yes do it!
21 23 #
... ... @@ -23,7 +25,6 @@ if session.reconnect.activatable:
23 25 print("Reconnecting...")
24 26 session.reconnect().wait(10)
25 27  
26   -print(session.reconnect)
27 28 print(session.connected)
28 29  
29 30 #print('----------------------')
... ...