]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 7.4.1254 v7.4.1254
authorBram Moolenaar <Bram@vim.org>
Wed, 3 Feb 2016 22:25:07 +0000 (23:25 +0100)
committerBram Moolenaar <Bram@vim.org>
Wed, 3 Feb 2016 22:25:07 +0000 (23:25 +0100)
Problem:    Opening a second channel causes a crash. (Ken Takata)
Solution:   Don't re-allocate the array with channels.

src/channel.c
src/testdir/test_channel.py
src/testdir/test_channel.vim
src/version.c

index 2e5965bc7bb6e33f95f83d833f3cbb74a06d563a..9e15fe4551d1dc9b1f67ddc379bae743725d2c7d 100644 (file)
@@ -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));
 
index 3133c2f408e7291ebd0ee54f30f59600019daa50..f1d774fd11436e9ab65a1f5b31a269760126a2d7 100644 (file)
@@ -24,14 +24,10 @@ except ImportError:
     # 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')
@@ -77,19 +73,19 @@ class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
                         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.
@@ -105,14 +101,12 @@ class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
 
                     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
 
index de64188e20b30a147a10b032c408904630e3fde2..b416520e45e27b955b939475f42ee9bc523cbf75 100644 (file)
@@ -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")
@@ -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
 
@@ -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()
index 2d61d4f26c1d6e1930b48455e99eb3fc6ff00032..6a86d6a36ee973448c475591c8beab25165d52d0 100644 (file)
@@ -742,6 +742,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1254,
 /**/
     1253,
 /**/