diff options
author | Tristan Gingold <tgingold@free.fr> | 2021-05-15 08:30:20 +0200 |
---|---|---|
committer | Tristan Gingold <tgingold@free.fr> | 2021-05-15 08:30:20 +0200 |
commit | c2b3f93dfa40ee92de53b54e38e1819cfc33cb04 (patch) | |
tree | 3bf0031d114d606de8f78caae199629aedd8c307 /src | |
parent | a9f0afa140aca437d927f0329bd86f6d628659e4 (diff) | |
download | ghdl-c2b3f93dfa40ee92de53b54e38e1819cfc33cb04.tar.gz ghdl-c2b3f93dfa40ee92de53b54e38e1819cfc33cb04.tar.bz2 ghdl-c2b3f93dfa40ee92de53b54e38e1819cfc33cb04.zip |
grt-table.adb: avoid overflow for computing memory size. For #1761
Diffstat (limited to 'src')
-rw-r--r-- | src/grt/grt-table.adb | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/src/grt/grt-table.adb b/src/grt/grt-table.adb index 89fc043ad..a800b8c25 100644 --- a/src/grt/grt-table.adb +++ b/src/grt/grt-table.adb @@ -39,7 +39,8 @@ package body Grt.Table is pragma Import (C, Free); -- Resize and reallocate the table according to LAST_VAL. - procedure Resize is + procedure Resize + is function Realloc (T : Table_Ptr; Size : size_t) return Table_Ptr; pragma Import (C, Realloc); @@ -49,8 +50,10 @@ package body Grt.Table is Max := Max + (Max - Table_Low_Bound + 1); end loop; - New_Size := size_t ((Max - Table_Low_Bound + 1) * - (Table_Type'Component_Size / Storage_Unit)); + -- Do the multiplication using size_t to avoid overflow if the bounds + -- are a 32bit type on a 64bit machine. + New_Size := (size_t (Max - Table_Low_Bound + 1) + * size_t (Table_Type'Component_Size / Storage_Unit)); Table := Realloc (Table, New_Size); @@ -113,6 +116,6 @@ begin Last_Val := Table_Index_Type'Pred (Table_Low_Bound); Max := Table_Low_Bound + Table_Index_Type (Table_Initial) - 1; - Table := Malloc (size_t (Table_Initial * - (Table_Type'Component_Size / Storage_Unit))); + Table := Malloc (size_t (Table_Initial) + * size_t (Table_Type'Component_Size / Storage_Unit)); end Grt.Table; |