Skip to content

Commit 32e5ec0

Browse files
committed
patch 8.2.1962: netbeans may access freed memory
Problem: Netbeans may access freed memory. Solution: Check the buffer pointer is still valid. Add a test. (Yegappan Lakshmanan, closes #7248)
1 parent 85d9b03 commit 32e5ec0

3 files changed

Lines changed: 49 additions & 9 deletions

File tree

src/netbeans.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ nb_free(void)
572572
buf = buf_list[i];
573573
vim_free(buf.displayname);
574574
vim_free(buf.signmap);
575-
if (buf.bufp != NULL)
575+
if (buf.bufp != NULL && buf_valid(buf.bufp))
576576
{
577577
buf.bufp->b_netbeans_file = FALSE;
578578
buf.bufp->b_was_netbeans_file = FALSE;
@@ -1943,15 +1943,13 @@ nb_do_cmd(
19431943
if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
19441944
{
19451945
emsg("E532: highlighting color name too long in defineAnnoType");
1946-
vim_free(typeName);
1946+
VIM_CLEAR(typeName);
19471947
parse_error = TRUE;
19481948
}
19491949
else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
19501950
addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
1951-
else
1952-
vim_free(typeName);
19531951

1954-
// don't free typeName; it's used directly in addsigntype()
1952+
vim_free(typeName);
19551953
vim_free(fg);
19561954
vim_free(bg);
19571955
vim_free(tooltip);
@@ -3240,7 +3238,7 @@ addsigntype(
32403238
}
32413239
}
32423240

3243-
globalsignmap[i] = (char *)typeName;
3241+
globalsignmap[i] = (char *)vim_strsave(typeName);
32443242
globalsignmapused = i + 1;
32453243
}
32463244

src/testdir/test_netbeans.vim

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ endfunc
3434
" Read the "Xnetbeans" file and filter out geometry messages.
3535
func ReadXnetbeans()
3636
let l = readfile("Xnetbeans")
37-
" Xnetbeans may include '0:geometry=' messages on GUI environment if window
37+
" Xnetbeans may include '0:geometry=' messages in the GUI Vim if the window
3838
" position, size, or z order are changed. Remove these messages because
39-
" will causes troubles on check.
39+
" these message will break the assert for the output.
4040
return filter(l, 'v:val !~ "^0:geometry="')
4141
endfunc
4242

@@ -388,7 +388,7 @@ func Nb_basic(port)
388388
call assert_equal('send: 2:defineAnnoType!60 1 "s1" "x" "=>" blue none', l[-1])
389389
sleep 1m
390390
call assert_equal({'name': '1', 'texthl': 'NB_s1', 'text': '=>'},
391-
\ sign_getdefined()[0])
391+
\ sign_getdefined()->get(0, {}))
392392
let g:last += 3
393393

394394
" defineAnnoType with a long color name
@@ -892,4 +892,44 @@ func Test_nb_quit_with_conn()
892892
call s:run_server('Nb_quit_with_conn')
893893
endfunc
894894

895+
func Nb_bwipe_buffer(port)
896+
call delete("Xnetbeans")
897+
call writefile([], "Xnetbeans")
898+
899+
" Last line number in the Xnetbeans file. Used to verify the result of the
900+
" communication with the netbeans server
901+
let g:last = 0
902+
903+
" Establish the connection with the netbeans server
904+
exe 'nbstart :localhost:' .. a:port .. ':bunny'
905+
call WaitFor('len(ReadXnetbeans()) > (g:last + 2)')
906+
let l = ReadXnetbeans()
907+
call assert_equal(['AUTH bunny',
908+
\ '0:version=0 "2.5"',
909+
\ '0:startupDone=0'], l[-3:])
910+
let g:last += 3
911+
912+
" Open the command buffer to communicate with the server
913+
split Xcmdbuf
914+
call WaitFor('len(ReadXnetbeans()) > (g:last + 2)')
915+
let l = ReadXnetbeans()
916+
call assert_equal('0:fileOpened=0 "Xcmdbuf" T F',
917+
\ substitute(l[-3], '".*/', '"', ''))
918+
call assert_equal('send: 1:putBufferNumber!15 "Xcmdbuf"',
919+
\ substitute(l[-2], '".*/', '"', ''))
920+
call assert_equal('1:startDocumentListen!16', l[-1])
921+
let g:last += 3
922+
923+
sleep 10m
924+
endfunc
925+
926+
" This test used to reference a buffer after it was freed leading to an ASAN
927+
" error.
928+
func Test_nb_bwipe_buffer()
929+
call s:run_server('Nb_bwipe_buffer')
930+
%bwipe!
931+
sleep 100m
932+
nbclose
933+
endfunc
934+
895935
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
1962,
753755
/**/
754756
1961,
755757
/**/

0 commit comments

Comments
 (0)