Skip to content

Commit

Permalink
patch 9.0.0804: crash when trying to divide a number by -1
Browse files Browse the repository at this point in the history
Problem:    Crash when trying to divice the largest negative number by -1.
Solution:   Handle this case specifically.
  • Loading branch information
brammool committed Oct 20, 2022
1 parent 4362576 commit cdef1ce
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/eval.c
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions src/testdir/test_expr.vim
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -695,6 +695,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
804,
/**/
803,
/**/
Expand Down

0 comments on commit cdef1ce

Please sign in to comment.