Fix floating point division by zero in preprocessor expressions.

Closes #1183
This commit is contained in:
William S Fulton 2018-01-15 07:51:18 +00:00
commit ea5b55ecf4
4 changed files with 39 additions and 4 deletions

View file

@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.0.0 (in progress)
===========================
2018-01-15: wsfulton
Fix issue #1183. Floating point exception evaluating preprocessor expressions
resulting in division by zero.
2018-01-14: wsfulton
Fix issue #1172. Seg fault parsing invalid exponents in the preprocessor.

View file

@ -0,0 +1,13 @@
%module xxx
#define ZERO 0
#if 1%ZERO
#endif
#if 2/ZERO
#endif
#if 1%(5-5)
#endif
#if 2/(55-55)
#endif

View file

@ -0,0 +1,8 @@
pp_expressions_bad.i:5: Warning 202: Could not evaluate expression '1%ZERO'
pp_expressions_bad.i:5: Warning 202: Error: 'Modulo by zero in expression'
pp_expressions_bad.i:7: Warning 202: Could not evaluate expression '2/ZERO'
pp_expressions_bad.i:7: Warning 202: Error: 'Division by zero in expression'
pp_expressions_bad.i:10: Warning 202: Could not evaluate expression '1%(5-5)'
pp_expressions_bad.i:10: Warning 202: Error: 'Modulo by zero in expression'
pp_expressions_bad.i:12: Warning 202: Could not evaluate expression '2/(55-55)'
pp_expressions_bad.i:12: Warning 202: Error: 'Division by zero in expression'

View file

@ -188,12 +188,22 @@ static int reduce_op() {
sp--;
break;
case SWIG_TOKEN_SLASH:
stack[sp - 2].value = stack[sp - 2].value / stack[sp].value;
sp -= 2;
if (stack[sp].value != 0) {
stack[sp - 2].value = stack[sp - 2].value / stack[sp].value;
sp -= 2;
} else {
errmsg = "Division by zero in expression";
return 0;
}
break;
case SWIG_TOKEN_PERCENT:
stack[sp - 2].value = stack[sp - 2].value % stack[sp].value;
sp -= 2;
if (stack[sp].value != 0) {
stack[sp - 2].value = stack[sp - 2].value % stack[sp].value;
sp -= 2;
} else {
errmsg = "Modulo by zero in expression";
return 0;
}
break;
case SWIG_TOKEN_LSHIFT:
stack[sp - 2].value = stack[sp - 2].value << stack[sp].value;