Commit 71b94e2c01e6ed387eb7e828d81c200204451010

Authored by Perry Werneck
1 parent b4c8f3ff

Fixing winhttp memory management

Showing 2 changed files with 35 additions and 21 deletions   Show diff stats
src/core/util.c
@@ -507,9 +507,11 @@ LIB3270_EXPORT void * lib3270_free(void *p) @@ -507,9 +507,11 @@ LIB3270_EXPORT void * lib3270_free(void *p)
507 507
508 LIB3270_EXPORT void lib3270_autoptr_cleanup_char(char **ptr) 508 LIB3270_EXPORT void lib3270_autoptr_cleanup_char(char **ptr)
509 { 509 {
510 - if(*ptr)  
511 - free(*ptr);  
512 - *ptr = NULL; 510 + if(ptr && *ptr)
  511 + {
  512 + free((void *) *ptr);
  513 + *ptr = NULL;
  514 + }
513 } 515 }
514 516
515 LIB3270_EXPORT void * lib3270_realloc(void *p, int len) 517 LIB3270_EXPORT void * lib3270_realloc(void *p, int len)
src/ssl/windows/http.c
@@ -55,18 +55,29 @@ static void lib3270_autoptr_cleanup_HINTERNET(HINTERNET **hInternet) @@ -55,18 +55,29 @@ static void lib3270_autoptr_cleanup_HINTERNET(HINTERNET **hInternet)
55 55
56 X509_CRL * get_crl_using_http(H3270 *hSession, SSL_ERROR_MESSAGE * message, const char *consturl) 56 X509_CRL * get_crl_using_http(H3270 *hSession, SSL_ERROR_MESSAGE * message, const char *consturl)
57 { 57 {
58 - // Strip URL.  
59 - lib3270_autoptr(char) urldup = lib3270_unescape(consturl); 58 + wchar_t wHostname[4096];
  59 + wchar_t wPath[4096];
  60 +
  61 + {
  62 + // Strip URL
  63 + char * url = lib3270_unescape(consturl);
  64 +
  65 + char *hostname = strstr(url,"://");
  66 + if(!hostname)
  67 + hostname = url;
  68 + else
  69 + hostname += 3;
  70 +
  71 + char *path = strchr(hostname,'/');
  72 + if(path)
  73 + *(path++) = 0;
60 74
61 - char *hostname = strstr(urldup,"://");  
62 - if(!hostname)  
63 - hostname = urldup;  
64 - else  
65 - hostname += 3; 75 + mbstowcs(wHostname, hostname, strlen(hostname)+1);
  76 + mbstowcs(wPath, path, strlen(path)+1);
66 77
67 - char *path = strchr(hostname,'/');  
68 - if(path)  
69 - *(path++) = 0; 78 + lib3270_free(url);
  79 +
  80 + }
70 81
71 // https://docs.microsoft.com/en-us/windows/desktop/api/winhttp/nf-winhttp-winhttpopenrequest 82 // https://docs.microsoft.com/en-us/windows/desktop/api/winhttp/nf-winhttp-winhttpopenrequest
72 83
@@ -90,9 +101,6 @@ X509_CRL * get_crl_using_http(H3270 *hSession, SSL_ERROR_MESSAGE * message, cons @@ -90,9 +101,6 @@ X509_CRL * get_crl_using_http(H3270 *hSession, SSL_ERROR_MESSAGE * message, cons
90 } 101 }
91 102
92 // Connect to server 103 // Connect to server
93 - debug("Hostname: \"%s\"",hostname);  
94 - wchar_t wHostname[4096];  
95 - mbstowcs(wHostname, hostname, strlen(hostname)+1);  
96 lib3270_autoptr(HINTERNET) hConnect = WinHttpConnect(httpSession, wHostname, INTERNET_DEFAULT_HTTP_PORT, 0); 104 lib3270_autoptr(HINTERNET) hConnect = WinHttpConnect(httpSession, wHostname, INTERNET_DEFAULT_HTTP_PORT, 0);
97 if(!hConnect) 105 if(!hConnect)
98 { 106 {
@@ -108,9 +116,6 @@ X509_CRL * get_crl_using_http(H3270 *hSession, SSL_ERROR_MESSAGE * message, cons @@ -108,9 +116,6 @@ X509_CRL * get_crl_using_http(H3270 *hSession, SSL_ERROR_MESSAGE * message, cons
108 } 116 }
109 117
110 // Create request. 118 // Create request.
111 - debug("Path: \"%s\"",path);  
112 - wchar_t wPath[4096];  
113 - mbstowcs(wPath, path, strlen(path)+1);  
114 lib3270_autoptr(HINTERNET) hRequest = WinHttpOpenRequest(hConnect, L"GET", wPath, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_ESCAPE_PERCENT); 119 lib3270_autoptr(HINTERNET) hRequest = WinHttpOpenRequest(hConnect, L"GET", wPath, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_ESCAPE_PERCENT);
115 if(!hConnect) 120 if(!hConnect)
116 { 121 {
@@ -160,7 +165,8 @@ X509_CRL * get_crl_using_http(H3270 *hSession, SSL_ERROR_MESSAGE * message, cons @@ -160,7 +165,8 @@ X509_CRL * get_crl_using_http(H3270 *hSession, SSL_ERROR_MESSAGE * message, cons
160 lib3270_autoptr(char) httpText = lib3270_malloc(szResponse+1); 165 lib3270_autoptr(char) httpText = lib3270_malloc(szResponse+1);
161 memset(httpText,0,szResponse+1); 166 memset(httpText,0,szResponse+1);
162 167
163 - debug("Response length: %u", (unsigned int) szResponse); 168 + debug("Data block: %p",httpText);
  169 + debug("Response before: %u", (unsigned int) szResponse);
164 170
165 if(!WinHttpReadData(hRequest,httpText,szResponse,&szResponse)){ 171 if(!WinHttpReadData(hRequest,httpText,szResponse,&szResponse)){
166 message->error = hSession->ssl.error = 0; 172 message->error = hSession->ssl.error = 0;
@@ -171,12 +177,17 @@ X509_CRL * get_crl_using_http(H3270 *hSession, SSL_ERROR_MESSAGE * message, cons @@ -171,12 +177,17 @@ X509_CRL * get_crl_using_http(H3270 *hSession, SSL_ERROR_MESSAGE * message, cons
171 return NULL; 177 return NULL;
172 } 178 }
173 179
  180 + debug("Response after: %u", (unsigned int) szResponse);
  181 +
174 // 182 //
175 // Parse CRL 183 // Parse CRL
176 // 184 //
177 X509_CRL * x509_crl = NULL; 185 X509_CRL * x509_crl = NULL;
178 186
179 - if(!d2i_X509_CRL(&x509_crl, (const unsigned char **) &httpText, szResponse)) 187 + // Copy the pointer because d2i_X509_CRL changes the value!!!
  188 + const unsigned char *crl_data = (const unsigned char *) httpText;
  189 +
  190 + if(!d2i_X509_CRL(&x509_crl,&crl_data, szResponse))
180 { 191 {
181 message->error = hSession->ssl.error = ERR_get_error(); 192 message->error = hSession->ssl.error = ERR_get_error();
182 message->title = _( "Security error" ); 193 message->title = _( "Security error" );
@@ -185,6 +196,7 @@ X509_CRL * get_crl_using_http(H3270 *hSession, SSL_ERROR_MESSAGE * message, cons @@ -185,6 +196,7 @@ X509_CRL * get_crl_using_http(H3270 *hSession, SSL_ERROR_MESSAGE * message, cons
185 return NULL; 196 return NULL;
186 } 197 }
187 198
  199 + debug("**************URL:[%s]*********************",consturl);
188 return x509_crl; 200 return x509_crl;
189 201
190 } 202 }