]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Prevent overflow in computation of aggregate size
authorPiotr Trojanek <trojanek@adacore.com>
Thu, 31 Mar 2022 18:56:58 +0000 (20:56 +0200)
committerPierre-Marie de Rodat <derodat@adacore.com>
Wed, 18 May 2022 08:41:01 +0000 (08:41 +0000)
When computing size of a static aggregate to decide if it should be
transformed into assignments and loops we could have an overflow check.
This is mostly harmless, because colossal aggregates will likely crash
the application anyway, no matter how we transform them.

This was not detected because compiler was built with -gnatg switch that
suppresses overflow checks (they are only enabled by an explicit -gnato
switch).

gcc/ada/

* exp_aggr.adb (Component_Count): Calculate size as an Uint and
only then check if it is in the range of Int, as otherwise the
multiplication of Int values can overflow.

gcc/ada/exp_aggr.adb

index 72f65555681ea711e5e6d23a352bace4e2b291b1..4714cab8bfae1c4cad3f353f95ae09243c0a0c65 100644 (file)
@@ -661,10 +661,10 @@ package body Exp_Aggr is
 
                   declare
                      UI : constant Uint :=
-                            Expr_Value (Hi) - Expr_Value (Lo) + 1;
+                            (Expr_Value (Hi) - Expr_Value (Lo) + 1) * Siz;
                   begin
                      if UI_Is_In_Int_Range (UI) then
-                        return Siz * UI_To_Int (UI);
+                        return UI_To_Int (UI);
                      else
                         return Int'Last;
                      end if;