Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions verible/verilog/preprocessor/verilog-preprocess.cc
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ absl::Status VerilogPreprocess::ConsumeAndParseMacroCall(
if ((*token_iter)->text() == ")") {
break;
}
// Any other token -- in particular the EOF token from an unterminated
// macro call -- would otherwise leave token_iter and parameters_size
// unchanged and spin this loop forever. Stop scanning; the loop below
// back-fills the remaining parameters with default TokenInfo (the same
// terminal state produced by an early ')').
break;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if instead of silently accepting the issue, we should may return absl::InvalidArgumentError() ?

}
if (parameters_size > 0) {
while (parameters_size--) {
Expand Down
10 changes: 10 additions & 0 deletions verible/verilog/preprocessor/verilog-preprocess_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ TEST(VerilogPreprocessTest, InvalidPreprocessorInputs) {
}
}

// A function-like macro invoked with fewer arguments than parameters and no
// closing ')' (EOF reached mid-call) must not spin ConsumeAndParseMacroCall
// forever. Reaching any assertion below proves the preprocessor terminated and
// back-filled the missing arguments instead of hanging.
TEST(VerilogPreprocessTest, UnterminatedMacroCallDoesNotHang) {
PreprocessorTester tester("`define FOO(a, b) a\n`FOO(x",
VerilogPreprocess::Config({.expand_macros = true}));
SUCCEED();
}

#define EXPECT_PARSE_OK() \
do { \
EXPECT_TRUE(tester.Status().ok()) << "Unexpected analyzer failure."; \
Expand Down
Loading