Shouldn't this line be null terminated?

Shouldn't this line be null terminated?
HostEnt := gethostbyname(MarshaledAString(TEncoding.UTF8.GetBytes(Name)));

class function TIPAddress.LookupName(const Name: string): TIPAddress;
var
HostEnt: PHostEnt;
begin
HostEnt := gethostbyname(MarshaledAString(TEncoding.UTF8.GetBytes(Name)));
FillChar(Result, SizeOf(Result), 0);
if HostEnt <> nil then
Result.FAddr.S_addr := PCardinal(HostEnt.h_addr_list^)^;
end;

For example...
HostEnt := gethostbyname(MarshaledAString(TEncoding.UTF8.GetBytes(Name + #0)));
or
HostEnt := gethostbyname(MarshaledAString(Utf8String(Name))); because the Utf8String is null terminated internally.

It appears it currently only works if you are lucky enough to have a zero in memory at the end, especially on the new Linux compiler. The System.Net.Socket units have a lot of great functionality that I wish I could use.

Comments

  1. I tried to find that one, thanks. I just upvoted it. It is a shame because this one issue makes almost the entire socket connect() related and supporting classes completely unreliable in Delphi that use these routines.

    ReplyDelete
  2. You may want to upvote this one as well, since gethostbyname is deprecated and should not be used at all quality.embarcadero.com - Log in - Embarcadero Technologies

    ReplyDelete

Post a Comment