From e4864771edba19ed2d86c516af76f149a396ac84 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Wed, 17 Feb 2016 20:41:27 +0000 Subject: [PATCH] Earlier bounds checking during elaboration. Fixes #251 --- src/elab.c | 11 ++++++++++- test/elab/issue251.vhd | 34 ++++++++++++++++++++++++++++++++++ test/test_elab.c | 16 ++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 test/elab/issue251.vhd diff --git a/src/elab.c b/src/elab.c index bd93b424..0b1cf4e0 100644 --- a/src/elab.c +++ b/src/elab.c @@ -603,7 +603,15 @@ static netid_t elab_get_net(tree_t expr, int n) { switch (tree_kind(expr)) { case T_REF: - return tree_net(tree_ref(expr), n); + { + tree_t decl = tree_ref(expr); + if (n < 0 || n >= tree_nets(decl)) { + assert(bounds_errors() > 0); // Should have already caught this + return NETID_INVALID; + } + else + return tree_net(tree_ref(expr), n); + } case T_ARRAY_REF: { @@ -1423,6 +1431,7 @@ static void elab_entity_arch(tree_t t, tree_t arch, const elab_ctx_t *ctx) elab_funcs(arch, t, ctx); simplify(arch); + bounds_check(arch); elab_ctx_t new_ctx = { .out = ctx->out, diff --git a/test/elab/issue251.vhd b/test/elab/issue251.vhd new file mode 100644 index 00000000..e23e4b66 --- /dev/null +++ b/test/elab/issue251.vhd @@ -0,0 +1,34 @@ +entity sub is + port ( + x : in bit_vector(3 downto 0); + y : out bit ); +end entity; + +architecture test of sub is +begin + y <= x(0) xor x(1) xor x(2) xor x(3); +end architecture; + +------------------------------------------------------------------------------- + +entity issue251 is +end entity; + +architecture test of issue251 is + signal a : bit_vector(3 downto 0); + signal b : bit_vector(1 downto 0); +begin + + sub1_i: entity work.sub + port map ( + x(-1) => a(0), -- Error + x(3 downto 0) => a, + y => open ); + + sub2_i: entity work.sub + port map ( + x(3) => a(-1), -- Error + x(2 downto 0) => a, + y => open ); + +end architecture; diff --git a/test/test_elab.c b/test/test_elab.c index 3539d0ca..731a7574 100644 --- a/test/test_elab.c +++ b/test/test_elab.c @@ -400,6 +400,21 @@ START_TEST(test_libbind3) } END_TEST +START_TEST(test_issue251) +{ + input_from_file(TESTDIR "/elab/issue251.vhd"); + + const error_t expect[] = { + { 24, "array X index -1 out of bounds 3 downto 0" }, + { 30, "array A index -1 out of bounds 3 downto 0" }, + { -1, NULL } + }; + expect_errors(expect); + + fail_unless(run_elab() == NULL); +} +END_TEST + int main(void) { Suite *s = suite_create("elab"); @@ -430,6 +445,7 @@ int main(void) tcase_add_test(tc, test_libbind2); tcase_add_test(tc, test_toplevel2); tcase_add_test(tc, test_libbind3); + tcase_add_test(tc, test_issue251); suite_add_tcase(s, tc); return nvc_run_test(s); -- 2.39.2