Commit dc6b9ba2 by Matthew Heaney Committed by Arnaud Charlet

a-convec.ads, [...]: (operator "&"): handle potential overflow for large index types

2006-02-17  Matthew Heaney  <heaney@adacore.com>

	* a-convec.ads, a-convec.adb: 
	(operator "&"): handle potential overflow for large index types
	(Insert): removed Contraint_Error when using large index types
	(Insert_Space): removed Constraint_Error for large index types
	(Length): moved constraint check from Length to Insert

	* a-coinve.ads, a-coinve.adb: Stream attribute procedures are declared
	as not null access.
	Explicit raise statements now include an exception message.
	(operator "&"): handle potential overflow for large index types
	(Insert): removed Contraint_Error when using large index types
	(Insert_Space): removed Constraint_Error for large index types
	(Length): moved constraint check from Length to Insert

From-SVN: r111197
parent 35ecbe09
...@@ -40,6 +40,7 @@ with System; use type System.Address; ...@@ -40,6 +40,7 @@ with System; use type System.Address;
package body Ada.Containers.Indefinite_Vectors is package body Ada.Containers.Indefinite_Vectors is
type Int is range System.Min_Int .. System.Max_Int; type Int is range System.Min_Int .. System.Max_Int;
type UInt is mod System.Max_Binary_Modulus;
procedure Free is procedure Free is
new Ada.Unchecked_Deallocation (Elements_Type, Elements_Access); new Ada.Unchecked_Deallocation (Elements_Type, Elements_Access);
...@@ -120,12 +121,18 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -120,12 +121,18 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
declare declare
Last_As_Int : constant Int'Base := -- TODO: handle overflow N : constant Int'Base := Int (LN) + Int (RN);
Int (Index_Type'First) + Int (LN) + Int (RN) - 1; Last_As_Int : Int'Base;
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Int (No_Index) > Int'Last - N then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if;
Last_As_Int := Int (No_Index) + N;
if Last_As_Int > Int (Index_Type'Last) then
raise Constraint_Error with "new length is out of range";
end if; end if;
declare declare
...@@ -209,12 +216,17 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -209,12 +216,17 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
declare declare
Last_As_Int : constant Int'Base := Last_As_Int : Int'Base;
Int (Index_Type'First) + Int (LN);
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Int (Index_Type'First) > Int'Last - Int (LN) then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if;
Last_As_Int := Int (Index_Type'First) + Int (LN);
if Last_As_Int > Int (Index_Type'Last) then
raise Constraint_Error with "new length is out of range";
end if; end if;
declare declare
...@@ -285,12 +297,17 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -285,12 +297,17 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
declare declare
Last_As_Int : constant Int'Base := Last_As_Int : Int'Base;
Int (Index_Type'First) + Int (RN);
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Int (Index_Type'First) > Int'Last - Int (RN) then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if;
Last_As_Int := Int (Index_Type'First) + Int (RN);
if Last_As_Int > Int (Index_Type'Last) then
raise Constraint_Error with "new length is out of range";
end if; end if;
declare declare
...@@ -339,7 +356,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -339,7 +356,7 @@ package body Ada.Containers.Indefinite_Vectors is
function "&" (Left, Right : Element_Type) return Vector is function "&" (Left, Right : Element_Type) return Vector is
begin begin
if Index_Type'First >= Index_Type'Last then if Index_Type'First >= Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if; end if;
declare declare
...@@ -348,6 +365,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -348,6 +365,7 @@ package body Ada.Containers.Indefinite_Vectors is
subtype ET is Elements_Type (Index_Type'First .. Last); subtype ET is Elements_Type (Index_Type'First .. Last);
Elements : Elements_Access := new ET; Elements : Elements_Access := new ET;
begin begin
begin begin
Elements (Elements'First) := new Element_Type'(Left); Elements (Elements'First) := new Element_Type'(Left);
...@@ -445,7 +463,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -445,7 +463,7 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with "vector is already at its maximum length";
end if; end if;
Insert Insert
...@@ -465,7 +483,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -465,7 +483,7 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with "vector is already at its maximum length";
end if; end if;
Insert Insert
...@@ -495,7 +513,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -495,7 +513,8 @@ package body Ada.Containers.Indefinite_Vectors is
procedure Clear (Container : in out Vector) is procedure Clear (Container : in out Vector) is
begin begin
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
while Container.Last >= Index_Type'First loop while Container.Last >= Index_Type'First loop
...@@ -532,12 +551,12 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -532,12 +551,12 @@ package body Ada.Containers.Indefinite_Vectors is
is is
begin begin
if Index < Index_Type'First then if Index < Index_Type'First then
raise Constraint_Error; raise Constraint_Error with "Index is out of range (too small)";
end if; end if;
if Index > Container.Last then if Index > Container.Last then
if Index > Container.Last + 1 then if Index > Container.Last + 1 then
raise Constraint_Error; raise Constraint_Error with "Index is out of range (too large)";
end if; end if;
return; return;
...@@ -548,14 +567,14 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -548,14 +567,14 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
declare declare
Index_As_Int : constant Int := Int (Index); Index_As_Int : constant Int := Int (Index);
Old_Last_As_Int : constant Int := Int (Container.Last); Old_Last_As_Int : constant Int := Int (Container.Last);
-- TODO: somewhat vestigial...fix ???
Count1 : constant Int'Base := Int (Count); Count1 : constant Int'Base := Int (Count);
Count2 : constant Int'Base := Old_Last_As_Int - Index_As_Int + 1; Count2 : constant Int'Base := Old_Last_As_Int - Index_As_Int + 1;
N : constant Int'Base := Int'Min (Count1, Count2); N : constant Int'Base := Int'Min (Count1, Count2);
...@@ -609,13 +628,15 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -609,13 +628,15 @@ package body Ada.Containers.Indefinite_Vectors is
is is
begin begin
if Position.Container = null then if Position.Container = null then
raise Constraint_Error; raise Constraint_Error with "Position cursor has no element";
end if; end if;
if Position.Container /= Container'Unchecked_Access if Position.Container /= Container'Unrestricted_Access then
or else Position.Index > Container.Last raise Program_Error with "Position cursor denotes wrong container";
then end if;
raise Program_Error;
if Position.Index > Container.Last then
raise Program_Error with "Position index is out of range";
end if; end if;
Delete (Container, Position.Index, Count); Delete (Container, Position.Index, Count);
...@@ -662,7 +683,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -662,7 +683,8 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
declare declare
...@@ -693,7 +715,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -693,7 +715,7 @@ package body Ada.Containers.Indefinite_Vectors is
is is
begin begin
if Index > Container.Last then if Index > Container.Last then
raise Constraint_Error; raise Constraint_Error with "Index is out of range";
end if; end if;
declare declare
...@@ -701,7 +723,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -701,7 +723,7 @@ package body Ada.Containers.Indefinite_Vectors is
begin begin
if EA = null then if EA = null then
raise Constraint_Error; raise Constraint_Error with "element is empty";
end if; end if;
return EA.all; return EA.all;
...@@ -711,7 +733,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -711,7 +733,7 @@ package body Ada.Containers.Indefinite_Vectors is
function Element (Position : Cursor) return Element_Type is function Element (Position : Cursor) return Element_Type is
begin begin
if Position.Container = null then if Position.Container = null then
raise Constraint_Error; raise Constraint_Error with "Position cursor has no element";
end if; end if;
return Element (Position.Container.all, Position.Index); return Element (Position.Container.all, Position.Index);
...@@ -723,7 +745,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -723,7 +745,7 @@ package body Ada.Containers.Indefinite_Vectors is
procedure Finalize (Container : in out Vector) is procedure Finalize (Container : in out Vector) is
begin begin
Clear (Container); Clear (Container); -- Checks busy-bit
declare declare
X : Elements_Access := Container.Elements; X : Elements_Access := Container.Elements;
...@@ -743,11 +765,14 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -743,11 +765,14 @@ package body Ada.Containers.Indefinite_Vectors is
Position : Cursor := No_Element) return Cursor Position : Cursor := No_Element) return Cursor
is is
begin begin
if Position.Container /= null if Position.Container /= null then
and then (Position.Container /= Container'Unchecked_Access if Position.Container /= Container'Unrestricted_Access then
or else Position.Index > Container.Last) raise Program_Error with "Position cursor denotes wrong container";
then end if;
raise Program_Error;
if Position.Index > Container.Last then
raise Program_Error with "Position index is out of range";
end if;
end if; end if;
for J in Position.Index .. Container.Last loop for J in Position.Index .. Container.Last loop
...@@ -888,7 +913,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -888,7 +913,8 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
if Source.Busy > 0 then if Source.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
Target.Set_Length (Length (Target) + Length (Source)); Target.Set_Length (Length (Target) + Length (Source));
...@@ -963,7 +989,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -963,7 +989,8 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
if Container.Lock > 0 then if Container.Lock > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with cursors (vector is locked)";
end if; end if;
Sort (Container.Elements (Index_Type'First .. Container.Last)); Sort (Container.Elements (Index_Type'First .. Container.Last));
...@@ -996,20 +1023,25 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -996,20 +1023,25 @@ package body Ada.Containers.Indefinite_Vectors is
is is
N : constant Int := Int (Count); N : constant Int := Int (Count);
First : constant Int := Int (Index_Type'First);
New_Last_As_Int : Int'Base; New_Last_As_Int : Int'Base;
New_Last : Index_Type; New_Last : Index_Type;
New_Length : UInt;
Max_Length : constant UInt := UInt (Count_Type'Last);
Dst : Elements_Access; Dst : Elements_Access;
begin begin
if Before < Index_Type'First then if Before < Index_Type'First then
raise Constraint_Error; raise Constraint_Error with
"Before index is out of range (too small)";
end if; end if;
if Before > Container.Last if Before > Container.Last
and then Before > Container.Last + 1 and then Before > Container.Last + 1
then then
raise Constraint_Error; raise Constraint_Error with
"Before index is out of range (too large)";
end if; end if;
if Count = 0 then if Count = 0 then
...@@ -1020,17 +1052,28 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1020,17 +1052,28 @@ package body Ada.Containers.Indefinite_Vectors is
Old_Last_As_Int : constant Int := Int (Container.Last); Old_Last_As_Int : constant Int := Int (Container.Last);
begin begin
if Old_Last_As_Int > Int'Last - N then -- see a-convec.adb ???
raise Constraint_Error with "new length is out of range";
end if;
New_Last_As_Int := Old_Last_As_Int + N; New_Last_As_Int := Old_Last_As_Int + N;
if New_Last_As_Int > Index_Type'Pos (Index_Type'Last) then if New_Last_As_Int > Int (Index_Type'Last) then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if;
New_Length := UInt (New_Last_As_Int - First + 1);
if New_Length > Max_Length then
raise Constraint_Error with "new length is out of range";
end if; end if;
New_Last := Index_Type (New_Last_As_Int); New_Last := Index_Type (New_Last_As_Int);
end; end;
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
if Container.Elements = null then if Container.Elements = null then
...@@ -1050,6 +1093,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1050,6 +1093,7 @@ package body Ada.Containers.Indefinite_Vectors is
if New_Last <= Container.Elements'Last then if New_Last <= Container.Elements'Last then
declare declare
E : Elements_Type renames Container.Elements.all; E : Elements_Type renames Container.Elements.all;
begin begin
if Before <= Container.Last then if Before <= Container.Last then
declare declare
...@@ -1058,16 +1102,18 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1058,16 +1102,18 @@ package body Ada.Containers.Indefinite_Vectors is
Index : constant Index_Type := Index_Type (Index_As_Int); Index : constant Index_Type := Index_Type (Index_As_Int);
J : Index_Type'Base := Before; J : Index_Type'Base;
begin begin
E (Index .. New_Last) := E (Before .. Container.Last); E (Index .. New_Last) := E (Before .. Container.Last);
Container.Last := New_Last; Container.Last := New_Last;
J := Before;
while J < Index loop while J < Index loop
E (J) := new Element_Type'(New_Item); E (J) := new Element_Type'(New_Item);
J := J + 1; J := J + 1;
end loop; end loop;
exception exception
when others => when others =>
E (J .. Index - 1) := (others => null); E (J .. Index - 1) := (others => null);
...@@ -1086,35 +1132,40 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1086,35 +1132,40 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
declare declare
First : constant Int := Int (Index_Type'First); C, CC : UInt;
New_Size : constant Int'Base := New_Last_As_Int - First + 1;
Size : Int'Base := Int'Max (1, Container.Elements'Length);
begin begin
while Size < New_Size loop C := UInt'Max (1, Container.Elements'Length);
if Size > Int'Last / 2 then while C < New_Length loop
Size := Int'Last; if C > UInt'Last / 2 then
C := UInt'Last;
exit; exit;
end if; end if;
Size := 2 * Size; C := 2 * C;
end loop; end loop;
-- TODO: The following calculations aren't quite right, since if C > Max_Length then
-- there will be overflow if Index_Type'Range is very large C := Max_Length;
-- (e.g. this package is instantiated with a 64-bit integer). end if;
-- END TODO.
declare if Index_Type'First <= 0
Max_Size : constant Int'Base := Int (Index_Type'Last) - First + 1; and then Index_Type'Last >= 0
begin then
if Size > Max_Size then CC := UInt (Index_Type'Last) + UInt (-Index_Type'First) + 1;
Size := Max_Size;
end if; else
end; CC := UInt (Int (Index_Type'Last) - First + 1);
end if;
if C > CC then
C := CC;
end if;
declare declare
Dst_Last : constant Index_Type := Index_Type (First + Size - 1); Dst_Last : constant Index_Type :=
Index_Type (First + UInt'Pos (C) - Int'(1));
begin begin
Dst := new Elements_Type (Index_Type'First .. Dst_Last); Dst := new Elements_Type (Index_Type'First .. Dst_Last);
end; end;
...@@ -1172,13 +1223,15 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1172,13 +1223,15 @@ package body Ada.Containers.Indefinite_Vectors is
begin begin
if Before < Index_Type'First then if Before < Index_Type'First then
raise Constraint_Error; raise Constraint_Error with
"Before index is out of range (too small)";
end if; end if;
if Before > Container.Last if Before > Container.Last
and then Before > Container.Last + 1 and then Before > Container.Last + 1
then then
raise Constraint_Error; raise Constraint_Error with
"Before index is out of range (too large)";
end if; end if;
if N = 0 then if N = 0 then
...@@ -1268,7 +1321,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1268,7 +1321,7 @@ package body Ada.Containers.Indefinite_Vectors is
if Before.Container /= null if Before.Container /= null
and then Before.Container /= Container'Unchecked_Access and then Before.Container /= Container'Unchecked_Access
then then
raise Program_Error; raise Program_Error with "Before cursor denotes wrong container";
end if; end if;
if Is_Empty (New_Item) then if Is_Empty (New_Item) then
...@@ -1279,7 +1332,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1279,7 +1332,8 @@ package body Ada.Containers.Indefinite_Vectors is
or else Before.Index > Container.Last or else Before.Index > Container.Last
then then
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with
"vector is already at its maximum length";
end if; end if;
Index := Container.Last + 1; Index := Container.Last + 1;
...@@ -1303,7 +1357,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1303,7 +1357,7 @@ package body Ada.Containers.Indefinite_Vectors is
if Before.Container /= null if Before.Container /= null
and then Before.Container /= Vector_Access'(Container'Unchecked_Access) and then Before.Container /= Vector_Access'(Container'Unchecked_Access)
then then
raise Program_Error; raise Program_Error with "Before cursor denotes wrong container";
end if; end if;
if Is_Empty (New_Item) then if Is_Empty (New_Item) then
...@@ -1322,7 +1376,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1322,7 +1376,8 @@ package body Ada.Containers.Indefinite_Vectors is
or else Before.Index > Container.Last or else Before.Index > Container.Last
then then
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with
"vector is already at its maximum length";
end if; end if;
Index := Container.Last + 1; Index := Container.Last + 1;
...@@ -1346,9 +1401,9 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1346,9 +1401,9 @@ package body Ada.Containers.Indefinite_Vectors is
begin begin
if Before.Container /= null if Before.Container /= null
and then Before.Container /= Vector_Access'(Container'Unchecked_Access) and then Before.Container /= Container'Unchecked_Access
then then
raise Program_Error; raise Program_Error with "Before cursor denotes wrong container";
end if; end if;
if Count = 0 then if Count = 0 then
...@@ -1359,7 +1414,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1359,7 +1414,8 @@ package body Ada.Containers.Indefinite_Vectors is
or else Before.Index > Container.Last or else Before.Index > Container.Last
then then
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with
"vector is already at its maximum length";
end if; end if;
Index := Container.Last + 1; Index := Container.Last + 1;
...@@ -1382,9 +1438,9 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1382,9 +1438,9 @@ package body Ada.Containers.Indefinite_Vectors is
begin begin
if Before.Container /= null if Before.Container /= null
and then Before.Container /= Vector_Access'(Container'Unchecked_Access) and then Before.Container /= Container'Unchecked_Access
then then
raise Program_Error; raise Program_Error with "Before cursor denotes wrong container";
end if; end if;
if Count = 0 then if Count = 0 then
...@@ -1403,7 +1459,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1403,7 +1459,8 @@ package body Ada.Containers.Indefinite_Vectors is
or else Before.Index > Container.Last or else Before.Index > Container.Last
then then
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with
"vector is already at its maximum length";
end if; end if;
Index := Container.Last + 1; Index := Container.Last + 1;
...@@ -1428,20 +1485,25 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1428,20 +1485,25 @@ package body Ada.Containers.Indefinite_Vectors is
is is
N : constant Int := Int (Count); N : constant Int := Int (Count);
First : constant Int := Int (Index_Type'First);
New_Last_As_Int : Int'Base; New_Last_As_Int : Int'Base;
New_Last : Index_Type; New_Last : Index_Type;
New_Length : UInt;
Max_Length : constant UInt := UInt (Count_Type'Last);
Dst : Elements_Access; Dst : Elements_Access;
begin begin
if Before < Index_Type'First then if Before < Index_Type'First then
raise Constraint_Error; raise Constraint_Error with
"Before index is out of range (too small)";
end if; end if;
if Before > Container.Last if Before > Container.Last
and then Before > Container.Last + 1 and then Before > Container.Last + 1
then then
raise Constraint_Error; raise Constraint_Error with
"Before index is out of range (too large)";
end if; end if;
if Count = 0 then if Count = 0 then
...@@ -1452,17 +1514,28 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1452,17 +1514,28 @@ package body Ada.Containers.Indefinite_Vectors is
Old_Last_As_Int : constant Int := Int (Container.Last); Old_Last_As_Int : constant Int := Int (Container.Last);
begin begin
if Old_Last_As_Int > Int'Last - N then -- see a-convec.adb ???
raise Constraint_Error with "new length is out of range";
end if;
New_Last_As_Int := Old_Last_As_Int + N; New_Last_As_Int := Old_Last_As_Int + N;
if New_Last_As_Int > Index_Type'Pos (Index_Type'Last) then if New_Last_As_Int > Int (Index_Type'Last) then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if;
New_Length := UInt (New_Last_As_Int - First + 1);
if New_Length > Max_Length then
raise Constraint_Error with "new length is out of range";
end if; end if;
New_Last := Index_Type (New_Last_As_Int); New_Last := Index_Type (New_Last_As_Int);
end; end;
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
if Container.Elements = null then if Container.Elements = null then
...@@ -1497,35 +1570,40 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1497,35 +1570,40 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
declare declare
First : constant Int := Int (Index_Type'First); C, CC : UInt;
New_Size : constant Int'Base := New_Last_As_Int - First + 1;
Size : Int'Base := Int'Max (1, Container.Elements'Length);
begin begin
while Size < New_Size loop C := UInt'Max (1, Container.Elements'Length);
if Size > Int'Last / 2 then while C < New_Length loop
Size := Int'Last; if C > UInt'Last / 2 then
C := UInt'Last;
exit; exit;
end if; end if;
Size := 2 * Size; C := 2 * C;
end loop; end loop;
-- TODO: The following calculations aren't quite right, since if C > Max_Length then
-- there will be overflow if Index_Type'Range is very large C := Max_Length;
-- (e.g. this package is instantiated with a 64-bit integer). end if;
-- END TODO.
declare if Index_Type'First <= 0
Max_Size : constant Int'Base := Int (Index_Type'Last) - First + 1; and then Index_Type'Last >= 0
begin then
if Size > Max_Size then CC := UInt (Index_Type'Last) + UInt (-Index_Type'First) + 1;
Size := Max_Size;
end if; else
end; CC := UInt (Int (Index_Type'Last) - First + 1);
end if;
if C > CC then
C := CC;
end if;
declare declare
Dst_Last : constant Index_Type := Index_Type (First + Size - 1); Dst_Last : constant Index_Type :=
Index_Type (First + UInt'Pos (C) - 1);
begin begin
Dst := new Elements_Type (Index_Type'First .. Dst_Last); Dst := new Elements_Type (Index_Type'First .. Dst_Last);
end; end;
...@@ -1570,9 +1648,9 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1570,9 +1648,9 @@ package body Ada.Containers.Indefinite_Vectors is
begin begin
if Before.Container /= null if Before.Container /= null
and then Before.Container /= Vector_Access'(Container'Unchecked_Access) and then Before.Container /= Container'Unchecked_Access
then then
raise Program_Error; raise Program_Error with "Before cursor denotes wrong container";
end if; end if;
if Count = 0 then if Count = 0 then
...@@ -1591,7 +1669,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1591,7 +1669,8 @@ package body Ada.Containers.Indefinite_Vectors is
or else Before.Index > Container.Last or else Before.Index > Container.Last
then then
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with
"vector is already at its maximum length";
end if; end if;
Index := Container.Last + 1; Index := Container.Last + 1;
...@@ -1682,10 +1761,6 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1682,10 +1761,6 @@ package body Ada.Containers.Indefinite_Vectors is
N : constant Int'Base := L - F + 1; N : constant Int'Base := L - F + 1;
begin begin
if N > Count_Type'Pos (Count_Type'Last) then
raise Constraint_Error;
end if;
return Count_Type (N); return Count_Type (N);
end Length; end Length;
...@@ -1703,10 +1778,11 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1703,10 +1778,11 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
if Source.Busy > 0 then if Source.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (Source is busy)";
end if; end if;
Clear (Target); Clear (Target); -- Checks busy-bit
declare declare
Target_Elements : constant Elements_Access := Target.Elements; Target_Elements : constant Elements_Access := Target.Elements;
...@@ -1819,11 +1895,11 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1819,11 +1895,11 @@ package body Ada.Containers.Indefinite_Vectors is
begin begin
if Index > Container.Last then if Index > Container.Last then
raise Constraint_Error; raise Constraint_Error with "Index is out of range";
end if; end if;
if V.Elements (Index) = null then if V.Elements (Index) = null then
raise Constraint_Error; raise Constraint_Error with "element is null";
end if; end if;
B := B + 1; B := B + 1;
...@@ -1848,7 +1924,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1848,7 +1924,7 @@ package body Ada.Containers.Indefinite_Vectors is
is is
begin begin
if Position.Container = null then if Position.Container = null then
raise Constraint_Error; raise Constraint_Error with "Position cursor has no element";
end if; end if;
Query_Element (Position.Container.all, Position.Index, Process); Query_Element (Position.Container.all, Position.Index, Process);
...@@ -1859,7 +1935,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1859,7 +1935,7 @@ package body Ada.Containers.Indefinite_Vectors is
---------- ----------
procedure Read procedure Read
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Container : out Vector) Container : out Vector)
is is
Length : Count_Type'Base; Length : Count_Type'Base;
...@@ -1891,11 +1967,11 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1891,11 +1967,11 @@ package body Ada.Containers.Indefinite_Vectors is
end Read; end Read;
procedure Read procedure Read
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Position : out Cursor) Position : out Cursor)
is is
begin begin
raise Program_Error; raise Program_Error with "attempt to stream vector cursor";
end Read; end Read;
--------------------- ---------------------
...@@ -1909,11 +1985,12 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1909,11 +1985,12 @@ package body Ada.Containers.Indefinite_Vectors is
is is
begin begin
if Index > Container.Last then if Index > Container.Last then
raise Constraint_Error; raise Constraint_Error with "Index is out of range";
end if; end if;
if Container.Lock > 0 then if Container.Lock > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with cursors (vector is locked)";
end if; end if;
declare declare
...@@ -1931,11 +2008,11 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1931,11 +2008,11 @@ package body Ada.Containers.Indefinite_Vectors is
is is
begin begin
if Position.Container = null then if Position.Container = null then
raise Constraint_Error; raise Constraint_Error with "Position cursor has no element";
end if; end if;
if Position.Container /= Container'Unrestricted_Access then if Position.Container /= Container'Unrestricted_Access then
raise Program_Error; raise Program_Error with "Position cursor denotes wrong container";
end if; end if;
Replace_Element (Container, Position.Index, New_Item); Replace_Element (Container, Position.Index, New_Item);
...@@ -1963,7 +2040,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1963,7 +2040,8 @@ package body Ada.Containers.Indefinite_Vectors is
elsif N < Container.Elements'Length then elsif N < Container.Elements'Length then
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
declare declare
...@@ -1994,7 +2072,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -1994,7 +2072,7 @@ package body Ada.Containers.Indefinite_Vectors is
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if; end if;
declare declare
...@@ -2014,7 +2092,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2014,7 +2092,8 @@ package body Ada.Containers.Indefinite_Vectors is
if Capacity <= N then if Capacity <= N then
if N < Container.Elements'Length then if N < Container.Elements'Length then
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
declare declare
...@@ -2043,7 +2122,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2043,7 +2122,8 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
declare declare
...@@ -2052,7 +2132,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2052,7 +2132,7 @@ package body Ada.Containers.Indefinite_Vectors is
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if; end if;
declare declare
...@@ -2093,15 +2173,18 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2093,15 +2173,18 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
if Container.Lock > 0 then if Container.Lock > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with cursors (vector is locked)";
end if; end if;
declare declare
I : Index_Type := Index_Type'First; I : Index_Type;
J : Index_Type := Container.Last; J : Index_Type;
E : Elements_Type renames Container.Elements.all; E : Elements_Type renames Container.Elements.all;
begin begin
I := Index_Type'First;
J := Container.Last;
while I < J loop while I < J loop
declare declare
EI : constant Element_Access := E (I); EI : constant Element_Access := E (I);
...@@ -2132,7 +2215,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2132,7 +2215,7 @@ package body Ada.Containers.Indefinite_Vectors is
if Position.Container /= null if Position.Container /= null
and then Position.Container /= Container'Unchecked_Access and then Position.Container /= Container'Unchecked_Access
then then
raise Program_Error; raise Program_Error with "Position cursor denotes wrong container";
end if; end if;
if Position.Container = null if Position.Container = null
...@@ -2226,7 +2309,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2226,7 +2309,8 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
if Length < N then if Length < N then
...@@ -2267,10 +2351,12 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2267,10 +2351,12 @@ package body Ada.Containers.Indefinite_Vectors is
I, J : Index_Type) I, J : Index_Type)
is is
begin begin
if I > Container.Last if I > Container.Last then
or else J > Container.Last raise Constraint_Error with "I index is out of range";
then end if;
raise Constraint_Error;
if J > Container.Last then
raise Constraint_Error with "J index is out of range";
end if; end if;
if I = J then if I = J then
...@@ -2278,7 +2364,8 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2278,7 +2364,8 @@ package body Ada.Containers.Indefinite_Vectors is
end if; end if;
if Container.Lock > 0 then if Container.Lock > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with cursors (vector is locked)";
end if; end if;
declare declare
...@@ -2298,16 +2385,20 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2298,16 +2385,20 @@ package body Ada.Containers.Indefinite_Vectors is
I, J : Cursor) I, J : Cursor)
is is
begin begin
if I.Container = null if I.Container = null then
or else J.Container = null raise Constraint_Error with "I cursor has no element";
then
raise Constraint_Error;
end if; end if;
if I.Container /= Container'Unrestricted_Access if J.Container = null then
or else J.Container /= Container'Unrestricted_Access raise Constraint_Error with "J cursor has no element";
then end if;
raise Program_Error;
if I.Container /= Container'Unrestricted_Access then
raise Program_Error with "I cursor denotes wrong container";
end if;
if J.Container /= Container'Unrestricted_Access then
raise Program_Error with "J cursor denotes wrong container";
end if; end if;
Swap (Container, I.Index, J.Index); Swap (Container, I.Index, J.Index);
...@@ -2364,7 +2455,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2364,7 +2455,7 @@ package body Ada.Containers.Indefinite_Vectors is
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
raise Constraint_Error; raise Constraint_Error with "Length is out of range";
end if; end if;
Last := Index_Type (Last_As_Int); Last := Index_Type (Last_As_Int);
...@@ -2391,7 +2482,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2391,7 +2482,7 @@ package body Ada.Containers.Indefinite_Vectors is
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
raise Constraint_Error; raise Constraint_Error with "Length is out of range";
end if; end if;
Last := Index_Type (Last_As_Int); Last := Index_Type (Last_As_Int);
...@@ -2433,11 +2524,11 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2433,11 +2524,11 @@ package body Ada.Containers.Indefinite_Vectors is
begin begin
if Index > Container.Last then if Index > Container.Last then
raise Constraint_Error; raise Constraint_Error with "Index is out of range";
end if; end if;
if Container.Elements (Index) = null then if Container.Elements (Index) = null then
raise Constraint_Error; raise Constraint_Error with "element is null";
end if; end if;
B := B + 1; B := B + 1;
...@@ -2463,11 +2554,11 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2463,11 +2554,11 @@ package body Ada.Containers.Indefinite_Vectors is
is is
begin begin
if Position.Container = null then if Position.Container = null then
raise Constraint_Error; raise Constraint_Error with "Position cursor has no element";
end if; end if;
if Position.Container /= Container'Unrestricted_Access then if Position.Container /= Container'Unrestricted_Access then
raise Program_Error; raise Program_Error with "Position cursor denotes wrong container";
end if; end if;
Update_Element (Container, Position.Index, Process); Update_Element (Container, Position.Index, Process);
...@@ -2478,7 +2569,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2478,7 +2569,7 @@ package body Ada.Containers.Indefinite_Vectors is
----------- -----------
procedure Write procedure Write
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Container : Vector) Container : Vector)
is is
N : constant Count_Type := Length (Container); N : constant Count_Type := Length (Container);
...@@ -2499,7 +2590,7 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2499,7 +2590,7 @@ package body Ada.Containers.Indefinite_Vectors is
-- There's another way to do this. Instead a separate -- There's another way to do this. Instead a separate
-- Boolean for each element, you could write a Boolean -- Boolean for each element, you could write a Boolean
-- followed by a count of how many nulls or non-nulls -- followed by a count of how many nulls or non-nulls
-- follow in the array. -- follow in the array. ???
if E (Indx) = null then if E (Indx) = null then
Boolean'Write (Stream, False); Boolean'Write (Stream, False);
...@@ -2512,11 +2603,11 @@ package body Ada.Containers.Indefinite_Vectors is ...@@ -2512,11 +2603,11 @@ package body Ada.Containers.Indefinite_Vectors is
end Write; end Write;
procedure Write procedure Write
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Position : Cursor) Position : Cursor)
is is
begin begin
raise Program_Error; raise Program_Error with "attempt to stream vector cursor";
end Write; end Write;
end Ada.Containers.Indefinite_Vectors; end Ada.Containers.Indefinite_Vectors;
...@@ -324,13 +324,13 @@ private ...@@ -324,13 +324,13 @@ private
use Ada.Streams; use Ada.Streams;
procedure Write procedure Write
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Container : Vector); Container : Vector);
for Vector'Write use Write; for Vector'Write use Write;
procedure Read procedure Read
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Container : out Vector); Container : out Vector);
for Vector'Read use Read; for Vector'Read use Read;
...@@ -346,13 +346,13 @@ private ...@@ -346,13 +346,13 @@ private
end record; end record;
procedure Write procedure Write
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Position : Cursor); Position : Cursor);
for Cursor'Write use Write; for Cursor'Write use Write;
procedure Read procedure Read
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Position : out Cursor); Position : out Cursor);
for Cursor'Read use Read; for Cursor'Read use Read;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
-- -- -- --
-- B o d y -- -- B o d y --
-- -- -- --
-- Copyright (C) 2004-2005 Free Software Foundation, Inc. -- -- Copyright (C) 2004-2006 Free Software Foundation, Inc. --
-- -- -- --
-- This specification is derived from the Ada Reference Manual for use with -- -- This specification is derived from the Ada Reference Manual for use with --
-- GNAT. The copyright notice above, and the license provisions that follow -- -- GNAT. The copyright notice above, and the license provisions that follow --
...@@ -41,6 +41,7 @@ with System; use type System.Address; ...@@ -41,6 +41,7 @@ with System; use type System.Address;
package body Ada.Containers.Vectors is package body Ada.Containers.Vectors is
type Int is range System.Min_Int .. System.Max_Int; type Int is range System.Min_Int .. System.Max_Int;
type UInt is mod System.Max_Binary_Modulus;
procedure Free is procedure Free is
new Ada.Unchecked_Deallocation (Elements_Type, Elements_Access); new Ada.Unchecked_Deallocation (Elements_Type, Elements_Access);
...@@ -86,12 +87,18 @@ package body Ada.Containers.Vectors is ...@@ -86,12 +87,18 @@ package body Ada.Containers.Vectors is
end if; end if;
declare declare
Last_As_Int : constant Int'Base := -- TODO: handle overflow N : constant Int'Base := Int (LN) + Int (RN);
Int (Index_Type'First) + Int (LN) + Int (RN) - 1; Last_As_Int : Int'Base;
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Int (No_Index) > Int'Last - N then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if;
Last_As_Int := Int (No_Index) + N;
if Last_As_Int > Int (Index_Type'Last) then
raise Constraint_Error with "new length is out of range";
end if; end if;
declare declare
...@@ -130,12 +137,17 @@ package body Ada.Containers.Vectors is ...@@ -130,12 +137,17 @@ package body Ada.Containers.Vectors is
end if; end if;
declare declare
Last_As_Int : constant Int'Base := -- TODO: handle overflow Last_As_Int : Int'Base;
Int (Index_Type'First) + Int (LN);
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Int (Index_Type'First) > Int'Last - Int (LN) then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if;
Last_As_Int := Int (Index_Type'First) + Int (LN);
if Last_As_Int > Int (Index_Type'Last) then
raise Constraint_Error with "new length is out of range";
end if; end if;
declare declare
...@@ -172,12 +184,17 @@ package body Ada.Containers.Vectors is ...@@ -172,12 +184,17 @@ package body Ada.Containers.Vectors is
end if; end if;
declare declare
Last_As_Int : constant Int'Base := -- TODO: handle overflow Last_As_Int : Int'Base;
Int (Index_Type'First) + Int (RN);
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Int (Index_Type'First) > Int'Last - Int (RN) then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if;
Last_As_Int := Int (Index_Type'First) + Int (RN);
if Last_As_Int > Int (Index_Type'Last) then
raise Constraint_Error with "new length is out of range";
end if; end if;
declare declare
...@@ -196,10 +213,10 @@ package body Ada.Containers.Vectors is ...@@ -196,10 +213,10 @@ package body Ada.Containers.Vectors is
end; end;
end "&"; end "&";
function "&" (Left, Right : Element_Type) return Vector is function "&" (Left, Right : Element_Type) return Vector is
begin begin
if Index_Type'First >= Index_Type'Last then if Index_Type'First >= Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if; end if;
declare declare
...@@ -273,7 +290,7 @@ package body Ada.Containers.Vectors is ...@@ -273,7 +290,7 @@ package body Ada.Containers.Vectors is
end if; end if;
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with "vector is already at its maximum length";
end if; end if;
Insert Insert
...@@ -293,7 +310,7 @@ package body Ada.Containers.Vectors is ...@@ -293,7 +310,7 @@ package body Ada.Containers.Vectors is
end if; end if;
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with "vector is already at its maximum length";
end if; end if;
Insert Insert
...@@ -323,7 +340,8 @@ package body Ada.Containers.Vectors is ...@@ -323,7 +340,8 @@ package body Ada.Containers.Vectors is
procedure Clear (Container : in out Vector) is procedure Clear (Container : in out Vector) is
begin begin
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
Container.Last := No_Index; Container.Last := No_Index;
...@@ -352,12 +370,12 @@ package body Ada.Containers.Vectors is ...@@ -352,12 +370,12 @@ package body Ada.Containers.Vectors is
is is
begin begin
if Index < Index_Type'First then if Index < Index_Type'First then
raise Constraint_Error; raise Constraint_Error with "Index is out of range (too small)";
end if; end if;
if Index > Container.Last then if Index > Container.Last then
if Index > Container.Last + 1 then if Index > Container.Last + 1 then
raise Constraint_Error; raise Constraint_Error with "Index is out of range (too large)";
end if; end if;
return; return;
...@@ -368,7 +386,8 @@ package body Ada.Containers.Vectors is ...@@ -368,7 +386,8 @@ package body Ada.Containers.Vectors is
end if; end if;
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
declare declare
...@@ -409,13 +428,15 @@ package body Ada.Containers.Vectors is ...@@ -409,13 +428,15 @@ package body Ada.Containers.Vectors is
is is
begin begin
if Position.Container = null then if Position.Container = null then
raise Constraint_Error; raise Constraint_Error with "Position cursor has no element";
end if; end if;
if Position.Container /= Container'Unrestricted_Access if Position.Container /= Container'Unrestricted_Access then
or else Position.Index > Container.Last raise Program_Error with "Position cursor denotes wrong container";
then end if;
raise Program_Error;
if Position.Index > Container.Last then
raise Program_Error with "Position index is out of range";
end if; end if;
Delete (Container, Position.Index, Count); Delete (Container, Position.Index, Count);
...@@ -470,7 +491,8 @@ package body Ada.Containers.Vectors is ...@@ -470,7 +491,8 @@ package body Ada.Containers.Vectors is
end if; end if;
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
Index := Int'Base (Container.Last) - Int'Base (Count); Index := Int'Base (Container.Last) - Int'Base (Count);
...@@ -492,7 +514,7 @@ package body Ada.Containers.Vectors is ...@@ -492,7 +514,7 @@ package body Ada.Containers.Vectors is
is is
begin begin
if Index > Container.Last then if Index > Container.Last then
raise Constraint_Error; raise Constraint_Error with "Index is out of range";
end if; end if;
return Container.Elements (Index); return Container.Elements (Index);
...@@ -501,7 +523,7 @@ package body Ada.Containers.Vectors is ...@@ -501,7 +523,7 @@ package body Ada.Containers.Vectors is
function Element (Position : Cursor) return Element_Type is function Element (Position : Cursor) return Element_Type is
begin begin
if Position.Container = null then if Position.Container = null then
raise Constraint_Error; raise Constraint_Error with "Position cursor has no element";
end if; end if;
return Element (Position.Container.all, Position.Index); return Element (Position.Container.all, Position.Index);
...@@ -516,7 +538,8 @@ package body Ada.Containers.Vectors is ...@@ -516,7 +538,8 @@ package body Ada.Containers.Vectors is
begin begin
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
Container.Elements := null; Container.Elements := null;
...@@ -534,11 +557,14 @@ package body Ada.Containers.Vectors is ...@@ -534,11 +557,14 @@ package body Ada.Containers.Vectors is
Position : Cursor := No_Element) return Cursor Position : Cursor := No_Element) return Cursor
is is
begin begin
if Position.Container /= null if Position.Container /= null then
and then (Position.Container /= Container'Unrestricted_Access if Position.Container /= Container'Unrestricted_Access then
or else Position.Index > Container.Last) raise Program_Error with "Position cursor denotes wrong container";
then end if;
raise Program_Error;
if Position.Index > Container.Last then
raise Program_Error with "Position index is out of range";
end if;
end if; end if;
for J in Position.Index .. Container.Last loop for J in Position.Index .. Container.Last loop
...@@ -653,7 +679,8 @@ package body Ada.Containers.Vectors is ...@@ -653,7 +679,8 @@ package body Ada.Containers.Vectors is
end if; end if;
if Source.Busy > 0 then if Source.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
Target.Set_Length (Length (Target) + Length (Source)); Target.Set_Length (Length (Target) + Length (Source));
...@@ -708,7 +735,8 @@ package body Ada.Containers.Vectors is ...@@ -708,7 +735,8 @@ package body Ada.Containers.Vectors is
end if; end if;
if Container.Lock > 0 then if Container.Lock > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with cursors (vector is locked)";
end if; end if;
Sort (Container.Elements (Index_Type'First .. Container.Last)); Sort (Container.Elements (Index_Type'First .. Container.Last));
...@@ -741,20 +769,25 @@ package body Ada.Containers.Vectors is ...@@ -741,20 +769,25 @@ package body Ada.Containers.Vectors is
is is
N : constant Int := Count_Type'Pos (Count); N : constant Int := Count_Type'Pos (Count);
First : constant Int := Int (Index_Type'First);
New_Last_As_Int : Int'Base; New_Last_As_Int : Int'Base;
New_Last : Index_Type; New_Last : Index_Type;
New_Length : UInt;
Max_Length : constant UInt := UInt (Count_Type'Last);
Dst : Elements_Access; Dst : Elements_Access;
begin begin
if Before < Index_Type'First then if Before < Index_Type'First then
raise Constraint_Error; raise Constraint_Error with
"Before index is out of range (too small)";
end if; end if;
if Before > Container.Last if Before > Container.Last
and then Before > Container.Last + 1 and then Before > Container.Last + 1
then then
raise Constraint_Error; raise Constraint_Error with
"Before index is out of range (too large)";
end if; end if;
if Count = 0 then if Count = 0 then
...@@ -762,22 +795,59 @@ package body Ada.Containers.Vectors is ...@@ -762,22 +795,59 @@ package body Ada.Containers.Vectors is
end if; end if;
declare declare
Old_Last : constant Extended_Index := Container.Last; Old_Last_As_Int : constant Int := Int (Container.Last);
Old_Last_As_Int : constant Int := Index_Type'Pos (Old_Last);
begin begin
if Old_Last_As_Int > Int'Last - N then
-- ???
-- The purpose of this test is to ensure that the calculation of
-- New_Last_As_Int (see below) doesn't overflow.
-- This isn't quite right, since the only requirements are:
-- V.Last <= Index_Type'Last
-- V.Length <= Count_Type'Last
-- To be strictly correct there's no (explicit) requirement that
-- Old_Last + N <= Int'Last
-- However, there might indeed be an implied requirement, since
-- machine constraints dictate that
-- Index_Type'Last <= Int'Last
-- and so this check is perhaps proper after all.
-- This shouldn't be an issue in practice, since it can only
-- happen when N is very large, or V.Last is near Int'Last.
-- N isn't likely to be large, since there's probably not enough
-- storage.
-- V.Last would only be large if IT'First is very large (and
-- V.Length has some "normal" size). But typically IT'First is
-- either 0 or 1.
raise Constraint_Error with "new length is out of range";
end if;
New_Last_As_Int := Old_Last_As_Int + N; New_Last_As_Int := Old_Last_As_Int + N;
if New_Last_As_Int > Index_Type'Pos (Index_Type'Last) then if New_Last_As_Int > Int (Index_Type'Last) then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if;
New_Length := UInt (New_Last_As_Int - First + Int'(1));
if New_Length > Max_Length then
raise Constraint_Error with "new length is out of range";
end if; end if;
New_Last := Index_Type (New_Last_As_Int); New_Last := Index_Type (New_Last_As_Int);
end; end;
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
if Container.Elements = null then if Container.Elements = null then
...@@ -795,6 +865,7 @@ package body Ada.Containers.Vectors is ...@@ -795,6 +865,7 @@ package body Ada.Containers.Vectors is
if New_Last <= Container.Elements'Last then if New_Last <= Container.Elements'Last then
declare declare
E : Elements_Type renames Container.Elements.all; E : Elements_Type renames Container.Elements.all;
begin begin
if Before <= Container.Last then if Before <= Container.Last then
declare declare
...@@ -820,35 +891,40 @@ package body Ada.Containers.Vectors is ...@@ -820,35 +891,40 @@ package body Ada.Containers.Vectors is
end if; end if;
declare declare
First : constant Int := Int (Index_Type'First); C, CC : UInt;
New_Size : constant Int'Base := New_Last_As_Int - First + 1;
Size : Int'Base := Int'Max (1, Container.Elements'Length);
begin begin
while Size < New_Size loop C := UInt'Max (1, Container.Elements'Length);
if Size > Int'Last / 2 then while C < New_Length loop
Size := Int'Last; if C > UInt'Last / 2 then
C := UInt'Last;
exit; exit;
end if; end if;
Size := 2 * Size; C := 2 * C;
end loop; end loop;
-- TODO: The following calculations aren't quite right, since if C > Max_Length then
-- there will be overflow if Index_Type'Range is very large C := Max_Length;
-- (e.g. this package is instantiated with a 64-bit integer). end if;
-- END TODO.
declare if Index_Type'First <= 0
Max_Size : constant Int'Base := Int (Index_Type'Last) - First + 1; and then Index_Type'Last >= 0
begin then
if Size > Max_Size then CC := UInt (Index_Type'Last) + UInt (-Index_Type'First) + 1;
Size := Max_Size;
end if; else
end; CC := UInt (Int (Index_Type'Last) - First + 1);
end if;
if C > CC then
C := CC;
end if;
declare declare
Dst_Last : constant Index_Type := Index_Type (First + Size - 1); Dst_Last : constant Index_Type :=
Index_Type (First + UInt'Pos (C) - 1);
begin begin
Dst := new Elements_Type (Index_Type'First .. Dst_Last); Dst := new Elements_Type (Index_Type'First .. Dst_Last);
end; end;
...@@ -900,13 +976,15 @@ package body Ada.Containers.Vectors is ...@@ -900,13 +976,15 @@ package body Ada.Containers.Vectors is
begin begin
if Before < Index_Type'First then if Before < Index_Type'First then
raise Constraint_Error; raise Constraint_Error with
"Before index is out of range (too small)";
end if; end if;
if Before > Container.Last if Before > Container.Last
and then Before > Container.Last + 1 and then Before > Container.Last + 1
then then
raise Constraint_Error; raise Constraint_Error with
"Before index is out of range (too large)";
end if; end if;
if N = 0 then if N = 0 then
...@@ -984,9 +1062,9 @@ package body Ada.Containers.Vectors is ...@@ -984,9 +1062,9 @@ package body Ada.Containers.Vectors is
begin begin
if Before.Container /= null if Before.Container /= null
and then Before.Container /= Vector_Access'(Container'Unchecked_Access) and then Before.Container /= Container'Unchecked_Access
then then
raise Program_Error; raise Program_Error with "Before cursor denotes wrong container";
end if; end if;
if Is_Empty (New_Item) then if Is_Empty (New_Item) then
...@@ -997,7 +1075,8 @@ package body Ada.Containers.Vectors is ...@@ -997,7 +1075,8 @@ package body Ada.Containers.Vectors is
or else Before.Index > Container.Last or else Before.Index > Container.Last
then then
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with
"vector is already at its maximum length";
end if; end if;
Index := Container.Last + 1; Index := Container.Last + 1;
...@@ -1019,9 +1098,9 @@ package body Ada.Containers.Vectors is ...@@ -1019,9 +1098,9 @@ package body Ada.Containers.Vectors is
begin begin
if Before.Container /= null if Before.Container /= null
and then Before.Container /= Vector_Access'(Container'Unchecked_Access) and then Before.Container /= Container'Unchecked_Access
then then
raise Program_Error; raise Program_Error with "Before cursor denotes wrong container";
end if; end if;
if Is_Empty (New_Item) then if Is_Empty (New_Item) then
...@@ -1040,7 +1119,8 @@ package body Ada.Containers.Vectors is ...@@ -1040,7 +1119,8 @@ package body Ada.Containers.Vectors is
or else Before.Index > Container.Last or else Before.Index > Container.Last
then then
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with
"vector is already at its maximum length";
end if; end if;
Index := Container.Last + 1; Index := Container.Last + 1;
...@@ -1064,9 +1144,9 @@ package body Ada.Containers.Vectors is ...@@ -1064,9 +1144,9 @@ package body Ada.Containers.Vectors is
begin begin
if Before.Container /= null if Before.Container /= null
and then Before.Container /= Vector_Access'(Container'Unchecked_Access) and then Before.Container /= Container'Unchecked_Access
then then
raise Program_Error; raise Program_Error with "Before cursor denotes wrong container";
end if; end if;
if Count = 0 then if Count = 0 then
...@@ -1077,7 +1157,8 @@ package body Ada.Containers.Vectors is ...@@ -1077,7 +1157,8 @@ package body Ada.Containers.Vectors is
or else Before.Index > Container.Last or else Before.Index > Container.Last
then then
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with
"vector is already at its maximum length";
end if; end if;
Index := Container.Last + 1; Index := Container.Last + 1;
...@@ -1100,9 +1181,9 @@ package body Ada.Containers.Vectors is ...@@ -1100,9 +1181,9 @@ package body Ada.Containers.Vectors is
begin begin
if Before.Container /= null if Before.Container /= null
and then Before.Container /= Vector_Access'(Container'Unchecked_Access) and then Before.Container /= Container'Unchecked_Access
then then
raise Program_Error; raise Program_Error with "Before cursor denotes wrong container";
end if; end if;
if Count = 0 then if Count = 0 then
...@@ -1121,7 +1202,8 @@ package body Ada.Containers.Vectors is ...@@ -1121,7 +1202,8 @@ package body Ada.Containers.Vectors is
or else Before.Index > Container.Last or else Before.Index > Container.Last
then then
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with
"vector is already at its maximum length";
end if; end if;
Index := Container.Last + 1; Index := Container.Last + 1;
...@@ -1171,20 +1253,25 @@ package body Ada.Containers.Vectors is ...@@ -1171,20 +1253,25 @@ package body Ada.Containers.Vectors is
is is
N : constant Int := Count_Type'Pos (Count); N : constant Int := Count_Type'Pos (Count);
First : constant Int := Int (Index_Type'First);
New_Last_As_Int : Int'Base; New_Last_As_Int : Int'Base;
New_Last : Index_Type; New_Last : Index_Type;
New_Length : UInt;
Max_Length : constant UInt := UInt (Count_Type'Last);
Dst : Elements_Access; Dst : Elements_Access;
begin begin
if Before < Index_Type'First then if Before < Index_Type'First then
raise Constraint_Error; raise Constraint_Error with
"Before index is out of range (too small)";
end if; end if;
if Before > Container.Last if Before > Container.Last
and then Before > Container.Last + 1 and then Before > Container.Last + 1
then then
raise Constraint_Error; raise Constraint_Error with
"Before index is out of range (too large)";
end if; end if;
if Count = 0 then if Count = 0 then
...@@ -1192,22 +1279,31 @@ package body Ada.Containers.Vectors is ...@@ -1192,22 +1279,31 @@ package body Ada.Containers.Vectors is
end if; end if;
declare declare
Old_Last : constant Extended_Index := Container.Last; Old_Last_As_Int : constant Int := Int (Container.Last);
Old_Last_As_Int : constant Int := Index_Type'Pos (Old_Last);
begin begin
if Old_Last_As_Int > Int'Last - N then -- see Insert ???
raise Constraint_Error with "new length is out of range";
end if;
New_Last_As_Int := Old_Last_As_Int + N; New_Last_As_Int := Old_Last_As_Int + N;
if New_Last_As_Int > Index_Type'Pos (Index_Type'Last) then if New_Last_As_Int > Int (Index_Type'Last) then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if;
New_Length := UInt (New_Last_As_Int - First + Int'(1));
if New_Length > Max_Length then
raise Constraint_Error with "new length is out of range";
end if; end if;
New_Last := Index_Type (New_Last_As_Int); New_Last := Index_Type (New_Last_As_Int);
end; end;
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
if Container.Elements = null then if Container.Elements = null then
...@@ -1240,35 +1336,40 @@ package body Ada.Containers.Vectors is ...@@ -1240,35 +1336,40 @@ package body Ada.Containers.Vectors is
end if; end if;
declare declare
First : constant Int := Int (Index_Type'First); C, CC : UInt;
New_Size : constant Int'Base := New_Last_As_Int - First + 1;
Size : Int'Base := Int'Max (1, Container.Elements'Length);
begin begin
while Size < New_Size loop C := UInt'Max (1, Container.Elements'Length);
if Size > Int'Last / 2 then while C < New_Length loop
Size := Int'Last; if C > UInt'Last / 2 then
C := UInt'Last;
exit; exit;
end if; end if;
Size := 2 * Size; C := 2 * C;
end loop; end loop;
-- TODO: The following calculations aren't quite right, since if C > Max_Length then
-- there will be overflow if Index_Type'Range is very large C := Max_Length;
-- (e.g. this package is instantiated with a 64-bit integer). end if;
-- END TODO.
declare if Index_Type'First <= 0
Max_Size : constant Int'Base := Int (Index_Type'Last) - First + 1; and then Index_Type'Last >= 0
begin then
if Size > Max_Size then CC := UInt (Index_Type'Last) + UInt (-Index_Type'First) + 1;
Size := Max_Size;
end if; else
end; CC := UInt (Int (Index_Type'Last) - First + 1);
end if;
if C > CC then
C := CC;
end if;
declare declare
Dst_Last : constant Index_Type := Index_Type (First + Size - 1); Dst_Last : constant Index_Type :=
Index_Type (First + UInt'Pos (C) - 1);
begin begin
Dst := new Elements_Type (Index_Type'First .. Dst_Last); Dst := new Elements_Type (Index_Type'First .. Dst_Last);
end; end;
...@@ -1317,9 +1418,9 @@ package body Ada.Containers.Vectors is ...@@ -1317,9 +1418,9 @@ package body Ada.Containers.Vectors is
begin begin
if Before.Container /= null if Before.Container /= null
and then Before.Container /= Vector_Access'(Container'Unchecked_Access) and then Before.Container /= Container'Unchecked_Access
then then
raise Program_Error; raise Program_Error with "Before cursor denotes wrong container";
end if; end if;
if Count = 0 then if Count = 0 then
...@@ -1338,7 +1439,8 @@ package body Ada.Containers.Vectors is ...@@ -1338,7 +1439,8 @@ package body Ada.Containers.Vectors is
or else Before.Index > Container.Last or else Before.Index > Container.Last
then then
if Container.Last = Index_Type'Last then if Container.Last = Index_Type'Last then
raise Constraint_Error; raise Constraint_Error with
"vector is already at its maximum length";
end if; end if;
Index := Container.Last + 1; Index := Container.Last + 1;
...@@ -1429,10 +1531,6 @@ package body Ada.Containers.Vectors is ...@@ -1429,10 +1531,6 @@ package body Ada.Containers.Vectors is
N : constant Int'Base := L - F + 1; N : constant Int'Base := L - F + 1;
begin begin
if N > Count_Type'Pos (Count_Type'Last) then
raise Constraint_Error;
end if;
return Count_Type (N); return Count_Type (N);
end Length; end Length;
...@@ -1450,11 +1548,13 @@ package body Ada.Containers.Vectors is ...@@ -1450,11 +1548,13 @@ package body Ada.Containers.Vectors is
end if; end if;
if Target.Busy > 0 then if Target.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (Target is busy)";
end if; end if;
if Source.Busy > 0 then if Source.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (Source is busy)";
end if; end if;
declare declare
...@@ -1568,7 +1668,7 @@ package body Ada.Containers.Vectors is ...@@ -1568,7 +1668,7 @@ package body Ada.Containers.Vectors is
begin begin
if Index > Container.Last then if Index > Container.Last then
raise Constraint_Error; raise Constraint_Error with "Index is out of range";
end if; end if;
B := B + 1; B := B + 1;
...@@ -1593,7 +1693,7 @@ package body Ada.Containers.Vectors is ...@@ -1593,7 +1693,7 @@ package body Ada.Containers.Vectors is
is is
begin begin
if Position.Container = null then if Position.Container = null then
raise Constraint_Error; raise Constraint_Error with "Position cursor has no element";
end if; end if;
Query_Element (Position.Container.all, Position.Index, Process); Query_Element (Position.Container.all, Position.Index, Process);
...@@ -1604,7 +1704,7 @@ package body Ada.Containers.Vectors is ...@@ -1604,7 +1704,7 @@ package body Ada.Containers.Vectors is
---------- ----------
procedure Read procedure Read
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Container : out Vector) Container : out Vector)
is is
Length : Count_Type'Base; Length : Count_Type'Base;
...@@ -1627,11 +1727,11 @@ package body Ada.Containers.Vectors is ...@@ -1627,11 +1727,11 @@ package body Ada.Containers.Vectors is
end Read; end Read;
procedure Read procedure Read
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Position : out Cursor) Position : out Cursor)
is is
begin begin
raise Program_Error; raise Program_Error with "attempt to stream vector cursor";
end Read; end Read;
--------------------- ---------------------
...@@ -1645,11 +1745,12 @@ package body Ada.Containers.Vectors is ...@@ -1645,11 +1745,12 @@ package body Ada.Containers.Vectors is
is is
begin begin
if Index > Container.Last then if Index > Container.Last then
raise Constraint_Error; raise Constraint_Error with "Index is out of range";
end if; end if;
if Container.Lock > 0 then if Container.Lock > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with cursors (vector is locked)";
end if; end if;
Container.Elements (Index) := New_Item; Container.Elements (Index) := New_Item;
...@@ -1662,11 +1763,11 @@ package body Ada.Containers.Vectors is ...@@ -1662,11 +1763,11 @@ package body Ada.Containers.Vectors is
is is
begin begin
if Position.Container = null then if Position.Container = null then
raise Constraint_Error; raise Constraint_Error with "Position cursor has no element";
end if; end if;
if Position.Container /= Container'Unrestricted_Access then if Position.Container /= Container'Unrestricted_Access then
raise Program_Error; raise Program_Error with "Position cursor denotes wrong container";
end if; end if;
Replace_Element (Container, Position.Index, New_Item); Replace_Element (Container, Position.Index, New_Item);
...@@ -1694,7 +1795,8 @@ package body Ada.Containers.Vectors is ...@@ -1694,7 +1795,8 @@ package body Ada.Containers.Vectors is
elsif N < Container.Elements'Length then elsif N < Container.Elements'Length then
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
declare declare
...@@ -1725,7 +1827,7 @@ package body Ada.Containers.Vectors is ...@@ -1725,7 +1827,7 @@ package body Ada.Containers.Vectors is
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if; end if;
declare declare
...@@ -1733,6 +1835,7 @@ package body Ada.Containers.Vectors is ...@@ -1733,6 +1835,7 @@ package body Ada.Containers.Vectors is
subtype Array_Subtype is subtype Array_Subtype is
Elements_Type (Index_Type'First .. Last); Elements_Type (Index_Type'First .. Last);
begin begin
Container.Elements := new Array_Subtype; Container.Elements := new Array_Subtype;
end; end;
...@@ -1744,7 +1847,8 @@ package body Ada.Containers.Vectors is ...@@ -1744,7 +1847,8 @@ package body Ada.Containers.Vectors is
if Capacity <= N then if Capacity <= N then
if N < Container.Elements'Length then if N < Container.Elements'Length then
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
declare declare
...@@ -1774,7 +1878,8 @@ package body Ada.Containers.Vectors is ...@@ -1774,7 +1878,8 @@ package body Ada.Containers.Vectors is
end if; end if;
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
declare declare
...@@ -1783,7 +1888,7 @@ package body Ada.Containers.Vectors is ...@@ -1783,7 +1888,7 @@ package body Ada.Containers.Vectors is
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
raise Constraint_Error; raise Constraint_Error with "new length is out of range";
end if; end if;
declare declare
...@@ -1832,15 +1937,17 @@ package body Ada.Containers.Vectors is ...@@ -1832,15 +1937,17 @@ package body Ada.Containers.Vectors is
end if; end if;
if Container.Lock > 0 then if Container.Lock > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with cursors (vector is locked)";
end if; end if;
declare declare
I : Index_Type := Index_Type'First; I, J : Index_Type;
J : Index_Type := Container.Last; E : Elements_Type renames Container.Elements.all;
E : Elements_Type renames Container.Elements.all;
begin begin
I := Index_Type'First;
J := Container.Last;
while I < J loop while I < J loop
declare declare
EI : constant Element_Type := E (I); EI : constant Element_Type := E (I);
...@@ -1869,10 +1976,9 @@ package body Ada.Containers.Vectors is ...@@ -1869,10 +1976,9 @@ package body Ada.Containers.Vectors is
begin begin
if Position.Container /= null if Position.Container /= null
and then Position.Container /= and then Position.Container /= Container'Unchecked_Access
Vector_Access'(Container'Unchecked_Access)
then then
raise Program_Error; raise Program_Error with "Position cursor denotes wrong container";
end if; end if;
if Position.Container = null if Position.Container = null
...@@ -1957,7 +2063,8 @@ package body Ada.Containers.Vectors is ...@@ -1957,7 +2063,8 @@ package body Ada.Containers.Vectors is
end if; end if;
if Container.Busy > 0 then if Container.Busy > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with elements (vector is busy)";
end if; end if;
if Length > Capacity (Container) then if Length > Capacity (Container) then
...@@ -1978,10 +2085,12 @@ package body Ada.Containers.Vectors is ...@@ -1978,10 +2085,12 @@ package body Ada.Containers.Vectors is
procedure Swap (Container : in out Vector; I, J : Index_Type) is procedure Swap (Container : in out Vector; I, J : Index_Type) is
begin begin
if I > Container.Last if I > Container.Last then
or else J > Container.Last raise Constraint_Error with "I index is out of range";
then end if;
raise Constraint_Error;
if J > Container.Last then
raise Constraint_Error with "J index is out of range";
end if; end if;
if I = J then if I = J then
...@@ -1989,7 +2098,8 @@ package body Ada.Containers.Vectors is ...@@ -1989,7 +2098,8 @@ package body Ada.Containers.Vectors is
end if; end if;
if Container.Lock > 0 then if Container.Lock > 0 then
raise Program_Error; raise Program_Error with
"attempt to tamper with cursors (vector is locked)";
end if; end if;
declare declare
...@@ -2006,16 +2116,20 @@ package body Ada.Containers.Vectors is ...@@ -2006,16 +2116,20 @@ package body Ada.Containers.Vectors is
procedure Swap (Container : in out Vector; I, J : Cursor) is procedure Swap (Container : in out Vector; I, J : Cursor) is
begin begin
if I.Container = null if I.Container = null then
or else J.Container = null raise Constraint_Error with "I cursor has no element";
then
raise Constraint_Error;
end if; end if;
if I.Container /= Container'Unrestricted_Access if J.Container = null then
or else J.Container /= Container'Unrestricted_Access raise Constraint_Error with "J cursor has no element";
then end if;
raise Program_Error;
if I.Container /= Container'Unrestricted_Access then
raise Program_Error with "I cursor denotes wrong container";
end if;
if J.Container /= Container'Unrestricted_Access then
raise Program_Error with "J cursor denotes wrong container";
end if; end if;
Swap (Container, I.Index, J.Index); Swap (Container, I.Index, J.Index);
...@@ -2072,13 +2186,13 @@ package body Ada.Containers.Vectors is ...@@ -2072,13 +2186,13 @@ package body Ada.Containers.Vectors is
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
raise Constraint_Error; raise Constraint_Error with "Length is out of range";
end if; end if;
Last := Index_Type (Last_As_Int); Last := Index_Type (Last_As_Int);
Elements := new Elements_Type (Index_Type'First .. Last); Elements := new Elements_Type (Index_Type'First .. Last);
return (Controlled with Elements, Last, 0, 0); return Vector'(Controlled with Elements, Last, 0, 0);
end; end;
end To_Vector; end To_Vector;
...@@ -2099,13 +2213,13 @@ package body Ada.Containers.Vectors is ...@@ -2099,13 +2213,13 @@ package body Ada.Containers.Vectors is
begin begin
if Last_As_Int > Index_Type'Pos (Index_Type'Last) then if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
raise Constraint_Error; raise Constraint_Error with "Length is out of range";
end if; end if;
Last := Index_Type (Last_As_Int); Last := Index_Type (Last_As_Int);
Elements := new Elements_Type'(Index_Type'First .. Last => New_Item); Elements := new Elements_Type'(Index_Type'First .. Last => New_Item);
return (Controlled with Elements, Last, 0, 0); return Vector'(Controlled with Elements, Last, 0, 0);
end; end;
end To_Vector; end To_Vector;
...@@ -2123,7 +2237,7 @@ package body Ada.Containers.Vectors is ...@@ -2123,7 +2237,7 @@ package body Ada.Containers.Vectors is
begin begin
if Index > Container.Last then if Index > Container.Last then
raise Constraint_Error; raise Constraint_Error with "Index is out of range";
end if; end if;
B := B + 1; B := B + 1;
...@@ -2149,11 +2263,11 @@ package body Ada.Containers.Vectors is ...@@ -2149,11 +2263,11 @@ package body Ada.Containers.Vectors is
is is
begin begin
if Position.Container = null then if Position.Container = null then
raise Constraint_Error; raise Constraint_Error with "Position cursor has no element";
end if; end if;
if Position.Container /= Container'Unrestricted_Access then if Position.Container /= Container'Unrestricted_Access then
raise Program_Error; raise Program_Error with "Position cursor denotes wrong container";
end if; end if;
Update_Element (Container, Position.Index, Process); Update_Element (Container, Position.Index, Process);
...@@ -2164,7 +2278,7 @@ package body Ada.Containers.Vectors is ...@@ -2164,7 +2278,7 @@ package body Ada.Containers.Vectors is
----------- -----------
procedure Write procedure Write
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Container : Vector) Container : Vector)
is is
begin begin
...@@ -2176,11 +2290,11 @@ package body Ada.Containers.Vectors is ...@@ -2176,11 +2290,11 @@ package body Ada.Containers.Vectors is
end Write; end Write;
procedure Write procedure Write
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Position : Cursor) Position : Cursor)
is is
begin begin
raise Program_Error; raise Program_Error with "attempt to stream vector cursor";
end Write; end Write;
end Ada.Containers.Vectors; end Ada.Containers.Vectors;
...@@ -332,13 +332,13 @@ private ...@@ -332,13 +332,13 @@ private
use Ada.Streams; use Ada.Streams;
procedure Write procedure Write
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Container : Vector); Container : Vector);
for Vector'Write use Write; for Vector'Write use Write;
procedure Read procedure Read
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Container : out Vector); Container : out Vector);
for Vector'Read use Read; for Vector'Read use Read;
...@@ -354,13 +354,13 @@ private ...@@ -354,13 +354,13 @@ private
end record; end record;
procedure Write procedure Write
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Position : Cursor); Position : Cursor);
for Cursor'Write use Write; for Cursor'Write use Write;
procedure Read procedure Read
(Stream : access Root_Stream_Type'Class; (Stream : not null access Root_Stream_Type'Class;
Position : out Cursor); Position : out Cursor);
for Cursor'Read use Read; for Cursor'Read use Read;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment