Detect that a function pointer parameter has not the right size
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.
You may be interested in the Type-checked pointers compiler directive.
I confirm that {$T+} find the problem. But as it's "project wide", it has consequences everywhere including in first party code.
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.
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.
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.