Browse Source

remember; tor_socket_errno has side effects!

svn:r2997
Nick Mathewson 21 years ago
parent
commit
f77ff938b7
4 changed files with 14 additions and 7 deletions
  1. 4 0
      src/common/compat.c
  2. 4 2
      src/or/buffers.c
  3. 3 2
      src/or/connection.c
  4. 3 3
      src/or/main.c

+ 4 - 0
src/common/compat.c

@@ -582,6 +582,10 @@ void tor_mutex_release(tor_mutex_t *m)
  * get your errors from WSAGetLastError, not errno.  (If you supply a
  * socket of -1, we check WSAGetLastError, but don't correct
  * WSAEWOULDBLOCKs.)
+ *
+ * The upshot of all of this is that when a socket call fails, you
+ * should call tor_socket_errno <em>at most once</em> on the failing
+ * socket to get the error.
  */
 #ifdef MS_WINDOWS
 int tor_socket_errno(int sock)

+ 4 - 2
src/or/buffers.c

@@ -184,7 +184,8 @@ int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof) {
 //  log_fn(LOG_DEBUG,"reading at most %d bytes.",at_most);
   read_result = recv(s, buf->mem+buf->datalen, at_most, 0);
   if (read_result < 0) {
-    if(!ERRNO_IS_EAGAIN(tor_socket_errno(s))) { /* it's a real error */
+    int e = tor_socket_errno(s);
+    if(!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
       return -1;
     }
     return 0; /* would block. */
@@ -254,7 +255,8 @@ int flush_buf(int s, buf_t *buf, size_t *buf_flushlen)
 
   write_result = send(s, buf->mem, *buf_flushlen, 0);
   if (write_result < 0) {
-    if(!ERRNO_IS_EAGAIN(tor_socket_errno(s))) { /* it's a real error */
+    int e = tor_socket_errno(s);
+    if(!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
       return -1;
     }
     log_fn(LOG_DEBUG,"write() would block, returning.");

+ 3 - 2
src/or/connection.c

@@ -538,10 +538,11 @@ int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_
   log_fn(LOG_DEBUG,"Connecting to %s:%u.",address,port);
 
   if(connect(s,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0) {
-    if(!ERRNO_IS_CONN_EINPROGRESS(tor_socket_errno(s))) {
+    int e = tor_socket_errno(s);
+    if(!ERRNO_IS_CONN_EINPROGRESS(e)) {
       /* yuck. kill it. */
       log_fn(LOG_INFO,"Connect() to %s:%u failed: %s",address,port,
-             tor_socket_strerror(tor_socket_errno(s)));
+             tor_socket_strerror(e));
       tor_close_socket(s);
       return -1;
     } else {

+ 3 - 3
src/or/main.c

@@ -869,11 +869,11 @@ static int do_main_loop(void) {
 
     /* let catch() handle things like ^c, and otherwise don't worry about it */
     if (poll_result < 0) {
+      int e = tor_socket_errno(-1);
       /* let the program survive things like ^z */
-      if(tor_socket_errno(-1) != EINTR) {
+      if(e != EINTR) {
         log_fn(LOG_ERR,"poll failed: %s [%d]",
-               tor_socket_strerror(tor_socket_errno(-1)),
-               tor_socket_errno(-1));
+               tor_socket_strerror(e), e);
         return -1;
       } else {
         log_fn(LOG_DEBUG,"poll interrupted.");