https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124016
Bug ID: 124016
Summary: Bounded_Indefinite_Holders cause integer overflow
error with class-wide types or large array types
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: ada
Assignee: unassigned at gcc dot gnu.org
Reporter: liam at liampwll dot com
CC: dkm at gcc dot gnu.org
Target Milestone: ---
This one is pretty straightforward, reproducer below:
with Ada.Containers.Bounded_Indefinite_Holders;
procedure Example is
type A is tagged null record;
package A_Class_Holders is new
Ada.Containers.Bounded_Indefinite_Holders (A'Class, 1024);
type B is array (Long_Integer range <>) of Integer;
package B_Class_Holders is new
Ada.Containers.Bounded_Indefinite_Holders (B, 1024);
begin
null;
end Example;
The error for A occurs because Element_Type'Max_Size_In_Storage_Elements
returns Storage_Count'Last for a class-wide type, which is then added to other
numbers in the declaration for Extra_Storage. The fix here is to just add an
extra check for a very large Element_Type'Max_Size_In_Storage_Elements.
function Max_Allocation_Overhead_In_Storage_Elements return Storage_Count
is
- (if Element_Size_In_Storage_Elements >= Long_Integer (Integer'Last) then
+ (if Element_Type'Max_Size_In_Storage_Elements >= Storage_Count
(Integer'Last)
+ or else Element_Size_In_Storage_Elements >= Long_Integer
(Integer'Last)
+ then
The error for B is going to occur any time you try to do a B'Size as GNAT just
returns the maximum array size, which is larger than Long_Integer.
Element_Size_In_Storage_Elements can be turned in to an expression function to
fix this when the above change is applied.
This can all also be addressed with a GNAT attribute to determine storage size
overhead, as the comment in the ads file states.