Skip to content

Commit 3b05b13

Browse files
committed
patch 7.4.1254
Problem: Opening a second channel causes a crash. (Ken Takata) Solution: Don't re-allocate the array with channels.
1 parent 608a891 commit 3b05b13

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

src/channel.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,25 @@ FILE *debugfd = NULL;
133133
add_channel(void)
134134
{
135135
int idx;
136-
channel_T *new_channels;
137136
channel_T *ch;
138137

139138
if (channels != NULL)
139+
{
140140
for (idx = 0; idx < channel_count; ++idx)
141141
if (channels[idx].ch_fd < 0)
142142
/* re-use a closed channel slot */
143143
return idx;
144-
if (channel_count == MAX_OPEN_CHANNELS)
145-
return -1;
146-
new_channels = (channel_T *)alloc(sizeof(channel_T) * (channel_count + 1));
147-
if (new_channels == NULL)
148-
return -1;
149-
if (channels != NULL)
150-
mch_memmove(new_channels, channels, sizeof(channel_T) * channel_count);
151-
channels = new_channels;
144+
if (channel_count == MAX_OPEN_CHANNELS)
145+
return -1;
146+
}
147+
else
148+
{
149+
channels = (channel_T *)alloc((int)sizeof(channel_T)
150+
* MAX_OPEN_CHANNELS);
151+
if (channels == NULL)
152+
return -1;
153+
}
154+
152155
ch = &channels[channel_count];
153156
(void)vim_memset(ch, 0, sizeof(channel_T));
154157

src/testdir/test_channel.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@
2424
# Python 2
2525
import SocketServer as socketserver
2626

27-
thesocket = None
28-
2927
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
3028

3129
def handle(self):
3230
print("=== socket opened ===")
33-
global thesocket
34-
thesocket = self.request
3531
while True:
3632
try:
3733
received = self.request.recv(4096).decode('utf-8')
@@ -77,19 +73,19 @@ def handle(self):
7773
cmd = '["ex","call append(\\"$\\",\\"added1\\")"]'
7874
cmd += '["ex","call append(\\"$\\",\\"added2\\")"]'
7975
print("sending: {}".format(cmd))
80-
thesocket.sendall(cmd.encode('utf-8'))
76+
self.request.sendall(cmd.encode('utf-8'))
8177
response = "ok"
8278
elif decoded[1] == 'eval-works':
8379
# Send an eval request. We ignore the response.
8480
cmd = '["eval","\\"foo\\" . 123", -1]'
8581
print("sending: {}".format(cmd))
86-
thesocket.sendall(cmd.encode('utf-8'))
82+
self.request.sendall(cmd.encode('utf-8'))
8783
response = "ok"
8884
elif decoded[1] == 'eval-fails':
8985
# Send an eval request that will fail.
9086
cmd = '["eval","xxx", -2]'
9187
print("sending: {}".format(cmd))
92-
thesocket.sendall(cmd.encode('utf-8'))
88+
self.request.sendall(cmd.encode('utf-8'))
9389
response = "ok"
9490
elif decoded[1] == 'eval-result':
9591
# Send back the last received eval result.
@@ -105,14 +101,12 @@ def handle(self):
105101

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

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

114-
thesocket = None
115-
116110
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
117111
pass
118112

src/testdir/test_channel.vim

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ else
1717
finish
1818
endif
1919

20+
let s:port = -1
21+
2022
func s:start_server()
2123
" The Python program writes the port number in Xportnr.
2224
call delete("Xportnr")
@@ -49,9 +51,9 @@ func s:start_server()
4951
call assert_false(1, "Can't start test_channel.py")
5052
return -1
5153
endif
52-
let port = l[0]
54+
let s:port = l[0]
5355

54-
let handle = ch_open('localhost:' . port, 'json')
56+
let handle = ch_open('localhost:' . s:port, 'json')
5557
return handle
5658
endfunc
5759

@@ -94,6 +96,24 @@ func Test_communicate()
9496
call s:kill_server()
9597
endfunc
9698

99+
" Test that we can open two channels.
100+
func Test_two_channels()
101+
let handle = s:start_server()
102+
if handle < 0
103+
return
104+
endif
105+
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
106+
107+
let newhandle = ch_open('localhost:' . s:port, 'json')
108+
call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
109+
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
110+
111+
call ch_close(handle)
112+
call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
113+
114+
call s:kill_server()
115+
endfunc
116+
97117
" Test that a server crash is handled gracefully.
98118
func Test_server_crash()
99119
let handle = s:start_server()

src/version.c

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

743743
static int included_patches[] =
744744
{ /* Add new patch number below this line */
745+
/**/
746+
1254,
745747
/**/
746748
1253,
747749
/**/

0 commit comments

Comments
 (0)