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 <seanga2@gmail.com>
This commit is contained in:
Sean Anderson 2022-10-28 17:00:44 -04:00
parent c2dbf4e483
commit b18d90a2d9
4 changed files with 16 additions and 2 deletions

View File

@ -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;

View File

@ -0,0 +1,8 @@
`ifndef FOO
`define FOO
`define BAR(x)
`endif
module macro_args();
macro_args_sub sub();
endmodule

View File

@ -0,0 +1,3 @@
module macro_args_sub();
`BAR(0)
endmodule

View File

@ -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