From b18d90a2d9abb89378b3ad33803624e7fa14dc9b Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Fri, 28 Oct 2022 17:00:44 -0400 Subject: [PATCH] ivlpp: Fix segfault in macro_start_args macro_start_args truncates def_buf, but does not check to ensure that someone has allocated def_buf first. This will cause a null pointer dereference if the first access to def_buf while parsing a file is a macro invocation. Fix this by avoiding truncating def_buf if it is NULL, as it is effectively already truncated. Fixes: 680196953 ("Add support for text macros with arguments.") Signed-off-by: Sean Anderson --- ivlpp/lexor.lex | 6 ++++-- ivtest/ivltests/macro_args.v | 8 ++++++++ ivtest/ivltests/macro_args_sub.v | 3 +++ ivtest/regress-vlg.list | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 ivtest/ivltests/macro_args.v create mode 100644 ivtest/ivltests/macro_args_sub.v diff --git a/ivlpp/lexor.lex b/ivlpp/lexor.lex index 5fbfd4a19..bc34a1fac 100644 --- a/ivlpp/lexor.lex +++ b/ivlpp/lexor.lex @@ -1485,8 +1485,10 @@ static void macro_start_args(void) * entry for arg 0. This will be used by macro_finish_arg() to * calculate the buffer location for arg 1. */ - def_buf_free = def_buf_size - 1; - def_buf[0] = 0; + if (def_buf) { + def_buf_free = def_buf_size - 1; + def_buf[0] = 0; + } def_argo[0] = 0; def_argl[0] = 0; def_argc = 1; diff --git a/ivtest/ivltests/macro_args.v b/ivtest/ivltests/macro_args.v new file mode 100644 index 000000000..c23e832c0 --- /dev/null +++ b/ivtest/ivltests/macro_args.v @@ -0,0 +1,8 @@ +`ifndef FOO +`define FOO +`define BAR(x) +`endif + +module macro_args(); + macro_args_sub sub(); +endmodule diff --git a/ivtest/ivltests/macro_args_sub.v b/ivtest/ivltests/macro_args_sub.v new file mode 100644 index 000000000..38241285e --- /dev/null +++ b/ivtest/ivltests/macro_args_sub.v @@ -0,0 +1,3 @@ +module macro_args_sub(); + `BAR(0) +endmodule diff --git a/ivtest/regress-vlg.list b/ivtest/regress-vlg.list index 2b8274d21..df929e4c1 100644 --- a/ivtest/regress-vlg.list +++ b/ivtest/regress-vlg.list @@ -628,6 +628,7 @@ lh_varindx5 normal ivltests localparam_type normal ivltests gold=parameter_type.gold long_div normal ivltests gold=long_div.gold macro2 normal ivltests +macro_args CO,-yivltests ivltests macro_redefinition normal,-Wmacro-redefinition ivltests gold=macro_redefinition.gold macro_replacement normal,-Wmacro-replacement ivltests gold=macro_replacement.gold macsub normal ivltests