diff options
author | Arnaud Charlet <charlet@adacore.com> | 2020-01-14 08:21:20 -0500 |
---|---|---|
committer | Pierre-Marie de Rodat <derodat@adacore.com> | 2020-06-03 06:01:46 -0400 |
commit | 3f06f6c30a8c168388886c13785597e56b398981 (patch) | |
tree | a6466efc45cd0a536648ba9ed2cf29180e8af451 | |
parent | e4ef65f9049ff5cf04635917e7cc845a8c4446ec (diff) | |
download | gcc-3f06f6c30a8c168388886c13785597e56b398981.zip gcc-3f06f6c30a8c168388886c13785597e56b398981.tar.gz gcc-3f06f6c30a8c168388886c13785597e56b398981.tar.bz2 |
[Ada] Add detection of uninitialized big reals
2020-06-03 Arnaud Charlet <charlet@adacore.com>
gcc/ada/
* libgnat/a-nbnbin.ads: Minor reformatting.
* libgnat/a-nbnbre.ads, libgnat/a-nbnbre.adb (Is_Valid): Add
convention Intrinsic. Add detection of uninitialized big reals.
-rw-r--r-- | gcc/ada/libgnat/a-nbnbin.ads | 4 | ||||
-rw-r--r-- | gcc/ada/libgnat/a-nbnbre.adb | 40 | ||||
-rw-r--r-- | gcc/ada/libgnat/a-nbnbre.ads | 3 |
3 files changed, 39 insertions, 8 deletions
diff --git a/gcc/ada/libgnat/a-nbnbin.ads b/gcc/ada/libgnat/a-nbnbin.ads index a54b09f..4c1a22c 100644 --- a/gcc/ada/libgnat/a-nbnbin.ads +++ b/gcc/ada/libgnat/a-nbnbin.ads @@ -13,9 +13,9 @@ -- -- ------------------------------------------------------------------------------ -with Ada.Finalization; with Ada.Streams; +private with Ada.Finalization; private with System; -- Note that some Ada 2020 aspects are commented out since they are not @@ -89,7 +89,7 @@ is end Unsigned_Conversions; - function To_String (Arg : Big_Integer; + function To_String (Arg : Big_Integer; Width : Field := 0; Base : Number_Base := 10) return String with Post => To_String'Result'First = 1; diff --git a/gcc/ada/libgnat/a-nbnbre.adb b/gcc/ada/libgnat/a-nbnbre.adb index a2b40f7..07a9442 100644 --- a/gcc/ada/libgnat/a-nbnbre.adb +++ b/gcc/ada/libgnat/a-nbnbre.adb @@ -46,7 +46,7 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is -------------- function Is_Valid (Arg : Big_Real) return Boolean is - (Is_Valid (Arg.Num) and then Is_Valid (Arg.Den)); + (Is_Valid (Arg.Num) and Is_Valid (Arg.Den)); --------- -- "/" -- @@ -69,13 +69,17 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is -- Numerator -- --------------- - function Numerator (Arg : Big_Real) return Big_Integer is (Arg.Num); + function Numerator (Arg : Big_Real) return Big_Integer is + (if Is_Valid (Arg.Num) then Arg.Num + else raise Constraint_Error with "invalid big real"); ----------------- -- Denominator -- ----------------- - function Denominator (Arg : Big_Real) return Big_Positive is (Arg.Den); + function Denominator (Arg : Big_Real) return Big_Positive is + (if Is_Valid (Arg.Den) then Arg.Den + else raise Constraint_Error with "invalid big real"); --------- -- "=" -- @@ -409,6 +413,10 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is function "+" (L : Big_Real) return Big_Real is Result : Big_Real; begin + if not Is_Valid (L) then + raise Constraint_Error with "invalid big real"; + end if; + Result.Num := L.Num; Result.Den := L.Den; return Result; @@ -419,14 +427,16 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is --------- function "-" (L : Big_Real) return Big_Real is - (Num => -L.Num, Den => L.Den); + (if Is_Valid (L) then (Num => -L.Num, Den => L.Den) + else raise Constraint_Error with "invalid big real"); ----------- -- "abs" -- ----------- function "abs" (L : Big_Real) return Big_Real is - (Num => abs L.Num, Den => L.Den); + (if Is_Valid (L) then (Num => abs L.Num, Den => L.Den) + else raise Constraint_Error with "invalid big real"); --------- -- "+" -- @@ -435,6 +445,10 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is function "+" (L, R : Big_Real) return Big_Real is Result : Big_Real; begin + if not Is_Valid (L) or not Is_Valid (R) then + raise Constraint_Error with "invalid big real"; + end if; + Result.Num := L.Num * R.Den + R.Num * L.Den; Result.Den := L.Den * R.Den; Normalize (Result); @@ -448,6 +462,10 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is function "-" (L, R : Big_Real) return Big_Real is Result : Big_Real; begin + if not Is_Valid (L) or not Is_Valid (R) then + raise Constraint_Error with "invalid big real"; + end if; + Result.Num := L.Num * R.Den - R.Num * L.Den; Result.Den := L.Den * R.Den; Normalize (Result); @@ -461,6 +479,10 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is function "*" (L, R : Big_Real) return Big_Real is Result : Big_Real; begin + if not Is_Valid (L) or not Is_Valid (R) then + raise Constraint_Error with "invalid big real"; + end if; + Result.Num := L.Num * R.Num; Result.Den := L.Den * R.Den; Normalize (Result); @@ -474,6 +496,10 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is function "/" (L, R : Big_Real) return Big_Real is Result : Big_Real; begin + if not Is_Valid (L) or not Is_Valid (R) then + raise Constraint_Error with "invalid big real"; + end if; + Result.Num := L.Num * R.Den; Result.Den := L.Den * R.Num; Normalize (Result); @@ -487,6 +513,10 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is function "**" (L : Big_Real; R : Integer) return Big_Real is Result : Big_Real; begin + if not Is_Valid (L) then + raise Constraint_Error with "invalid big real"; + end if; + if R = 0 then Result.Num := To_Big_Integer (1); Result.Den := To_Big_Integer (1); diff --git a/gcc/ada/libgnat/a-nbnbre.ads b/gcc/ada/libgnat/a-nbnbre.ads index 4827caa..3ea93f6 100644 --- a/gcc/ada/libgnat/a-nbnbre.ads +++ b/gcc/ada/libgnat/a-nbnbre.ads @@ -27,7 +27,8 @@ is -- with Real_Literal => From_String, -- Put_Image => Put_Image; - function Is_Valid (Arg : Big_Real) return Boolean; + function Is_Valid (Arg : Big_Real) return Boolean + with Convention => Intrinsic; function "/" (Num, Den : Big_Integers.Big_Integer) return Big_Real; -- with Pre => (if Big_Integers."=" (Den, Big_Integers.To_Big_Integer (0)) |