This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Dangling cursor checks in Element function


In Ada.Containers.Ordered_Maps, if a dangling cursor is passed to the Element
function, execution is erroneous. Therefore, the compiler is not obligated to
detect this error. However, this patch inserts code that will detect this error
in some cases, and raise Program_Error. The same applies to Ordered_Sets,
Ordered_Multisets, Indefinite_Ordered_Maps, Indefinite_Ordered_Sets, and
Indefinite_Ordered_Multisets. No test available for erroneous execution.

Tested on x86_64-pc-linux-gnu, committed on trunk

2018-06-11  Bob Duff  <duff@adacore.com>

gcc/ada/

	* libgnat/a-ciorma.adb, libgnat/a-ciormu.adb, libgnat/a-ciorse.adb,
	libgnat/a-coorma.adb, libgnat/a-coormu.adb, libgnat/a-coorse.adb:
	(Element): Add code to detect dangling cursors in some cases.
--- gcc/ada/libgnat/a-ciorma.adb
+++ gcc/ada/libgnat/a-ciorma.adb
@@ -541,6 +541,13 @@ package body Ada.Containers.Indefinite_Ordered_Maps is
            "Position cursor of function Element is bad";
       end if;
 
+      if Checks and then
+        (Left (Position.Node) = Position.Node
+           or else Right (Position.Node) = Position.Node)
+      then
+         raise Program_Error with "dangling cursor";
+      end if;
+
       pragma Assert (Vet (Position.Container.Tree, Position.Node),
                      "Position cursor of function Element is bad");
 

--- gcc/ada/libgnat/a-ciormu.adb
+++ gcc/ada/libgnat/a-ciormu.adb
@@ -545,6 +545,13 @@ package body Ada.Containers.Indefinite_Ordered_Multisets is
          raise Program_Error with "Position cursor is bad";
       end if;
 
+      if Checks and then
+        (Left (Position.Node) = Position.Node
+           or else Right (Position.Node) = Position.Node)
+      then
+         raise Program_Error with "dangling cursor";
+      end if;
+
       pragma Assert (Vet (Position.Container.Tree, Position.Node),
                      "bad cursor in Element");
 

--- gcc/ada/libgnat/a-ciorse.adb
+++ gcc/ada/libgnat/a-ciorse.adb
@@ -534,6 +534,13 @@ package body Ada.Containers.Indefinite_Ordered_Sets is
          raise Program_Error with "Position cursor is bad";
       end if;
 
+      if Checks and then
+        (Left (Position.Node) = Position.Node
+           or else Right (Position.Node) = Position.Node)
+      then
+         raise Program_Error with "dangling cursor";
+      end if;
+
       pragma Assert (Vet (Position.Container.Tree, Position.Node),
                      "bad cursor in Element");
 

--- gcc/ada/libgnat/a-coorma.adb
+++ gcc/ada/libgnat/a-coorma.adb
@@ -481,6 +481,13 @@ package body Ada.Containers.Ordered_Maps is
            "Position cursor of function Element equals No_Element";
       end if;
 
+      if Checks and then
+        (Left (Position.Node) = Position.Node
+           or else Right (Position.Node) = Position.Node)
+      then
+         raise Program_Error with "dangling cursor";
+      end if;
+
       pragma Assert (Vet (Position.Container.Tree, Position.Node),
                      "Position cursor of function Element is bad");
 

--- gcc/ada/libgnat/a-coormu.adb
+++ gcc/ada/libgnat/a-coormu.adb
@@ -502,6 +502,13 @@ package body Ada.Containers.Ordered_Multisets is
          raise Constraint_Error with "Position cursor equals No_Element";
       end if;
 
+      if Checks and then
+        (Left (Position.Node) = Position.Node
+           or else Right (Position.Node) = Position.Node)
+      then
+         raise Program_Error with "dangling cursor";
+      end if;
+
       pragma Assert (Vet (Position.Container.Tree, Position.Node),
                      "bad cursor in Element");
 

--- gcc/ada/libgnat/a-coorse.adb
+++ gcc/ada/libgnat/a-coorse.adb
@@ -480,6 +480,13 @@ package body Ada.Containers.Ordered_Sets is
          raise Constraint_Error with "Position cursor equals No_Element";
       end if;
 
+      if Checks and then
+        (Left (Position.Node) = Position.Node
+           or else Right (Position.Node) = Position.Node)
+      then
+         raise Program_Error with "dangling cursor";
+      end if;
+
       pragma Assert (Vet (Position.Container.Tree, Position.Node),
                      "bad cursor in Element");
 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]