Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r52465 - trunk/boost/asio/detail
From: chris_at_[hidden]
Date: 2009-04-18 09:27:54


Author: chris_kohlhoff
Date: 2009-04-18 09:27:53 EDT (Sat, 18 Apr 2009)
New Revision: 52465
URL: http://svn.boost.org/trac/boost/changeset/52465

Log:
POSIX allows successful system calls to modify errno, so always clear the
error_code if the result indicates success. Fixes #2953.

Text files modified:
   trunk/boost/asio/detail/descriptor_ops.hpp | 45 ++++++++++++++++++----
   trunk/boost/asio/detail/socket_ops.hpp | 77 ++++++++++++++++++++++-----------------
   2 files changed, 79 insertions(+), 43 deletions(-)

Modified: trunk/boost/asio/detail/descriptor_ops.hpp
==============================================================================
--- trunk/boost/asio/detail/descriptor_ops.hpp (original)
+++ trunk/boost/asio/detail/descriptor_ops.hpp 2009-04-18 09:27:53 EDT (Sat, 18 Apr 2009)
@@ -50,13 +50,19 @@
 inline int open(const char* path, int flags, boost::system::error_code& ec)
 {
   clear_error(ec);
- return error_wrapper(::open(path, flags), ec);
+ int result = error_wrapper(::open(path, flags), ec);
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 }
 
 inline int close(int d, boost::system::error_code& ec)
 {
   clear_error(ec);
- return error_wrapper(::close(d), ec);
+ int result = error_wrapper(::close(d), ec);
+ if (result == 0)
+ clear_error(ec);
+ return result;
 }
 
 inline void init_buf_iov_base(void*& base, void* addr)
@@ -88,33 +94,48 @@
     boost::system::error_code& ec)
 {
   clear_error(ec);
- return error_wrapper(::readv(d, bufs, static_cast<int>(count)), ec);
+ int result = error_wrapper(::readv(d, bufs, static_cast<int>(count)), ec);
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 }
 
 inline int gather_write(int d, const buf* bufs, size_t count,
     boost::system::error_code& ec)
 {
   clear_error(ec);
- return error_wrapper(::writev(d, bufs, static_cast<int>(count)), ec);
+ int result = error_wrapper(::writev(d, bufs, static_cast<int>(count)), ec);
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 }
 
 inline int ioctl(int d, long cmd, ioctl_arg_type* arg,
     boost::system::error_code& ec)
 {
   clear_error(ec);
- return error_wrapper(::ioctl(d, cmd, arg), ec);
+ int result = error_wrapper(::ioctl(d, cmd, arg), ec);
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 }
 
 inline int fcntl(int d, long cmd, boost::system::error_code& ec)
 {
   clear_error(ec);
- return error_wrapper(::fcntl(d, cmd), ec);
+ int result = error_wrapper(::fcntl(d, cmd), ec);
+ if (result != -1)
+ clear_error(ec);
+ return result;
 }
 
 inline int fcntl(int d, long cmd, long arg, boost::system::error_code& ec)
 {
   clear_error(ec);
- return error_wrapper(::fcntl(d, cmd, arg), ec);
+ int result = error_wrapper(::fcntl(d, cmd, arg), ec);
+ if (result != -1)
+ clear_error(ec);
+ return result;
 }
 
 inline int poll_read(int d, boost::system::error_code& ec)
@@ -125,7 +146,10 @@
   fds.events = POLLIN;
   fds.revents = 0;
   clear_error(ec);
- return error_wrapper(::poll(&fds, 1, -1), ec);
+ int result = error_wrapper(::poll(&fds, 1, -1), ec);
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 }
 
 inline int poll_write(int d, boost::system::error_code& ec)
@@ -136,7 +160,10 @@
   fds.events = POLLOUT;
   fds.revents = 0;
   clear_error(ec);
- return error_wrapper(::poll(&fds, 1, -1), ec);
+ int result = error_wrapper(::poll(&fds, 1, -1), ec);
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 }
 
 } // namespace descriptor_ops

Modified: trunk/boost/asio/detail/socket_ops.hpp
==============================================================================
--- trunk/boost/asio/detail/socket_ops.hpp (original)
+++ trunk/boost/asio/detail/socket_ops.hpp 2009-04-18 09:27:53 EDT (Sat, 18 Apr 2009)
@@ -103,10 +103,7 @@
   }
 #endif
 
-#if defined(BOOST_WINDOWS)
   clear_error(ec);
-#endif
-
   return new_s;
 }
 
@@ -123,10 +120,8 @@
   clear_error(ec);
   int result = error_wrapper(call_bind(
         &msghdr::msg_namelen, s, addr, addrlen), ec);
-#if defined(BOOST_WINDOWS)
   if (result == 0)
     clear_error(ec);
-#endif
   return result;
 }
 
@@ -135,22 +130,20 @@
   clear_error(ec);
 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
   int result = error_wrapper(::closesocket(s), ec);
+#else // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+ int result = error_wrapper(::close(s), ec);
+#endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
   if (result == 0)
     clear_error(ec);
   return result;
-#else // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
- return error_wrapper(::close(s), ec);
-#endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 }
 
 inline int shutdown(socket_type s, int what, boost::system::error_code& ec)
 {
   clear_error(ec);
   int result = error_wrapper(::shutdown(s, what), ec);
-#if defined(BOOST_WINDOWS)
   if (result == 0)
     clear_error(ec);
-#endif
   return result;
 }
 
@@ -167,10 +160,8 @@
   clear_error(ec);
   int result = error_wrapper(call_connect(
         &msghdr::msg_namelen, s, addr, addrlen), ec);
-#if defined(BOOST_WINDOWS)
   if (result == 0)
     clear_error(ec);
-#endif
   return result;
 }
 
@@ -186,7 +177,10 @@
   return -1;
 #else
   clear_error(ec);
- return error_wrapper(::socketpair(af, type, protocol, sv), ec);
+ int result = error_wrapper(::socketpair(af, type, protocol, sv), ec);
+ if (result == 0)
+ clear_error(ec);
+ return result;
 #endif
 }
 
@@ -194,10 +188,8 @@
 {
   clear_error(ec);
   int result = error_wrapper(::listen(s, backlog), ec);
-#if defined(BOOST_WINDOWS)
   if (result == 0)
     clear_error(ec);
-#endif
   return result;
 }
 
@@ -281,7 +273,10 @@
   msghdr msg = msghdr();
   msg.msg_iov = bufs;
   msg.msg_iovlen = count;
- return error_wrapper(::recvmsg(s, &msg, flags), ec);
+ int result = error_wrapper(::recvmsg(s, &msg, flags), ec);
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 }
 
@@ -311,6 +306,8 @@
   msg.msg_iovlen = count;
   int result = error_wrapper(::recvmsg(s, &msg, flags), ec);
   *addrlen = msg.msg_namelen;
+ if (result >= 0)
+ clear_error(ec);
   return result;
 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 }
@@ -337,7 +334,10 @@
 #if defined(__linux__)
   flags |= MSG_NOSIGNAL;
 #endif // defined(__linux__)
- return error_wrapper(::sendmsg(s, &msg, flags), ec);
+ int result = error_wrapper(::sendmsg(s, &msg, flags), ec);
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 }
 
@@ -366,7 +366,10 @@
 #if defined(__linux__)
   flags |= MSG_NOSIGNAL;
 #endif // defined(__linux__)
- return error_wrapper(::sendmsg(s, &msg, flags), ec);
+ int result = error_wrapper(::sendmsg(s, &msg, flags), ec);
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 }
 
@@ -409,7 +412,10 @@
 
   return s;
 #else
- return error_wrapper(::socket(af, type, protocol), ec);
+ int s = error_wrapper(::socket(af, type, protocol), ec);
+ if (s >= 0)
+ clear_error(ec);
+ return s;
 #endif
 }
 
@@ -452,10 +458,8 @@
   clear_error(ec);
   int result = error_wrapper(call_setsockopt(&msghdr::msg_namelen,
         s, level, optname, optval, optlen), ec);
-# if defined(BOOST_WINDOWS)
   if (result == 0)
     clear_error(ec);
-# endif
   return result;
 #endif // defined(__BORLANDC__)
 }
@@ -544,6 +548,8 @@
     *static_cast<int*>(optval) /= 2;
   }
 #endif // defined(__linux__)
+ if (result == 0)
+ clear_error(ec);
   return result;
 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 }
@@ -564,10 +570,8 @@
   clear_error(ec);
   int result = error_wrapper(call_getpeername(
         &msghdr::msg_namelen, s, addr, addrlen), ec);
-#if defined(BOOST_WINDOWS)
   if (result == 0)
     clear_error(ec);
-#endif
   return result;
 }
 
@@ -587,10 +591,8 @@
   clear_error(ec);
   int result = error_wrapper(call_getsockname(
         &msghdr::msg_namelen, s, addr, addrlen), ec);
-#if defined(BOOST_WINDOWS)
   if (result == 0)
     clear_error(ec);
-#endif
   return result;
 }
 
@@ -600,12 +602,12 @@
   clear_error(ec);
 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
   int result = error_wrapper(::ioctlsocket(s, cmd, arg), ec);
- if (result == 0)
- clear_error(ec);
- return result;
 #else // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
- return error_wrapper(::ioctl(s, cmd, arg), ec);
+ int result = error_wrapper(::ioctl(s, cmd, arg), ec);
 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 }
 
 inline int select(int nfds, fd_set* readfds, fd_set* writefds,
@@ -643,10 +645,8 @@
 #else
   int result = error_wrapper(::select(nfds, readfds,
         writefds, exceptfds, timeout), ec);
-# if defined(BOOST_WINDOWS)
   if (result >= 0)
     clear_error(ec);
-# endif
   return result;
 #endif
 }
@@ -668,7 +668,10 @@
   fds.events = POLLIN;
   fds.revents = 0;
   clear_error(ec);
- return error_wrapper(::poll(&fds, 1, -1), ec);
+ int result = error_wrapper(::poll(&fds, 1, -1), ec);
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 }
 
@@ -689,7 +692,10 @@
   fds.events = POLLOUT;
   fds.revents = 0;
   clear_error(ec);
- return error_wrapper(::poll(&fds, 1, -1), ec);
+ int result = error_wrapper(::poll(&fds, 1, -1), ec);
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 }
 
@@ -713,7 +719,10 @@
   fds.events = POLLOUT;
   fds.revents = 0;
   clear_error(ec);
- return error_wrapper(::poll(&fds, 1, -1), ec);
+ int result = error_wrapper(::poll(&fds, 1, -1), ec);
+ if (result >= 0)
+ clear_error(ec);
+ return result;
 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk