Custom proxy missing null termination

delphi package - automated exception handling
Post Reply
AndersB
Posts: 3
Joined: Thu Feb 04, 2021 7:01 am

Custom proxy missing null termination

Post by AndersB »

I tried to set a custom proxy using the hidden madExcept.ProxyServer property. In AnsiToGlobalUnicode inside TWinHttp.Create the proxy AnsiString is converted into a unicode string, but the null termination is missing. Suggest to include the null termination in the copy:

function AnsiToGlobalUnicode(const ansi: AnsiString) : pointer;
var us1 : UnicodeString;
begin
if ansi <> '' then begin
us1 := UnicodeString(ansi);
result := pointer(GlobalAlloc(LPTR, Length(us1) * 2 + 2));
Move(us1[1], result^, Length(us1) * 2 + 2);
end else
result := nil;
end;

I'm also having trouble using the automatic proxy. In madExcept.TWinHttp.SendRequest an attempt is first made to send the request without proxy. If the request fails and the error is not ERROR_WINHTTP_SECURE_FAILURE the proxy is setup and another attempt is made. The issue for me is that I get the error ERROR_WINHTTP_SECURE_FAILURE when a proxy is required. Could be the way our company network is setup with different security softwares and proxies... I can bypass this issue if I could set the proxy myself, but that fails too as described above.
Last edited by AndersB on Thu Feb 04, 2021 7:25 am, edited 1 time in total.
iconic
Site Admin
Posts: 1065
Joined: Wed Jun 08, 2005 5:08 am

Re: Custom proxy missing null termination

Post by iconic »

Hi AndersB,

Your code update would be better with the below changes, but thanks for pointing this out to us.

Code: Select all

function AnsiToGlobalUnicode(const ansi: AnsiString) : pointer;
var us1 : UnicodeString;
begin
if ansi <> '' then begin
us1 := UnicodeString(ansi);
result := pointer(GlobalAlloc(GPTR, Length(us1) * 2 + 2)); // + 2 for zero termination as you've done but use GPTR instead of LPTR (same value but wrong flag name)
if result <> nil then // check allocation result 
Move(us1[1], result^, Length(us1) * 2); // memory is already zero at the end during alloc above so just copy the chars only
end else
result := nil;
end;
I'll try to reproduce your proxy issue this week but it may take a couple of days. WinHTTP is full of unpleasant surprises, unfortunately.

--Iconic
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: Custom proxy missing null termination

Post by madshi »

Thanks, I've fixed the missing null termination in my source code, as suggested by you guys.
Post Reply