diff --git a/CHANGES.current b/CHANGES.current index 0618e9725..e2072fd33 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -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. diff --git a/Examples/test-suite/errors/pp_expressions_bad.i b/Examples/test-suite/errors/pp_expressions_bad.i new file mode 100644 index 000000000..8f7b3abb3 --- /dev/null +++ b/Examples/test-suite/errors/pp_expressions_bad.i @@ -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 diff --git a/Examples/test-suite/errors/pp_expressions_bad.stderr b/Examples/test-suite/errors/pp_expressions_bad.stderr new file mode 100644 index 000000000..4f7aec541 --- /dev/null +++ b/Examples/test-suite/errors/pp_expressions_bad.stderr @@ -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' diff --git a/Source/Preprocessor/expr.c b/Source/Preprocessor/expr.c index 6d22c8c5e..f6de7ef84 100644 --- a/Source/Preprocessor/expr.c +++ b/Source/Preprocessor/expr.c @@ -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;