]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.0804: crash when trying to divide a number by -1 v9.0.0804
authorBram Moolenaar <Bram@vim.org>
Thu, 20 Oct 2022 13:17:18 +0000 (14:17 +0100)
committerBram Moolenaar <Bram@vim.org>
Thu, 20 Oct 2022 13:17:18 +0000 (14:17 +0100)
Problem:    Crash when trying to divice the largest negative number by -1.
Solution:   Handle this case specifically.

src/eval.c
src/testdir/test_expr.vim
src/version.c

index 1652fcb4ae4820ad07b36bcc06d25eff3294b2ba..062fab0ac9493d712b76827150ee966a72da9135 100644 (file)
@@ -66,6 +66,12 @@ num_divide(varnumber_T n1, varnumber_T n2, int *failed)
        else
            result = VARNUM_MAX;
     }
+    else if (n1 == VARNUM_MIN && n2 == -1)
+    {
+       // specific case: trying to do VARNUM_MIN / -1 results in a positive
+       // number that doesn't fit in varnumber_T and causes an FPE
+       result = VARNUM_MAX;
+    }
     else
        result = n1 / n2;
 
@@ -6023,7 +6029,7 @@ var2fpos(
 }
 
 /*
- * Convert list in "arg" into position "psop" and optional file number "fnump".
+ * Convert list in "arg" into position "posp" and optional file number "fnump".
  * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
  * Note that the column is passed on as-is, the caller may want to decrement
  * it to use 1 for the first column.
index b47896340f6059ebc7be84f66a7864d6f62dcf2d..e1fed369b747309039973d42cf969e277ec8cdf9 100644 (file)
@@ -761,6 +761,12 @@ func Test_eval_after_if()
   call assert_equal('b', s:val)
 endfunc
 
+func Test_divide_by_zero()
+  " only tests that this doesn't crash, the result is not important
+  echo 0 / 0
+  echo 0 / 0 / -1
+endfunc
+
 " Test for command-line completion of expressions
 func Test_expr_completion()
   CheckFeature cmdline_compl
index 0de044e3ceaab4714136fc80fbc31738ea9fc89f..b4756ca0493788f0ee4395f9909d8a58844075f2 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    804,
 /**/
     803,
 /**/