diff options
author | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-11-11 08:54:07 +0000 |
---|---|---|
committer | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-11-11 08:54:07 +0000 |
commit | d544a3d6c721b435151a74cb00aa32142a284097 (patch) | |
tree | 40693434b91eadff4f090f7d08995bdfeb71680f | |
parent | 0d93d8125e0ae23599ea6d562d70d235171afc9c (diff) | |
download | ruby-d544a3d6c721b435151a74cb00aa32142a284097.tar.gz |
* win32/win32.c (rb_w32_accept): secure fd before accept because if
error causes in securing, cannot restore the state of accepted
socket.
fixed [ruby-core:19728]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | win32/win32.c | 30 |
2 files changed, 26 insertions, 11 deletions
@@ -1,3 +1,10 @@ +Tue Nov 11 17:35:25 2008 NAKAMURA Usaku <usa@ruby-lang.org> + + * win32/win32.c (rb_w32_accept): secure fd before accept because if + error causes in securing, cannot restore the state of accepted + socket. + fixed [ruby-core:19728] + Tue Nov 11 14:40:40 2008 Martin Duerst <duerst@it.aoyama.ac.jp> * test/ruby/test_transcode.rb: unnecessary setup method diff --git a/win32/win32.c b/win32/win32.c index ae692ce9f0..f2129afac8 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -2296,25 +2296,33 @@ int WSAAPI rb_w32_accept(int s, struct sockaddr *addr, int *addrlen) { SOCKET r; + int fd; if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ - r = accept(TO_SOCKET(s), addr, addrlen); - if (r == INVALID_SOCKET) { - errno = map_errno(WSAGetLastError()); - s = -1; - } - else { - s = rb_w32_open_osfhandle(r, O_RDWR|O_BINARY|O_NOINHERIT); - if (s != -1) + HANDLE h = CreateFile("NUL", 0, 0, NULL, OPEN_ALWAYS, 0, NULL); + fd = rb_w32_open_osfhandle((intptr_t)h, O_RDWR|O_BINARY|O_NOINHERIT); + if (fd != -1) { + r = accept(TO_SOCKET(s), addr, addrlen); + if (r != INVALID_SOCKET) { + MTHREAD_ONLY(EnterCriticalSection(&(_pioinfo(fd)->lock))); + _set_osfhnd(fd, r); + MTHREAD_ONLY(LeaveCriticalSection(&_pioinfo(fd)->lock)); + CloseHandle(h); st_insert(socklist, (st_data_t)r, (st_data_t)0); - else - closesocket(r); + } + else { + errno = map_errno(WSAGetLastError()); + close(fd); + fd = -1; + } } + else + CloseHandle(h); }); - return s; + return fd; } #undef bind |