Skip to content

Commit 3aafa30

Browse files
Fix parser expressions (#2682)
* Fix segmentation faults in amrex::Parser if a constant was not defined. Without this fix, the following segments of code all seg-fault. With this fix, each of them will throw an exception instead. (If the setConstant lines are uncommented, the tests will pass with or without this fix.) { amrex::Print() << "Attempt 2+a" << std::endl; amrex::Parser parser("2+a"); //parser.setConstant("a", (amrex::Real)3.0); const auto parserExec = parser.compile<0>(); AMREX_ALWAYS_ASSERT(parserExec() == 5); } { amrex::Print() << "Attempt 2-a" << std::endl; amrex::Parser parser("2-a"); //parser.setConstant("a", (amrex::Real)3.0); const auto parserExec = parser.compile<0>(); AMREX_ALWAYS_ASSERT(parserExec() == -1); } { amrex::Print() << "Attempt 2*a" << std::endl; amrex::Parser parser("2*a"); //parser.setConstant("a", (amrex::Real)3.0); const auto parserExec = parser.compile<0>(); AMREX_ALWAYS_ASSERT(parserExec() == 6); } { amrex::Print() << "Attempt 3/a" << std::endl; amrex::Parser parser("3/a"); //parser.setConstant("a", (amrex::Real)3.0); const auto parserExec = parser.compile<0>(); AMREX_ALWAYS_ASSERT(parserExec() == 1); } { amrex::Print() << "Attempt -a" << std::endl; amrex::Parser parser("-a"); //parser.setConstant("a", (amrex::Real)3.0); const auto parserExec = parser.compile<0>(); AMREX_ALWAYS_ASSERT(parserExec() == -3); }
1 parent 8b691cb commit 3aafa30

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Src/Base/Parser/AMReX_Parser_Exe.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@ parser_compile_exe_size (struct parser_node* node, char*& p, std::size_t& exe_si
546546
t->i = AMREX_PARSER_LOCAL_IDX0 + lidx;
547547
} else {
548548
t->i = node->rip;
549+
if (t->i < 0) {
550+
throw std::runtime_error(std::string("Unknown variable ")
551+
+ ((struct parser_symbol*)node->r)->name);
552+
}
549553
}
550554
t->v = node->lvp.v;
551555
}
@@ -564,6 +568,10 @@ parser_compile_exe_size (struct parser_node* node, char*& p, std::size_t& exe_si
564568
t->i = AMREX_PARSER_LOCAL_IDX0 + lidx;
565569
} else {
566570
t->i = node->rip;
571+
if (t->i < 0) {
572+
throw std::runtime_error(std::string("Unknown variable ")
573+
+ ((struct parser_symbol*)node->r)->name);
574+
}
567575
}
568576
t->v = node->lvp.v;
569577
}
@@ -582,6 +590,10 @@ parser_compile_exe_size (struct parser_node* node, char*& p, std::size_t& exe_si
582590
t->i = AMREX_PARSER_LOCAL_IDX0 + lidx;
583591
} else {
584592
t->i = node->rip;
593+
if (t->i < 0) {
594+
throw std::runtime_error(std::string("Unknown variable ")
595+
+ ((struct parser_symbol*)node->r)->name);
596+
}
585597
}
586598
t->v = node->lvp.v;
587599
}
@@ -600,6 +612,10 @@ parser_compile_exe_size (struct parser_node* node, char*& p, std::size_t& exe_si
600612
t->i = AMREX_PARSER_LOCAL_IDX0 + lidx;
601613
} else {
602614
t->i = node->rip;
615+
if (t->i < 0) {
616+
throw std::runtime_error(std::string("Unknown variable ")
617+
+ ((struct parser_symbol*)node->r)->name);
618+
}
603619
}
604620
t->v = node->lvp.v;
605621
}
@@ -710,6 +726,10 @@ parser_compile_exe_size (struct parser_node* node, char*& p, std::size_t& exe_si
710726
t->i = AMREX_PARSER_LOCAL_IDX0 + lidx;
711727
} else {
712728
t->i = node->lvp.ip;
729+
if (t->i < 0) {
730+
throw std::runtime_error(std::string("Unknown variable ")
731+
+ ((struct parser_symbol*)node->l)->name);
732+
}
713733
}
714734
}
715735
exe_size += sizeof(ParserExeNEG_P);

0 commit comments

Comments
 (0)