Commit 78eed2a48453c66c34336fc33a13fb9de91e8241

Authored by Perry Werneck
1 parent 3af05c1f
Exists in master

Fixing segfaults on session creation error.

src/native/get.cc
@@ -37,6 +37,11 @@ @@ -37,6 +37,11 @@
37 * 37 *
38 */ 38 */
39 int tn3270_get_version(h3270::session *ses, char* str, int strlen) { 39 int tn3270_get_version(h3270::session *ses, char* str, int strlen) {
  40 +
  41 + if(!ses) {
  42 + return -1;
  43 + }
  44 +
40 try { 45 try {
41 strncpy(str,ses->get_version().c_str(),strlen); 46 strncpy(str,ses->get_version().c_str(),strlen);
42 } catch(std::exception &e) { 47 } catch(std::exception &e) {
@@ -52,6 +57,10 @@ @@ -52,6 +57,10 @@
52 */ 57 */
53 int tn3270_get_revision(h3270::session *ses, char* str, int strlen) { 58 int tn3270_get_revision(h3270::session *ses, char* str, int strlen) {
54 59
  60 + if(!ses) {
  61 + return -1;
  62 + }
  63 +
55 try { 64 try {
56 strncpy(str,ses->get_revision().c_str(),strlen); 65 strncpy(str,ses->get_revision().c_str(),strlen);
57 } catch(std::exception &e) { 66 } catch(std::exception &e) {
@@ -63,6 +72,10 @@ @@ -63,6 +72,10 @@
63 72
64 int tn3270_get_cstate(h3270::session *ses) { 73 int tn3270_get_cstate(h3270::session *ses) {
65 74
  75 + if(!ses) {
  76 + return -1;
  77 + }
  78 +
66 trace_to_file("%s: %d",__FUNCTION__,(int) ses->get_cstate()); 79 trace_to_file("%s: %d",__FUNCTION__,(int) ses->get_cstate());
67 80
68 try { 81 try {
@@ -76,6 +89,10 @@ @@ -76,6 +89,10 @@
76 89
77 int tn3270_get_program_message(h3270::session *ses) { 90 int tn3270_get_program_message(h3270::session *ses) {
78 91
  92 + if(!ses) {
  93 + return -1;
  94 + }
  95 +
79 try { 96 try {
80 return (int) ses->get_program_message(); 97 return (int) ses->get_program_message();
81 } catch(std::exception &e) { 98 } catch(std::exception &e) {
@@ -86,6 +103,11 @@ @@ -86,6 +103,11 @@
86 } 103 }
87 104
88 int tn3270_get_secure(h3270::session *ses) { 105 int tn3270_get_secure(h3270::session *ses) {
  106 +
  107 + if(!ses) {
  108 + return -1;
  109 + }
  110 +
89 try { 111 try {
90 return (int) ses->get_secure(); 112 return (int) ses->get_secure();
91 } catch(std::exception &e) { 113 } catch(std::exception &e) {
@@ -97,6 +119,10 @@ @@ -97,6 +119,10 @@
97 119
98 int tn3270_get_width(h3270::session *ses) { 120 int tn3270_get_width(h3270::session *ses) {
99 121
  122 + if(!ses) {
  123 + return 0;
  124 + }
  125 +
100 try { 126 try {
101 return (int) ses->get_width(); 127 return (int) ses->get_width();
102 } catch(std::exception &e) { 128 } catch(std::exception &e) {
@@ -108,6 +134,10 @@ @@ -108,6 +134,10 @@
108 134
109 int tn3270_get_height(h3270::session *ses) { 135 int tn3270_get_height(h3270::session *ses) {
110 136
  137 + if(!ses) {
  138 + return 0;
  139 + }
  140 +
111 try { 141 try {
112 return (int) ses->get_height(); 142 return (int) ses->get_height();
113 } catch(std::exception &e) { 143 } catch(std::exception &e) {
@@ -119,6 +149,10 @@ @@ -119,6 +149,10 @@
119 149
120 int tn3270_get_length(h3270::session *ses) { 150 int tn3270_get_length(h3270::session *ses) {
121 151
  152 + if(!ses) {
  153 + return 0;
  154 + }
  155 +
122 try { 156 try {
123 return (int) ses->get_length(); 157 return (int) ses->get_length();
124 } catch(std::exception &e) { 158 } catch(std::exception &e) {
@@ -130,25 +164,33 @@ @@ -130,25 +164,33 @@
130 164
131 int tn3270_get_cursor_addr(h3270::session *ses) { 165 int tn3270_get_cursor_addr(h3270::session *ses) {
132 166
133 - try {  
134 - return (int) ses->get_cursor_addr();  
135 - } catch(std::exception &e) {  
136 - tn3270_lasterror = e.what();  
137 - } 167 + if(ses) {
  168 +
  169 + try {
  170 + return (int) ses->get_cursor_addr();
  171 + } catch(std::exception &e) {
  172 + tn3270_lasterror = e.what();
  173 + }
  174 +
  175 + }
138 return -1; 176 return -1;
139 177
140 } 178 }
141 179
142 int tn3270_get_url(h3270::session *ses, char* str, int strlen) { 180 int tn3270_get_url(h3270::session *ses, char* str, int strlen) {
143 181
144 - try { 182 + if(ses) {
145 183
146 - strncpy(str,ses->get_url().c_str(),strlen);  
147 - return 0; 184 + try {
148 185
149 - } catch(std::exception &e) {  
150 - tn3270_lasterror = e.what();  
151 - } 186 + strncpy(str,ses->get_url().c_str(),strlen);
  187 + return 0;
  188 +
  189 + } catch(std::exception &e) {
  190 + tn3270_lasterror = e.what();
  191 + }
  192 +
  193 + }
152 return -1; 194 return -1;
153 195
154 } 196 }
src/native/init.cc
@@ -49,6 +49,7 @@ @@ -49,6 +49,7 @@
49 return h3270::session::create(name); 49 return h3270::session::create(name);
50 } catch(std::exception &e) { 50 } catch(std::exception &e) {
51 tn3270_lasterror = e.what(); 51 tn3270_lasterror = e.what();
  52 + trace_to_file("%s(%s)",__FUNCTION__,e.what());
52 } 53 }
53 return nullptr; 54 return nullptr;
54 } 55 }
src/native/network.cc
@@ -32,57 +32,91 @@ @@ -32,57 +32,91 @@
32 /*---[ Implement ]----------------------------------------------------------------------------------*/ 32 /*---[ Implement ]----------------------------------------------------------------------------------*/
33 33
34 int tn3270_connect(h3270::session *ses, const char *host, time_t wait) { 34 int tn3270_connect(h3270::session *ses, const char *host, time_t wait) {
35 - try {  
36 - debug("%s(%s,%d)",__FUNCTION__,host,(int) wait);  
37 - return ses->connect(host,wait);  
38 - } catch(std::exception &e) {  
39 - tn3270_lasterror = e.what(); 35 +
  36 + if(ses) {
  37 +
  38 + try {
  39 + debug("%s(%s,%d)",__FUNCTION__,host,(int) wait);
  40 + return ses->connect(host,wait);
  41 + } catch(std::exception &e) {
  42 + tn3270_lasterror = e.what();
  43 + }
  44 +
40 } 45 }
  46 +
41 return -1; 47 return -1;
42 } 48 }
43 49
44 int tn3270_disconnect(h3270::session *ses) { 50 int tn3270_disconnect(h3270::session *ses) {
45 - try {  
46 - return ses->disconnect();  
47 - } catch(std::exception &e) {  
48 - tn3270_lasterror = e.what(); 51 +
  52 + if(ses) {
  53 +
  54 + try {
  55 + return ses->disconnect();
  56 + } catch(std::exception &e) {
  57 + tn3270_lasterror = e.what();
  58 + }
  59 +
49 } 60 }
  61 +
50 return -1; 62 return -1;
51 } 63 }
52 64
53 int tn3270_is_connected(h3270::session *ses) { 65 int tn3270_is_connected(h3270::session *ses) {
54 - try {  
55 - return (int) ses->is_connected();  
56 - } catch(std::exception &e) {  
57 - tn3270_lasterror = e.what(); 66 +
  67 + if(ses) {
  68 +
  69 + try {
  70 + return (int) ses->is_connected();
  71 + } catch(std::exception &e) {
  72 + tn3270_lasterror = e.what();
  73 + }
  74 +
58 } 75 }
  76 +
59 return -1; 77 return -1;
60 } 78 }
61 79
62 int tn3270_is_ready(h3270::session *ses) { 80 int tn3270_is_ready(h3270::session *ses) {
63 - try {  
64 - return (int) ses->is_ready();  
65 - } catch(std::exception &e) {  
66 - tn3270_lasterror = e.what(); 81 +
  82 + if(ses) {
  83 +
  84 + try {
  85 + return (int) ses->is_ready();
  86 + } catch(std::exception &e) {
  87 + tn3270_lasterror = e.what();
  88 + }
  89 +
67 } 90 }
  91 +
68 return -1; 92 return -1;
69 } 93 }
70 94
71 int tn3270_wait_for_ready(h3270::session *ses, int seconds) { 95 int tn3270_wait_for_ready(h3270::session *ses, int seconds) {
72 - try {  
73 - return (int) ses->wait_for_ready(seconds);  
74 - } catch(std::exception &e) {  
75 - tn3270_lasterror = e.what(); 96 +
  97 + if(ses) {
  98 +
  99 + try {
  100 + return (int) ses->wait_for_ready(seconds);
  101 + } catch(std::exception &e) {
  102 + tn3270_lasterror = e.what();
  103 + }
  104 +
76 } 105 }
77 return -1; 106 return -1;
78 107
79 } 108 }
80 109
81 int tn3270_wait(h3270::session *ses, int seconds) { 110 int tn3270_wait(h3270::session *ses, int seconds) {
82 - try {  
83 - return (int) ses->wait(seconds);  
84 - } catch(std::exception &e) {  
85 - tn3270_lasterror = e.what(); 111 +
  112 + if(ses) {
  113 +
  114 + try {
  115 + return (int) ses->wait(seconds);
  116 + } catch(std::exception &e) {
  117 + tn3270_lasterror = e.what();
  118 + }
  119 +
86 } 120 }
87 return -1; 121 return -1;
88 } 122 }
src/native/screen.cc
@@ -33,6 +33,10 @@ @@ -33,6 +33,10 @@
33 33
34 int tn3270_get_contents(h3270::session *ses, char* str, int sz) { 34 int tn3270_get_contents(h3270::session *ses, char* str, int sz) {
35 35
  36 + if(!ses) {
  37 + return -1;
  38 + }
  39 +
36 try { 40 try {
37 41
38 std::string contents = ses->get_contents(); 42 std::string contents = ses->get_contents();
@@ -55,6 +59,10 @@ int tn3270_get_contents(h3270::session *ses, char* str, int sz) { @@ -55,6 +59,10 @@ int tn3270_get_contents(h3270::session *ses, char* str, int sz) {
55 59
56 int tn3270_get_string(h3270::session *ses, int addr, char* str, int strlen) { 60 int tn3270_get_string(h3270::session *ses, int addr, char* str, int strlen) {
57 61
  62 + if(!ses) {
  63 + return -1;
  64 + }
  65 +
58 try { 66 try {
59 memset(str,0,strlen); 67 memset(str,0,strlen);
60 strncpy(str,ses->get_string(addr,strlen).c_str(),strlen); 68 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) { @@ -67,6 +75,11 @@ int tn3270_get_string(h3270::session *ses, int addr, char* str, int strlen) {
67 } 75 }
68 76
69 int tn3270_get_string_at(h3270::session *ses, int row, int col, char* str, int sz) { 77 int tn3270_get_string_at(h3270::session *ses, int row, int col, char* str, int sz) {
  78 +
  79 + if(!ses) {
  80 + return -1;
  81 + }
  82 +
70 try { 83 try {
71 memset(str,0,sz+1); 84 memset(str,0,sz+1);
72 strncpy(str,ses->get_string_at(row,col,sz).c_str(),sz); 85 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 @@ -79,6 +92,11 @@ int tn3270_get_string_at(h3270::session *ses, int row, int col, char* str, int s
79 } 92 }
80 93
81 int tn3270_set_string_at(h3270::session *ses, int row, int col, const char* str) { 94 int tn3270_set_string_at(h3270::session *ses, int row, int col, const char* str) {
  95 +
  96 + if(!ses) {
  97 + return -1;
  98 + }
  99 +
82 try { 100 try {
83 trace_to_file("%s(%d,%d):\n%s\n",__FUNCTION__,row,col,str); 101 trace_to_file("%s(%d,%d):\n%s\n",__FUNCTION__,row,col,str);
84 debug("%s(%d,%d,\"%s\")",__FUNCTION__,row,col,str); 102 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) @@ -91,20 +109,32 @@ int tn3270_set_string_at(h3270::session *ses, int row, int col, const char* str)
91 } 109 }
92 110
93 int tn3270_wait_for_string_at(h3270::session *ses, int row, int col, const char *key, int timeout) { 111 int tn3270_wait_for_string_at(h3270::session *ses, int row, int col, const char *key, int timeout) {
94 - try {  
95 - return ses->wait_for_string_at(row,col,key,timeout);  
96 - } catch(std::exception &e) {  
97 - tn3270_lasterror = e.what(); 112 +
  113 + if(ses) {
  114 +
  115 + try {
  116 + return ses->wait_for_string_at(row,col,key,timeout);
  117 + } catch(std::exception &e) {
  118 + tn3270_lasterror = e.what();
  119 + }
  120 +
98 } 121 }
  122 +
99 return -1; 123 return -1;
100 124
101 } 125 }
102 126
103 int tn3270_cmp_string_at(h3270::session *ses, int row, int col, const char* str) { 127 int tn3270_cmp_string_at(h3270::session *ses, int row, int col, const char* str) {
104 - try {  
105 - return ses->cmp_string_at(row,col,str);  
106 - } catch(std::exception &e) {  
107 - tn3270_lasterror = e.what(); 128 +
  129 + if(ses) {
  130 +
  131 + try {
  132 + return ses->cmp_string_at(row,col,str);
  133 + } catch(std::exception &e) {
  134 + tn3270_lasterror = e.what();
  135 + }
  136 +
108 } 137 }
  138 +
109 return -1; 139 return -1;
110 } 140 }
src/native/set.cc
@@ -62,6 +62,11 @@ int tn3270_set_cursor_addr(h3270::session *ses, int addr) { @@ -62,6 +62,11 @@ int tn3270_set_cursor_addr(h3270::session *ses, int addr) {
62 } 62 }
63 63
64 int tn3270_set_charset(h3270::session *ses, const char* str) { 64 int tn3270_set_charset(h3270::session *ses, const char* str) {
  65 +
  66 + if(!ses) {
  67 + return EINVAL;
  68 + }
  69 +
65 try { 70 try {
66 trace_to_file("%s: \"%s\" -> \"%s\"",__FUNCTION__,ses->get_display_charset().c_str(),str); 71 trace_to_file("%s: \"%s\" -> \"%s\"",__FUNCTION__,ses->get_display_charset().c_str(),str);
67 ses->set_display_charset(NULL, str); 72 ses->set_display_charset(NULL, str);