aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2014-11-08 02:18:28 +0100
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2014-11-09 20:29:56 +0100
commit1c3b2a9cf7bc298941f1165885fb1a3c76105878 (patch)
treee0916a727f0f533cb4b509b2994633790ec898b2
parent69d53ec72d0fde8b8d21df72583e781d26c47687 (diff)
prevent assertions when pushing operators without corresponding operands
instead throw an error. The error could theoretically be thrown earlier instead of only when trying to perform a calculation. test cases: "++", "+1+", etc.
-rw-r--r--src/expressions.cpp18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/expressions.cpp b/src/expressions.cpp
index 87785a7..df18822 100644
--- a/src/expressions.cpp
+++ b/src/expressions.cpp
@@ -63,8 +63,10 @@ tecoInt
Expressions::pop_num(int index)
{
tecoInt n = 0;
+ Operator op = pop_op();
+
+ g_assert(op == OP_NUMBER);
- pop_op();
if (numbers.items() > 0) {
n = numbers.pop(index);
numbers.undo_push(n, index);
@@ -128,9 +130,17 @@ Expressions::calc(void)
{
tecoInt result;
- tecoInt vright = pop_num();
- Operator op = pop_op();
- tecoInt vleft = pop_num();
+ tecoInt vright;
+ Operator op;
+ tecoInt vleft;
+
+ if (operators.peek() != OP_NUMBER)
+ throw State::Error("Missing right operand");
+ vright = pop_num();
+ op = pop_op();
+ if (operators.peek() != OP_NUMBER)
+ throw State::Error("Missing left operand");
+ vleft = pop_num();
switch (op) {
case OP_POW: