Skip to content

Commit

Permalink
Array bounds check incorrectly optimised out. Fixes #747
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Jul 31, 2023
1 parent ea457d5 commit f640183
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/bounds.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,17 @@ static void bounds_check_array_ref(tree_t t)
type_t index_type = index_type_of(value_type, i);
const range_kind_t dir = tree_subkind(r);

int64_t ivalue;
checked = folded_int(pvalue, &ivalue);
int64_t ivalue, low, high;
if (folded_int(pvalue, &ivalue) && folded_bounds(r, &low, &high)) {
checked = true;

int64_t low, high;
if (folded_bounds(r, &low, &high)) {
int64_t folded;
if (!index_in_range(pvalue, low, high, &folded)) {
if (ivalue < low || ivalue > high) {
LOCAL_TEXT_BUF tb = tb_new();
tb_cat(tb, "array");
if (tree_kind(value) == T_REF)
tb_printf(tb, " %s", istr(tree_ident(value)));
tb_cat(tb, " index ");
to_string(tb, index_type, folded);
to_string(tb, index_type, ivalue);
tb_printf(tb, " outside of %s range ", type_pp(index_type));
bounds_fmt_type_range(tb, index_type, dir, low, high);

Expand Down
1 change: 1 addition & 0 deletions test/regress/gold/issue747.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(init): index 10 outside of NATURAL range 9 downto 0
21 changes: 21 additions & 0 deletions test/regress/issue747.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
entity issue747 is
generic (
L : natural := 1
);
end entity;

architecture test of issue747 is
signal s : bit_vector(L-1 downto 0);
begin

process
begin
wait for 1 ns;
s(10) <= '0'; -- no check done for out of bound
s(12 downto 11) <= "11"; -- check is done here
wait for 1 ns;
report "SIMULATION ENDED" severity failure;
wait;
end process;

end architecture;
1 change: 1 addition & 0 deletions test/regress/testlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -834,3 +834,4 @@ reflect5 normal,2019
seqblock1 normal,2019
seqblock2 normal,2019
stdenv6 normal,2019
issue747 fail,gold,gL=10

0 comments on commit f640183

Please sign in to comment.