diff mbox

[Ada] Crash on illegal discrete range

Message ID 20140731101148.GA10829@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet July 31, 2014, 10:11 a.m. UTC
With this patch the compiler rejects properly a discrete range in a loop
specification that uses 'length by mistake.

Compiler lab4.adb must yield:

   lab4.adb:13:23: expect attribute "range"

---
package body lab4 is
   procedure createArray (myArr : in out dynArray; maxIndex : Integer) is
      tempArr : dynArray (1..maxIndex);
      cntArr : integer :=0;
   begin
      for i in tempArr'length loop
         tempArr(cntArr):= cntArr;
         cntArr := cntArr+1;
      end loop;
   end createArray;
end lab4;
---
package lab4 is
   Type Node;
   Type Tree is access Node;

   Type Node is record
      Val : Integer;
      Left, Right : Tree;
   end record;

   type dynArray is array (positive range <>) of Integer; -- typeset for array

   procedure createArray (myArr : in out dynArray; maxIndex : Integer);
end lab4;

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

2014-07-31  Ed Schonberg  <schonberg@adacore.com>

	* sem_ch3.adb (Make_Index): Reject properly the use of 'Length
	in a discrete range, when 'range was probably intended.
diff mbox

Patch

Index: sem_ch3.adb
===================================================================
--- sem_ch3.adb	(revision 213338)
+++ sem_ch3.adb	(working copy)
@@ -17285,10 +17285,16 @@ 
 
       elsif Nkind (N) = N_Attribute_Reference then
 
-         --  The parser guarantees that the attribute is a RANGE attribute
+         --  Catch beginner's error (use of attribute other than 'Range)
 
+         if Attribute_Name (N) /= Name_Range then
+            Error_Msg_N ("expect attribute ''Range", N);
+            Set_Etype (N, Any_Type);
+            return;
+         end if;
+
          --  If the node denotes the range of a type mark, that is also the
-         --  resulting type, and we do no need to create an Itype for it.
+         --  resulting type, and we do not need to create an Itype for it.
 
          if Is_Entity_Name (Prefix (N))
            and then Comes_From_Source (N)