Patch 8.2.3309
Problem:    Vim9: divide by zero causes a crash.
Solution:   Give an error message. (closes #8727)
Files:      src/vim9execute.c, src/testdir/test_vim9_expr.vim


*** ../vim-8.2.3308/src/vim9execute.c   2021-08-07 15:35:32.790064386 +0200
--- src/vim9execute.c   2021-08-07 15:49:37.679695065 +0200
***************
*** 3435,3447 ****
                    typval_T    *tv2 = STACK_TV_BOT(-1);
                    varnumber_T arg1 = tv1->vval.v_number;
                    varnumber_T arg2 = tv2->vval.v_number;
!                   varnumber_T res;
  
                    switch (iptr->isn_arg.op.op_type)
                    {
                        case EXPR_MULT: res = arg1 * arg2; break;
!                       case EXPR_DIV: res = arg1 / arg2; break;
!                       case EXPR_REM: res = arg1 % arg2; break;
                        case EXPR_SUB: res = arg1 - arg2; break;
                        case EXPR_ADD: res = arg1 + arg2; break;
  
--- 3435,3456 ----
                    typval_T    *tv2 = STACK_TV_BOT(-1);
                    varnumber_T arg1 = tv1->vval.v_number;
                    varnumber_T arg2 = tv2->vval.v_number;
!                   varnumber_T res = 0;
!                   int         div_zero = FALSE;
  
                    switch (iptr->isn_arg.op.op_type)
                    {
                        case EXPR_MULT: res = arg1 * arg2; break;
!                       case EXPR_DIV:  if (arg2 == 0)
!                                           div_zero = TRUE;
!                                       else
!                                           res = arg1 / arg2;
!                                       break;
!                       case EXPR_REM:  if (arg2 == 0)
!                                           div_zero = TRUE;
!                                       else
!                                           res = arg1 % arg2;
!                                       break;
                        case EXPR_SUB: res = arg1 - arg2; break;
                        case EXPR_ADD: res = arg1 + arg2; break;
  
***************
*** 3451,3457 ****
                        case EXPR_GEQUAL: res = arg1 >= arg2; break;
                        case EXPR_SMALLER: res = arg1 < arg2; break;
                        case EXPR_SEQUAL: res = arg1 <= arg2; break;
!                       default: res = 0; break;
                    }
  
                    --ectx->ec_stack.ga_len;
--- 3460,3466 ----
                        case EXPR_GEQUAL: res = arg1 >= arg2; break;
                        case EXPR_SMALLER: res = arg1 < arg2; break;
                        case EXPR_SEQUAL: res = arg1 <= arg2; break;
!                       default: break;
                    }
  
                    --ectx->ec_stack.ga_len;
***************
*** 3462,3467 ****
--- 3471,3482 ----
                    }
                    else
                        tv1->vval.v_number = res;
+                   if (div_zero)
+                   {
+                       SOURCING_LNUM = iptr->isn_lnum;
+                       emsg(_(e_divide_by_zero));
+                       goto on_error;
+                   }
                }
                break;
  
*** ../vim-8.2.3308/src/testdir/test_vim9_expr.vim      2021-07-28 
20:52:08.681166840 +0200
--- src/testdir/test_vim9_expr.vim      2021-08-07 15:48:31.639881136 +0200
***************
*** 1348,1354 ****
  
  def Test_expr5_vim9script_channel()
    if !has('channel')
!     MissingFeature 'float'
    else
      var lines =<< trim END
          echo 'a' .. test_null_job()
--- 1348,1354 ----
  
  def Test_expr5_vim9script_channel()
    if !has('channel')
!     MissingFeature 'channel'
    else
      var lines =<< trim END
          echo 'a' .. test_null_job()
***************
*** 1502,1507 ****
--- 1502,1519 ----
  
    CheckDefExecAndScriptFailure(['echo 1 / 0'], 'E1154', 1)
    CheckDefExecAndScriptFailure(['echo 1 % 0'], 'E1154', 1)
+ 
+   lines =<< trim END
+     var n = 0
+     eval 1 / n
+   END
+   CheckDefExecAndScriptFailure(lines, 'E1154', 2)
+ 
+   lines =<< trim END
+     var n = 0
+     eval 1 % n
+   END
+   CheckDefExecAndScriptFailure(lines, 'E1154', 2)
  enddef
  
  def Test_expr6_vim9script()
*** ../vim-8.2.3308/src/version.c       2021-08-07 15:35:32.790064386 +0200
--- src/version.c       2021-08-07 15:46:43.740185008 +0200
***************
*** 757,758 ****
--- 757,760 ----
  {   /* Add new patch number below this line */
+ /**/
+     3309,
  /**/

-- 
Individualists unite!

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202108071351.177Dp9781845996%40masaka.moolenaar.net.

Raspunde prin e-mail lui