Skip to content

Commit

Permalink
fix segfault
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbra committed Jun 25, 2023
1 parent 8154e64 commit 65ed29f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/memline.c
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,18 @@ ml_sync_all(int check_file, int check_char)
|| buf->b_ml.ml_mfp->mf_fd < 0)
continue; // no file

// Safety Check
if (crypt_method_is_sodium(crypt_get_method_nr(buf))
&& *buf->b_p_key != NUL)
{
// close the swapfile
mf_close_file(buf, TRUE);
buf->b_p_swf = FALSE;
vim_free(buf->b_p_key);
buf->b_p_key = empty_option;
continue;
}

ml_flush_line(buf); // flush buffered line
// flush locked block
(void)ml_find_line(buf, (linenr_T)0, ML_FLUSH);
Expand Down Expand Up @@ -5571,6 +5583,15 @@ ml_crypt_prepare(memfile_T *mfp, off_T offset, int reading)
if (*key == NUL)
return NULL;

// safety check
if (crypt_method_is_sodium(method_nr))
{
// close the swapfile
mf_close_file(buf, TRUE);
buf->b_p_swf = FALSE;
return NULL;
}

if (method_nr == CRYPT_M_ZIP)
{
// For PKzip: Append the offset to the key, so that we use a different
Expand Down
20 changes: 20 additions & 0 deletions src/testdir/test_crypt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,24 @@ func Test_crypt_set_key_changes_buffer()
call delete('Xtest1.txt')
endfunc

func Test_crypt_set_key_segfault()
CheckFeature sodium

defer delete('Xtest2.txt')
new Xtest2.txt
call setline(1, 'nothing')
set cryptmethod=xchacha20
set key=foobar
w
new Xtest3
put ='other content'
setl modified
sil! preserve
bw!

set cryptmethod&
set key=
bwipe!
endfunc

" vim: shiftwidth=2 sts=2 expandtab

0 comments on commit 65ed29f

Please sign in to comment.