Skip to content

Commit 7ce3408

Browse files
authored
Merge pull request #456 from sbmlteam/issue-455
#455: ensure we dont crash on no children
2 parents 938c5b3 + 38c8b39 commit 7ce3408

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/sbml/math/ASTNode.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3899,6 +3899,42 @@ ASTNode::simplify()
38993899
delete child;
39003900
}
39013901

3902+
// special case, ensure we dont crash
3903+
if (numChildren == 0)
3904+
{
3905+
if (mType == AST_PLUS)
3906+
{
3907+
(*this) = *zero;
3908+
3909+
delete zero;
3910+
delete one;
3911+
delete two;
3912+
3913+
return;
3914+
}
3915+
3916+
if (mType == AST_TIMES)
3917+
{
3918+
(*this) = *one;
3919+
3920+
delete zero;
3921+
delete one;
3922+
delete two;
3923+
3924+
return;
3925+
}
3926+
3927+
if (mType == AST_POWER || mType == AST_DIVIDE || mType == AST_FUNCTION_POWER)
3928+
{
3929+
// dont crash
3930+
delete zero;
3931+
delete one;
3932+
delete two;
3933+
3934+
return;
3935+
}
3936+
}
3937+
39023938
// if we have 1 * x * ... dont need 1
39033939
if (mType == AST_TIMES && util_isEqual(getChild(0)->getValue(), 1.0))
39043940
{

src/sbml/math/test/TestInferRnFunctions.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,41 @@ START_TEST(test_convertRootToPower)
990990
END_TEST
991991

992992

993+
START_TEST(test_no_children)
994+
{
995+
{
996+
ASTNode n(ASTNodeType_t::AST_PLUS);
997+
n.simplify();
998+
// should be 0
999+
fail_unless(n.getType() == ASTNodeType_t::AST_REAL);
1000+
fail_unless(n.getValue() == 0.0);
1001+
}
1002+
1003+
{
1004+
ASTNode n(ASTNodeType_t::AST_TIMES);
1005+
n.simplify();
1006+
// should be 1
1007+
fail_unless(n.getType() == ASTNodeType_t::AST_REAL);
1008+
fail_unless(n.getValue() == 1.0);
1009+
}
1010+
1011+
{
1012+
ASTNode n(ASTNodeType_t::AST_DIVIDE);
1013+
n.simplify();
1014+
// should not crash, remains DIVIDE with no children
1015+
fail_unless(n.getType() == ASTNodeType_t::AST_DIVIDE);
1016+
}
1017+
1018+
1019+
{
1020+
ASTNode n(ASTNodeType_t::AST_POWER);
1021+
n.simplify();
1022+
// should not crash, remains DIVIDE with no children
1023+
fail_unless(n.getType() == ASTNodeType_t::AST_POWER);
1024+
}
1025+
}
1026+
END_TEST
1027+
9931028
Suite *
9941029
create_suite_TestInferRnFunctions()
9951030
{
@@ -1024,6 +1059,7 @@ create_suite_TestInferRnFunctions()
10241059
tcase_add_test(tcase, test_simplify4);
10251060
tcase_add_test(tcase, test_simplify5);
10261061
tcase_add_test(tcase, test_simplify6);
1062+
tcase_add_test(tcase, test_no_children);
10271063
tcase_add_test(tcase, test_convertRootToPower);
10281064

10291065
suite_add_tcase(suite, tcase);

0 commit comments

Comments
 (0)