From 0e95414ab2705649cea323115a618b558c30397f Mon Sep 17 00:00:00 2001
From: Tomas Heinrich <theinric@redhat.com>
Date: Wed, 2 Jul 2014 17:01:09 +0200
Subject: [PATCH] bugfix: prevent division by zero
The result is set to 0 in that case. Might be better to do something
else in the future, but at least it'll stop crashing for now.
---
grammar/rainerscript.c | 29 +++++++++++++++++++++++++----
1 file changed, 25 insertions(+), 4 deletions(-)
diff --git a/grammar/rainerscript.c b/grammar/rainerscript.c
index 25d9c9a..c3365b1 100644
--- a/grammar/rainerscript.c
+++ b/grammar/rainerscript.c
@@ -1560,6 +1560,17 @@ evalStrArrayCmp(es_str_t *estr_l, struct cnfarray* ar, int cmpop)
ret->d.n = var2Number(&l, &convok_l) x var2Number(&r, &convok_r); \
FREE_BOTH_RET
+#define COMP_NUM_BINOP_DIV(x) \
+ cnfexprEval(expr->l, &l, usrptr); \
+ cnfexprEval(expr->r, &r, usrptr); \
+ ret->datatype = 'N'; \
+ if((ret->d.n = var2Number(&r, &convok_r)) == 0) { \
+ /* division by zero */ \
+ } else { \
+ ret->d.n = var2Number(&l, &convok_l) x ret->d.n; \
+ } \
+ FREE_BOTH_RET
+
/* NOTE: array as right-hand argument MUST be handled by user */
#define PREP_TWO_STRINGS \
cnfexprEval(expr->l, &l, usrptr); \
@@ -1965,10 +1976,10 @@ cnfexprEval(struct cnfexpr *expr, struct var *ret, void* usrptr)
COMP_NUM_BINOP(*);
break;
case '/':
- COMP_NUM_BINOP(/);
+ COMP_NUM_BINOP_DIV(/);
break;
case '%':
- COMP_NUM_BINOP(%);
+ COMP_NUM_BINOP_DIV(%);
break;
case 'M':
cnfexprEval(expr->r, &r, usrptr);
@@ -2907,13 +2918,23 @@ cnfexprOptimize(struct cnfexpr *expr)
case '/':
if(getConstNumber(expr, &ln, &rn)) {
expr->nodetype = 'N';
- ((struct cnfnumval*)expr)->val = ln / rn;
+ if(rn == 0) {
+ /* division by zero */
+ ((struct cnfnumval*)expr)->val = 0;
+ } else {
+ ((struct cnfnumval*)expr)->val = ln / rn;
+ }
}
break;
case '%':
if(getConstNumber(expr, &ln, &rn)) {
expr->nodetype = 'N';
- ((struct cnfnumval*)expr)->val = ln % rn;
+ if(rn == 0) {
+ /* division by zero */
+ ((struct cnfnumval*)expr)->val = 0;
+ } else {
+ ((struct cnfnumval*)expr)->val = ln % rn;
+ }
}
break;
case CMP_NE:
--
1.9.3