From 07b6da32b4c47519cfe6f02146a3fc099fbcdcbe Mon Sep 17 00:00:00 2001
From: Tristan Gingold <tgingold@free.fr>
Date: Mon, 26 Dec 2022 17:33:14 +0100
Subject: synth: check bounds for pos and val attributes

---
 src/synth/synth-vhdl_expr.adb | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/synth/synth-vhdl_expr.adb b/src/synth/synth-vhdl_expr.adb
index af66fb874..6e397aa1a 100644
--- a/src/synth/synth-vhdl_expr.adb
+++ b/src/synth/synth-vhdl_expr.adb
@@ -2317,6 +2317,7 @@ package body Synth.Vhdl_Expr is
             declare
                Param : constant Node := Get_Parameter (Expr);
                V : Valtyp;
+               Vi : Int64;
                Dtype : Type_Acc;
             begin
                Dtype := Get_Subtype_Object (Syn_Inst, Get_Type (Expr));
@@ -2324,10 +2325,16 @@ package body Synth.Vhdl_Expr is
                --  FIXME: to be generalized.  Not always as simple as a
                --  subtype conversion.
                if Is_Static (V.Val) then
-                  V := Create_Value_Discrete (Read_Discrete (V), Dtype);
+                  Vi := Read_Discrete (V);
+                  if not In_Range (Dtype.Drange, Vi) then
+                     Error_Msg_Synth (Syn_Inst, Expr, "value out of range");
+                     return No_Valtyp;
+                  end if;
+                  return Create_Value_Discrete (Vi, Dtype);
+               else
+                  return Synth_Subtype_Conversion
+                    (Syn_Inst, V, Dtype, False, Expr);
                end if;
-               return Synth_Subtype_Conversion
-                 (Syn_Inst, V, Dtype, False, Expr);
             end;
          when Iir_Kind_Low_Type_Attribute =>
             return Synth_Low_High_Type_Attribute (Syn_Inst, Expr, Dir_To);
-- 
cgit v1.2.3