sonar-delphi icon indicating copy to clipboard operation
sonar-delphi copied to clipboard

Detect that a function pointer parameter has not the right size

Open zedalaye opened this issue 1 year ago • 5 comments

Prerequisites

  • [X] This rule has not already been suggested.
  • [X] This should be a new rule, not an improvement to an existing rule.
  • [X] This rule would be generally useful, not specific to my code or setup.

Suggested rule title

Pointers parameters should have the right size

Rule description

Check that pointer parameters have the right size according to the compiler architecture.

Rationale

In a migration process from Win32 to Win64, I encountered this error :

  n := tmpp.Count;
  p := nil;
  DynArraySetLength(p, field_type, 1, @n);

The code was build for Win32, so n was an Integer Now DynArraySetLength takes a PNativeInt which is 64bits wide under Win64 and the Delphi compiler does not emit a single warning about this critical issue.

Would be awesome if Sonarqube could help finding those problems.

zedalaye avatar Oct 17 '24 15:10 zedalaye

You may be interested in the Type-checked pointers compiler directive.

zaneduffield avatar Oct 17 '24 21:10 zaneduffield

I confirm that {$T+} find the problem. But as it's "project wide", it has consequences everywhere including in first party code.

zedalaye avatar Oct 18 '24 11:10 zedalaye

Yes, it has global scope. You'd have to deal with the stricter type checking in all compiled code.

Assuming you're using the precompiled RTL and precompiled 3rd party code, then it should only be an issue for the first party code — where you are able to fix the compiler errors.

zaneduffield avatar Oct 18 '24 20:10 zaneduffield

This could be a nice "progressive enhancement" sort of rule where you slowly migrate a codebase to using the {T+} directive, or a compromise where you want to enforce this in all of your application code but can't enable the directive due to noncompliant 3rdParty code being built into your project.

cirras avatar Nov 12 '24 03:11 cirras

I confirm that {$T+} find the problem. But as it's "project wide", it has consequences everywhere including in first party code.

The documentation says "global" but that is not true - you can turn this on or off at any place throughout a unit. I know this because some while ago I explicitly added $T- to Spring.inc to not cause compile errors for someone using $T+ in their project settings.

sglienke avatar Jul 16 '25 10:07 sglienke