From 52925ce9ff499a6cc180b9079a8ed44fb1c6b12c Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Thu, 6 Jan 2022 11:34:20 +0100 Subject: [PATCH] Handle invalid packed dimensions Trying to elaborate a type with invalid packed dimensions currently results in a crash. E.g. `typedef logic [] T;` The issue is in the `elaborate_array_ranges()` function which does not verify that the range specification is valid. Replace the `elaborate_array_ranges()` with `evaluate_ranges()`, which does the same thing, but properly checks the range specification and reports an error if it is invalid. Signed-off-by: Lars-Peter Clausen --- elab_type.cc | 44 ++++---------------------------------------- 1 file changed, 4 insertions(+), 40 deletions(-) diff --git a/elab_type.cc b/elab_type.cc index 49d3483df..4641b1507 100644 --- a/elab_type.cc +++ b/elab_type.cc @@ -34,44 +34,6 @@ using namespace std; -/* - * Some types have a list of ranges that need to be elaborated. This - * function elaborates the ranges referenced by "dims" into the vector - * "ranges". - */ -static void elaborate_array_ranges(Design*des, NetScope*scope, - vector&ranges, - const list*dims) -{ - if (dims == 0) - return; - - for (list::const_iterator cur = dims->begin() - ; cur != dims->end() ; ++ cur) { - - NetExpr*me = elab_and_eval(des, scope, cur->first, 0, true); - - NetExpr*le = elab_and_eval(des, scope, cur->second, 0, true); - - /* If elaboration failed for either expression, we - should have already reported the error, so just - skip the following evaluation to recover. */ - - long mnum = 0, lnum = 0; - if ( me && ! eval_as_long(mnum, me) ) { - assert(0); - des->errors += 1; - } - - if ( le && ! eval_as_long(lnum, le) ) { - assert(0); - des->errors += 1; - } - - ranges.push_back(netrange_t(mnum, lnum)); - } -} - /* * Elaborations of types may vary depending on the scope that it is * done in, so keep a per-scope cache of the results. @@ -164,7 +126,8 @@ ivl_type_s* enum_type_t::elaborate_type_raw(Design*, NetScope*scope) const ivl_type_s* vector_type_t::elaborate_type_raw(Design*des, NetScope*scope) const { vector packed; - elaborate_array_ranges(des, scope, packed, pdims.get()); + if (pdims.get()) + evaluate_ranges(des, scope, this, packed, *pdims); netvector_t*tmp = new netvector_t(packed, base_type); tmp->set_signed(signed_flag); @@ -192,7 +155,8 @@ ivl_type_s* string_type_t::elaborate_type_raw(Design*, NetScope*) const ivl_type_s* parray_type_t::elaborate_type_raw(Design*des, NetScope*scope) const { vectorpacked; - elaborate_array_ranges(des, scope, packed, dims.get()); + if (dims.get()) + evaluate_ranges(des, scope, this, packed, *dims); ivl_type_t etype = base_type->elaborate_type(des, scope);