Skip to content

Commit

Permalink
patch 7.4.1254
Browse files Browse the repository at this point in the history
Problem:    Opening a second channel causes a crash. (Ken Takata)
Solution:   Don't re-allocate the array with channels.
  • Loading branch information
brammool committed Feb 3, 2016
1 parent 608a891 commit 3b05b13
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
21 changes: 12 additions & 9 deletions src/channel.c
Expand Up @@ -133,22 +133,25 @@ FILE *debugfd = NULL;
add_channel(void)
{
int idx;
channel_T *new_channels;
channel_T *ch;

if (channels != NULL)
{
for (idx = 0; idx < channel_count; ++idx)
if (channels[idx].ch_fd < 0)
/* re-use a closed channel slot */
return idx;
if (channel_count == MAX_OPEN_CHANNELS)
return -1;
new_channels = (channel_T *)alloc(sizeof(channel_T) * (channel_count + 1));
if (new_channels == NULL)
return -1;
if (channels != NULL)
mch_memmove(new_channels, channels, sizeof(channel_T) * channel_count);
channels = new_channels;
if (channel_count == MAX_OPEN_CHANNELS)
return -1;
}
else
{
channels = (channel_T *)alloc((int)sizeof(channel_T)
* MAX_OPEN_CHANNELS);
if (channels == NULL)
return -1;
}

ch = &channels[channel_count];
(void)vim_memset(ch, 0, sizeof(channel_T));

Expand Down
14 changes: 4 additions & 10 deletions src/testdir/test_channel.py
Expand Up @@ -24,14 +24,10 @@
# Python 2
import SocketServer as socketserver

thesocket = None

class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):

def handle(self):
print("=== socket opened ===")
global thesocket
thesocket = self.request
while True:
try:
received = self.request.recv(4096).decode('utf-8')
Expand Down Expand Up @@ -77,19 +73,19 @@ def handle(self):
cmd = '["ex","call append(\\"$\\",\\"added1\\")"]'
cmd += '["ex","call append(\\"$\\",\\"added2\\")"]'
print("sending: {}".format(cmd))
thesocket.sendall(cmd.encode('utf-8'))
self.request.sendall(cmd.encode('utf-8'))
response = "ok"
elif decoded[1] == 'eval-works':
# Send an eval request. We ignore the response.
cmd = '["eval","\\"foo\\" . 123", -1]'
print("sending: {}".format(cmd))
thesocket.sendall(cmd.encode('utf-8'))
self.request.sendall(cmd.encode('utf-8'))
response = "ok"
elif decoded[1] == 'eval-fails':
# Send an eval request that will fail.
cmd = '["eval","xxx", -2]'
print("sending: {}".format(cmd))
thesocket.sendall(cmd.encode('utf-8'))
self.request.sendall(cmd.encode('utf-8'))
response = "ok"
elif decoded[1] == 'eval-result':
# Send back the last received eval result.
Expand All @@ -105,14 +101,12 @@ def handle(self):

encoded = json.dumps([decoded[0], response])
print("sending: {}".format(encoded))
thesocket.sendall(encoded.encode('utf-8'))
self.request.sendall(encoded.encode('utf-8'))

# Negative numbers are used for "eval" responses.
elif decoded[0] < 0:
last_eval = decoded

thesocket = None

class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass

Expand Down
24 changes: 22 additions & 2 deletions src/testdir/test_channel.vim
Expand Up @@ -17,6 +17,8 @@ else
finish
endif

let s:port = -1

func s:start_server()
" The Python program writes the port number in Xportnr.
call delete("Xportnr")
Expand Down Expand Up @@ -49,9 +51,9 @@ func s:start_server()
call assert_false(1, "Can't start test_channel.py")
return -1
endif
let port = l[0]
let s:port = l[0]

let handle = ch_open('localhost:' . port, 'json')
let handle = ch_open('localhost:' . s:port, 'json')
return handle
endfunc

Expand Down Expand Up @@ -94,6 +96,24 @@ func Test_communicate()
call s:kill_server()
endfunc

" Test that we can open two channels.
func Test_two_channels()
let handle = s:start_server()
if handle < 0
return
endif
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))

let newhandle = ch_open('localhost:' . s:port, 'json')
call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))

call ch_close(handle)
call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))

call s:kill_server()
endfunc

" Test that a server crash is handled gracefully.
func Test_server_crash()
let handle = s:start_server()
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -742,6 +742,8 @@ static char *(features[]) =

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

0 comments on commit 3b05b13

Please sign in to comment.