From 78eed2a48453c66c34336fc33a13fb9de91e8241 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Thu, 18 Apr 2019 18:24:51 -0300 Subject: [PATCH] Fixing segfaults on session creation error. --- src/native/get.cc | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++----------- src/native/init.cc | 1 + src/native/network.cc | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------- src/native/screen.cc | 46 ++++++++++++++++++++++++++++++++++++++-------- src/native/set.cc | 5 +++++ 5 files changed, 156 insertions(+), 44 deletions(-) diff --git a/src/native/get.cc b/src/native/get.cc index d9967c2..f967095 100644 --- a/src/native/get.cc +++ b/src/native/get.cc @@ -37,6 +37,11 @@ * */ int tn3270_get_version(h3270::session *ses, char* str, int strlen) { + + if(!ses) { + return -1; + } + try { strncpy(str,ses->get_version().c_str(),strlen); } catch(std::exception &e) { @@ -52,6 +57,10 @@ */ int tn3270_get_revision(h3270::session *ses, char* str, int strlen) { + if(!ses) { + return -1; + } + try { strncpy(str,ses->get_revision().c_str(),strlen); } catch(std::exception &e) { @@ -63,6 +72,10 @@ int tn3270_get_cstate(h3270::session *ses) { + if(!ses) { + return -1; + } + trace_to_file("%s: %d",__FUNCTION__,(int) ses->get_cstate()); try { @@ -76,6 +89,10 @@ int tn3270_get_program_message(h3270::session *ses) { + if(!ses) { + return -1; + } + try { return (int) ses->get_program_message(); } catch(std::exception &e) { @@ -86,6 +103,11 @@ } int tn3270_get_secure(h3270::session *ses) { + + if(!ses) { + return -1; + } + try { return (int) ses->get_secure(); } catch(std::exception &e) { @@ -97,6 +119,10 @@ int tn3270_get_width(h3270::session *ses) { + if(!ses) { + return 0; + } + try { return (int) ses->get_width(); } catch(std::exception &e) { @@ -108,6 +134,10 @@ int tn3270_get_height(h3270::session *ses) { + if(!ses) { + return 0; + } + try { return (int) ses->get_height(); } catch(std::exception &e) { @@ -119,6 +149,10 @@ int tn3270_get_length(h3270::session *ses) { + if(!ses) { + return 0; + } + try { return (int) ses->get_length(); } catch(std::exception &e) { @@ -130,25 +164,33 @@ int tn3270_get_cursor_addr(h3270::session *ses) { - try { - return (int) ses->get_cursor_addr(); - } catch(std::exception &e) { - tn3270_lasterror = e.what(); - } + if(ses) { + + try { + return (int) ses->get_cursor_addr(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + + } return -1; } int tn3270_get_url(h3270::session *ses, char* str, int strlen) { - try { + if(ses) { - strncpy(str,ses->get_url().c_str(),strlen); - return 0; + try { - } catch(std::exception &e) { - tn3270_lasterror = e.what(); - } + strncpy(str,ses->get_url().c_str(),strlen); + return 0; + + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + + } return -1; } diff --git a/src/native/init.cc b/src/native/init.cc index 5a8d01a..ca79ef2 100644 --- a/src/native/init.cc +++ b/src/native/init.cc @@ -49,6 +49,7 @@ return h3270::session::create(name); } catch(std::exception &e) { tn3270_lasterror = e.what(); + trace_to_file("%s(%s)",__FUNCTION__,e.what()); } return nullptr; } diff --git a/src/native/network.cc b/src/native/network.cc index d1db075..332a81d 100644 --- a/src/native/network.cc +++ b/src/native/network.cc @@ -32,57 +32,91 @@ /*---[ Implement ]----------------------------------------------------------------------------------*/ int tn3270_connect(h3270::session *ses, const char *host, time_t wait) { - try { - debug("%s(%s,%d)",__FUNCTION__,host,(int) wait); - return ses->connect(host,wait); - } catch(std::exception &e) { - tn3270_lasterror = e.what(); + + if(ses) { + + try { + debug("%s(%s,%d)",__FUNCTION__,host,(int) wait); + return ses->connect(host,wait); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + } + return -1; } int tn3270_disconnect(h3270::session *ses) { - try { - return ses->disconnect(); - } catch(std::exception &e) { - tn3270_lasterror = e.what(); + + if(ses) { + + try { + return ses->disconnect(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + } + return -1; } int tn3270_is_connected(h3270::session *ses) { - try { - return (int) ses->is_connected(); - } catch(std::exception &e) { - tn3270_lasterror = e.what(); + + if(ses) { + + try { + return (int) ses->is_connected(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + } + return -1; } int tn3270_is_ready(h3270::session *ses) { - try { - return (int) ses->is_ready(); - } catch(std::exception &e) { - tn3270_lasterror = e.what(); + + if(ses) { + + try { + return (int) ses->is_ready(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + } + return -1; } int tn3270_wait_for_ready(h3270::session *ses, int seconds) { - try { - return (int) ses->wait_for_ready(seconds); - } catch(std::exception &e) { - tn3270_lasterror = e.what(); + + if(ses) { + + try { + return (int) ses->wait_for_ready(seconds); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + } return -1; } int tn3270_wait(h3270::session *ses, int seconds) { - try { - return (int) ses->wait(seconds); - } catch(std::exception &e) { - tn3270_lasterror = e.what(); + + if(ses) { + + try { + return (int) ses->wait(seconds); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + } return -1; } diff --git a/src/native/screen.cc b/src/native/screen.cc index 710c8f5..0897ae3 100644 --- a/src/native/screen.cc +++ b/src/native/screen.cc @@ -33,6 +33,10 @@ int tn3270_get_contents(h3270::session *ses, char* str, int sz) { + if(!ses) { + return -1; + } + try { std::string contents = ses->get_contents(); @@ -55,6 +59,10 @@ int tn3270_get_contents(h3270::session *ses, char* str, int sz) { int tn3270_get_string(h3270::session *ses, int addr, char* str, int strlen) { + if(!ses) { + return -1; + } + try { memset(str,0,strlen); strncpy(str,ses->get_string(addr,strlen).c_str(),strlen); @@ -67,6 +75,11 @@ int tn3270_get_string(h3270::session *ses, int addr, char* str, int strlen) { } int tn3270_get_string_at(h3270::session *ses, int row, int col, char* str, int sz) { + + if(!ses) { + return -1; + } + try { memset(str,0,sz+1); strncpy(str,ses->get_string_at(row,col,sz).c_str(),sz); @@ -79,6 +92,11 @@ int tn3270_get_string_at(h3270::session *ses, int row, int col, char* str, int s } int tn3270_set_string_at(h3270::session *ses, int row, int col, const char* str) { + + if(!ses) { + return -1; + } + try { trace_to_file("%s(%d,%d):\n%s\n",__FUNCTION__,row,col,str); debug("%s(%d,%d,\"%s\")",__FUNCTION__,row,col,str); @@ -91,20 +109,32 @@ int tn3270_set_string_at(h3270::session *ses, int row, int col, const char* str) } int tn3270_wait_for_string_at(h3270::session *ses, int row, int col, const char *key, int timeout) { - try { - return ses->wait_for_string_at(row,col,key,timeout); - } catch(std::exception &e) { - tn3270_lasterror = e.what(); + + if(ses) { + + try { + return ses->wait_for_string_at(row,col,key,timeout); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + } + return -1; } int tn3270_cmp_string_at(h3270::session *ses, int row, int col, const char* str) { - try { - return ses->cmp_string_at(row,col,str); - } catch(std::exception &e) { - tn3270_lasterror = e.what(); + + if(ses) { + + try { + return ses->cmp_string_at(row,col,str); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + } + return -1; } diff --git a/src/native/set.cc b/src/native/set.cc index 3db2c77..1d60e93 100644 --- a/src/native/set.cc +++ b/src/native/set.cc @@ -62,6 +62,11 @@ int tn3270_set_cursor_addr(h3270::session *ses, int addr) { } int tn3270_set_charset(h3270::session *ses, const char* str) { + + if(!ses) { + return EINVAL; + } + try { trace_to_file("%s: \"%s\" -> \"%s\"",__FUNCTION__,ses->get_display_charset().c_str(),str); ses->set_display_charset(NULL, str); -- libgit2 0.21.2