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

New rule: Heap exceptions should not be created

Open fourls opened this issue 2 years ago • 1 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

Heap exceptions should not be created

Rule description

This rule would detect constructor calls of all types inheriting from EHeapException (EOutOfMemory and EInvalidPointer).

Rationale

Exceptions descending from EHeapException have their memory pre-allocated at the start of the application. Creating them at runtime is at best misleading and at worst has unpredictable consequences.

See the Embarcadero documentation:

Note: Memory for these exceptions is pre-allocated whenever an application starts and remains allocated as long as the application is running. Never raise EHeapException or its descendants directly.

fourls avatar Oct 31 '23 00:10 fourls

This one is a no-brainer, let's do it.

cirras avatar Nov 14 '23 03:11 cirras