aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZyX <kp-pav@yandex.ru>2015-12-26 01:18:14 +0300
committerZyX <kp-pav@yandex.ru>2016-02-02 00:54:00 +0300
commitb2ea083eeba5a99b90e554ad0023f0d54a21b7fe (patch)
treeb7680ff51bfee924219ef14521302e8f194bb32d
parent2873a17c55b3a056e80da54bd22af625f5891417 (diff)
downloadrneovim-b2ea083eeba5a99b90e554ad0023f0d54a21b7fe.tar.gz
rneovim-b2ea083eeba5a99b90e554ad0023f0d54a21b7fe.tar.bz2
rneovim-b2ea083eeba5a99b90e554ad0023f0d54a21b7fe.zip
eval: Return different values when dividing by zero
Fixes #3263
-rw-r--r--src/nvim/eval.c20
-rw-r--r--test/functional/eval/operators_spec.lua28
2 files changed, 43 insertions, 5 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index c360d306d4..8cf077ec19 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -4001,12 +4001,22 @@ eval6 (
* When either side is a float the result is a float.
*/
if (use_float) {
- if (op == '*')
+ if (op == '*') {
f1 = f1 * f2;
- else if (op == '/') {
- /* We rely on the floating point library to handle divide
- * by zero to result in "inf" and not a crash. */
- f1 = f2 != 0 ? f1 / f2 : INFINITY;
+ } else if (op == '/') {
+ // Division by zero triggers error from AddressSanitizer
+ f1 = (f2 == 0
+ ? (
+#ifdef NAN
+ f1 == 0
+ ? NAN
+ :
+#endif
+ (f1 > 0
+ ? INFINITY
+ : -INFINITY)
+ )
+ : f1 / f2);
} else {
EMSG(_("E804: Cannot use '%' with Float"));
return FAIL;
diff --git a/test/functional/eval/operators_spec.lua b/test/functional/eval/operators_spec.lua
new file mode 100644
index 0000000000..bc9a17935c
--- /dev/null
+++ b/test/functional/eval/operators_spec.lua
@@ -0,0 +1,28 @@
+local helpers = require('test.functional.helpers')
+local eq = helpers.eq
+local eval = helpers.eval
+local clear = helpers.clear
+
+describe('Division operator', function()
+ before_each(clear)
+
+ it('returns infinity on {positive}/0.0', function()
+ eq('str2float(\'inf\')', eval('string(1.0/0.0)'))
+ eq('str2float(\'inf\')', eval('string(1.0e-100/0.0)'))
+ eq('str2float(\'inf\')', eval('string(1.0e+100/0.0)'))
+ eq('str2float(\'inf\')', eval('string((1.0/0.0)/0.0)'))
+ end)
+
+ it('returns -infinity on {negative}/0.0', function()
+ eq('-str2float(\'inf\')', eval('string((-1.0)/0.0)'))
+ eq('-str2float(\'inf\')', eval('string((-1.0e-100)/0.0)'))
+ eq('-str2float(\'inf\')', eval('string((-1.0e+100)/0.0)'))
+ eq('-str2float(\'inf\')', eval('string((-1.0/0.0)/0.0)'))
+ end)
+
+ it('returns NaN on 0.0/0.0', function()
+ eq('str2float(\'nan\')', eval('string(0.0/0.0)'))
+ eq('str2float(\'nan\')', eval('string(-(0.0/0.0))'))
+ eq('str2float(\'nan\')', eval('string((-0.0)/0.0)'))
+ end)
+end)