Ian Boyd
Ian Boyd

Reputation: 256881

Delphi compiler warning for passing signed to unsigned

I recently tried using Range Checking in my application, in an attempt to catch array bounds violations, etc.

Unfortuantly this has caused quite a few non-errors relating to HRESULT:

var
    hr: HRESULT;
begin
    hr := E_UNEXPECTED;
    ShowMessage(SysErrorMessage(hr));

The call to SysErrorMessage fails with a ERangeError. That is because:

HRESULT = type Longint;  { from wtypes.h }

function SysErrorMessage(ErrorCode: Cardinal): string;

Manually typecast

The solution is simple: manually force a conversion to Cardinal every time i pass an HRESULT to SysErrorMessage.

This works well enough for code in the current project i'm working on - do a search for SysErrorMessage and scan the results for where I might be passing an HRESULT:

enter image description here

But it fails when there is code in shared libraries. And it also fails when there might be other constructs where i'm passing a signed to unsigned and vice versa.

Can the compiler just tell me?

Does the compiler have a warning option to catch when i mix signed and unsigned types? Obviously i'm going to disable the use of range checking.

See also

Bonus

All the WinSock functions return Integer:

int WSAAPI getaddrinfo(
  _In_opt_       PCSTR      pNodeName,
  _In_opt_       PCSTR      pServiceName,
  _In_opt_ const ADDRINFOA  *pHints,
  _Out_          PADDRINFOA *ppResult
);

and

int WSAGetLastError(void);

Jcl/Jvcl has a lot of fixin to be doin.

Upvotes: 2

Views: 523

Answers (1)

David Heffernan
David Heffernan

Reputation: 613232

Does the compiler have a warning option to catch when i mix signed and unsigned types?

W1024 will collect mixed arithmetic and comparisons. But there's nothing at compile time for assignment or parameter passing, which is essentially the same as assignment.

Upvotes: 1

Related Questions