Only Reclaim Allocated Storage (RCL02)

Level \(\rightarrow\) Mandatory

Category
Safety:

\(\checkmark\)

Cyber:

\(\checkmark\)

Goal
Maintainability:

\(\checkmark\)

Reliability:

\(\checkmark\)

Portability:

\(\checkmark\)

Performance:

Security:

\(\checkmark\)

Remediation \(\rightarrow\) High

Verification Method \(\rightarrow\) Code inspection

Reference

[SEI-C] MEM34-C: Only Free Memory Allocated Dynamically

Description

Only deallocate storage that was dynamically allocated by the evaluation of an allocator (i.e., new).

This is possible because Ada allows creation of access values designating declared (aliased) objects.

Applicable Vulnerability within ISO TR 24772-2

  • 6.39 Memory leak and heap fragmentation [XYL]

Noncompliant Code Example

   type String_Reference is access all String;
   procedure Free is new Ada.Unchecked_Deallocation
     (Object => String, Name => String_Reference);
   S : aliased String := "Hello";
   Y : String_Reference := S'Access;
begin
   Free (Y);

Compliant Code Example

Remove the call to Free (Y).

Notes

Enforcement of this rule can only be provided by manual code review, unless deallocation is forbidden via No_Unchecked_Deallocation.