Skip to content

Commit f900cf5

Browse files
committed
- fix crashes on 0 args
1 parent b511ff3 commit f900cf5

File tree

2 files changed

+125
-8
lines changed

2 files changed

+125
-8
lines changed

src/sbml/math/ASTNode.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3900,7 +3900,7 @@ ASTNode::simplify()
39003900
}
39013901

39023902
// if we have 1 * x * ... dont need 1
3903-
if (mType == AST_TIMES && util_isEqual(getChild(0)->getValue(), 1.0))
3903+
if (mType == AST_TIMES && numChildren > 0 && util_isEqual(getChild(0)->getValue(), 1.0))
39043904
{
39053905
ASTNode * newNode = new ASTNode(AST_TIMES);
39063906
for (unsigned int i = 1; i < numChildren; ++i)
@@ -3913,7 +3913,7 @@ ASTNode::simplify()
39133913
}
39143914

39153915
// if we have A - A should be 0
3916-
if (mType == AST_MINUS && getChild(0)->exactlyEqual(*getChild(1)))
3916+
if (mType == AST_MINUS && numChildren > 0 && getChild(0)->exactlyEqual(*getChild(1)))
39173917
{
39183918
ASTNode* child = zero->deepCopy();
39193919
(*this) = *(child);
@@ -3924,7 +3924,7 @@ ASTNode::simplify()
39243924
// or n + a + a should get n + 2*a
39253925
// or n + a + b + b should get n + a + 2*b
39263926

3927-
if (mType == AST_PLUS)
3927+
if (mType == AST_PLUS && numChildren > 0)
39283928
{
39293929
bool match = false;
39303930
unsigned int i;
@@ -3948,21 +3948,21 @@ ASTNode::simplify()
39483948
}
39493949
}
39503950
// if we have A/A should be 1
3951-
if (mType == AST_DIVIDE && getChild(0)->exactlyEqual(*getChild(1)))
3951+
if (mType == AST_DIVIDE && numChildren > 1 && getChild(0)->exactlyEqual(*getChild(1)))
39523952
{
39533953
ASTNode* child = one->deepCopy();
39543954
(*this) = *(child);
39553955
delete child;
39563956
}
39573957
// if we have A^1 just have A
3958-
if ((mType == AST_POWER || mType == AST_FUNCTION_POWER) && getChild(1)->exactlyEqual(*one))
3958+
if ((mType == AST_POWER || mType == AST_FUNCTION_POWER) && numChildren > 1 && getChild(1)->exactlyEqual(*one))
39593959
{
39603960
ASTNode* child = getChild(0)->deepCopy();
39613961
(*this) = *(child);
39623962
delete child;
39633963
}
39643964
// if we have A^0 just have 1
3965-
if ((mType == AST_POWER || mType == AST_FUNCTION_POWER) && getChild(1)->exactlyEqual(*zero))
3965+
if ((mType == AST_POWER || mType == AST_FUNCTION_POWER) && numChildren > 1 && getChild(1)->exactlyEqual(*zero))
39663966
{
39673967
ASTNode* child = one->deepCopy();
39683968
(*this) = *(child);
@@ -4190,7 +4190,7 @@ ASTNode::decompose()
41904190
}
41914191
}
41924192
}
4193-
else if (getType() == AST_DIVIDE)
4193+
else if (getType() == AST_DIVIDE && getNumChildren() > 0)
41944194
{
41954195
type = getChild(0)->getType();
41964196
if (type == AST_PLUS || type == AST_MINUS)

src/sbml/math/test/TestDerivativeFunctions.cpp

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ equals(const char* expected, const char* actual)
7979
static bool
8080
formulas_equal(const char* expected, ASTNode* actual)
8181
{
82-
return equals(expected, SBML_formulaToL3String(actual));
82+
return equals(expected, SBML_formulaToL3String(actual));
8383
}
8484

8585
START_TEST (test_deriv_const)
@@ -174,6 +174,20 @@ START_TEST(test_deriv_plus)
174174
delete n;
175175
delete node;
176176
delete deriv;
177+
178+
// test 0 args:
179+
n = readMathMLFromString(
180+
"<math xmlns='http://www.w3.org/1998/Math/MathML'>"
181+
" <apply>"
182+
" <plus/>"
183+
" </apply>"
184+
"</math>"
185+
);
186+
187+
deriv = n->derivative(x);
188+
189+
delete n;
190+
delete deriv;
177191
}
178192
END_TEST
179193

@@ -299,6 +313,25 @@ START_TEST(test_deriv_times)
299313
delete n;
300314
delete node;
301315
delete deriv;
316+
317+
// also check that it does not crash on no args:
318+
n = readMathMLFromString(
319+
"<math xmlns='http://www.w3.org/1998/Math/MathML'>"
320+
" <apply>"
321+
" <times/>"
322+
" </apply>"
323+
"</math>"
324+
);
325+
326+
deriv = n->derivative(x);
327+
328+
// should be 0
329+
fail_unless(deriv->isNumber() == true);
330+
fail_unless(deriv->getValue() == 0.0);
331+
332+
delete n;
333+
delete deriv;
334+
302335
}
303336
END_TEST
304337

@@ -361,6 +394,19 @@ START_TEST(test_deriv_divide)
361394
delete n;
362395
delete node;
363396
delete deriv;
397+
398+
// test 0 args:
399+
n = readMathMLFromString(
400+
"<math xmlns='http://www.w3.org/1998/Math/MathML'>"
401+
" <apply>"
402+
" <divide/>"
403+
" </apply>"
404+
"</math>"
405+
);
406+
deriv = n->derivative(x);
407+
408+
delete n;
409+
delete deriv;
364410
}
365411
END_TEST
366412

@@ -484,6 +530,23 @@ START_TEST(test_deriv_minus)
484530
delete n;
485531
delete node;
486532
delete deriv;
533+
534+
535+
// test 0 args
536+
n = readMathMLFromString(
537+
"<math xmlns='http://www.w3.org/1998/Math/MathML'>"
538+
" <apply>"
539+
" <minus/>"
540+
" </apply>"
541+
"</math>"
542+
);
543+
544+
deriv = n->derivative(x);
545+
546+
delete n;
547+
delete deriv;
548+
549+
487550
}
488551
END_TEST
489552

@@ -613,6 +676,19 @@ START_TEST(test_deriv_power)
613676
delete n;
614677
delete node;
615678
delete deriv;
679+
680+
// test 0 arg
681+
n = readMathMLFromString(
682+
"<math xmlns='http://www.w3.org/1998/Math/MathML'>"
683+
" <apply>"
684+
" <power/>"
685+
" </apply>"
686+
"</math>"
687+
);
688+
689+
deriv = n->derivative(x);
690+
delete n;
691+
delete deriv;
616692
}
617693
END_TEST
618694

@@ -810,6 +886,19 @@ START_TEST(test_deriv_log)
810886
delete n;
811887
delete node;
812888
delete deriv;
889+
890+
// test 0 args:
891+
n = readMathMLFromString(
892+
"<math xmlns='http://www.w3.org/1998/Math/MathML'>"
893+
" <apply>"
894+
" <log/>"
895+
" </apply>"
896+
"</math>");
897+
898+
deriv = n->derivative(x);
899+
900+
delete n;
901+
delete deriv;
813902
}
814903
END_TEST
815904

@@ -957,6 +1046,20 @@ START_TEST(test_deriv_exp)
9571046
delete n;
9581047
delete node;
9591048
delete deriv;
1049+
1050+
// test 0 args:
1051+
n = readMathMLFromString(
1052+
"<math xmlns='http://www.w3.org/1998/Math/MathML'>"
1053+
" <apply>"
1054+
" <exp/>"
1055+
" </apply>"
1056+
"</math>"
1057+
);
1058+
1059+
deriv = n->derivative(x);
1060+
1061+
delete n;
1062+
delete deriv;
9601063
}
9611064
END_TEST
9621065

@@ -1016,6 +1119,20 @@ START_TEST(test_deriv_abs)
10161119
delete n;
10171120
delete node;
10181121
delete deriv;
1122+
1123+
// test 0 args
1124+
n = readMathMLFromString(
1125+
"<math xmlns='http://www.w3.org/1998/Math/MathML'>"
1126+
" <apply>"
1127+
" <abs/>"
1128+
" </apply>"
1129+
"</math>"
1130+
);
1131+
1132+
deriv = n->derivative(x);
1133+
1134+
delete n;
1135+
delete deriv;
10191136
}
10201137
END_TEST
10211138

0 commit comments

Comments
 (0)