From 1c3b2a9cf7bc298941f1165885fb1a3c76105878 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sat, 8 Nov 2014 02:18:28 +0100 Subject: 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. --- src/expressions.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src/expressions.cpp') 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: -- cgit v1.2.3