Page 1 of 1

Custom proxy missing null termination

Posted: Thu Feb 04, 2021 7:12 am
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.

Re: Custom proxy missing null termination

Posted: Fri Feb 05, 2021 8:20 am
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

Re: Custom proxy missing null termination

Posted: Mon Feb 08, 2021 6:48 pm
by madshi
Thanks, I've fixed the missing null termination in my source code, as suggested by you guys.