You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by rp...@apache.org on 2010/08/20 16:59:16 UTC

svn commit: r987532 - in /apr/apr/branches/1.5.x: ./ network_io/unix/sockets.c test/testsock.c

Author: rpluem
Date: Fri Aug 20 14:59:15 2010
New Revision: 987532

URL: http://svn.apache.org/viewvc?rev=987532&view=rev
Log:
Merge r983618 from trunk:

* network_io/unix/sockets.c (apr_socket_connect): Copy the remote
  address by value rather than by reference.  This ensures that the
  sockaddr object returned by apr_socket_addr_get is allocated from
  the same pool as the socket object itself, as apr_socket_accept
  does; avoiding any potential lifetime mismatches.

* test/testsock.c (test_get_addr): Enhance test case to cover this.

PR: 49713

Submitted by: jorton
Reviewed by: rpluem

Modified:
    apr/apr/branches/1.5.x/   (props changed)
    apr/apr/branches/1.5.x/network_io/unix/sockets.c
    apr/apr/branches/1.5.x/test/testsock.c

Propchange: apr/apr/branches/1.5.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Fri Aug 20 14:59:15 2010
@@ -1 +1 @@
-/apr/apr/trunk:733052,747990,748361,748371,748565,748888,748902,748988,749810,760443,782838,783398,783958,784633,784773,788588,793192-793193,794118,794485,795267,799497,800627,809745,809854,810472,811455,813063,821306,829490,831641,835607,908427,910419,917819,917837-917838
+/apr/apr/trunk:733052,747990,748361,748371,748565,748888,748902,748988,749810,760443,782838,783398,783958,784633,784773,788588,793192-793193,794118,794485,795267,799497,800627,809745,809854,810472,811455,813063,821306,829490,831641,835607,908427,910419,917819,917837-917838,983618

Modified: apr/apr/branches/1.5.x/network_io/unix/sockets.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.5.x/network_io/unix/sockets.c?rev=987532&r1=987531&r2=987532&view=diff
==============================================================================
--- apr/apr/branches/1.5.x/network_io/unix/sockets.c (original)
+++ apr/apr/branches/1.5.x/network_io/unix/sockets.c Fri Aug 20 14:59:15 2010
@@ -343,10 +343,13 @@ apr_status_t apr_socket_connect(apr_sock
         /* A real remote address was passed in.  If the unspecified
          * address was used, the actual remote addr will have to be
          * determined using getpeername() if required. */
-        /* ### this should probably be a structure copy + fixup as per
-         * _accept()'s handling of local_addr */
-        sock->remote_addr = sa;
         sock->remote_addr_unknown = 0;
+
+        /* Copy the address structure details in. */
+        sock->remote_addr->sa = sa->sa;
+        sock->remote_addr->salen = sa->salen;
+        /* Adjust ipaddr_ptr et al. */
+        apr_sockaddr_vars_set(sock->remote_addr, sa->family, sa->port);
     }
 
     if (sock->local_addr->port == 0) {

Modified: apr/apr/branches/1.5.x/test/testsock.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.5.x/test/testsock.c?rev=987532&r1=987531&r2=987532&view=diff
==============================================================================
--- apr/apr/branches/1.5.x/test/testsock.c (original)
+++ apr/apr/branches/1.5.x/test/testsock.c Fri Aug 20 14:59:15 2010
@@ -328,8 +328,11 @@ static void test_get_addr(abts_case *tc,
     apr_status_t rv;
     apr_socket_t *ld, *sd, *cd;
     apr_sockaddr_t *sa, *ca;
+    apr_pool_t *subp;
     char *a, *b;
 
+    APR_ASSERT_SUCCESS(tc, "create subpool", apr_pool_create(&subp, p));
+
     ld = setup_socket(tc);
 
     APR_ASSERT_SUCCESS(tc,
@@ -337,7 +340,7 @@ static void test_get_addr(abts_case *tc,
                        apr_socket_addr_get(&sa, APR_LOCAL, ld));
 
     rv = apr_socket_create(&cd, sa->family, SOCK_STREAM,
-                           APR_PROTO_TCP, p);
+                           APR_PROTO_TCP, subp);
     APR_ASSERT_SUCCESS(tc, "create client socket", rv);
 
     APR_ASSERT_SUCCESS(tc, "enable non-block mode",
@@ -363,7 +366,7 @@ static void test_get_addr(abts_case *tc,
     }
 
     APR_ASSERT_SUCCESS(tc, "accept connection",
-                       apr_socket_accept(&sd, ld, p));
+                       apr_socket_accept(&sd, ld, subp));
     
     {
         /* wait for writability */
@@ -383,18 +386,38 @@ static void test_get_addr(abts_case *tc,
 
     APR_ASSERT_SUCCESS(tc, "get local address of server socket",
                        apr_socket_addr_get(&sa, APR_LOCAL, sd));
-
     APR_ASSERT_SUCCESS(tc, "get remote address of client socket",
                        apr_socket_addr_get(&ca, APR_REMOTE, cd));
-    
-    a = apr_psprintf(p, "%pI", sa);
-    b = apr_psprintf(p, "%pI", ca);
 
+    /* Test that the pool of the returned sockaddr objects exactly
+     * match the socket. */
+    ABTS_PTR_EQUAL(tc, subp, sa->pool);
+    ABTS_PTR_EQUAL(tc, subp, ca->pool);
+
+    /* Check equivalence. */
+    a = apr_psprintf(p, "%pI fam=%d", sa, sa->family);
+    b = apr_psprintf(p, "%pI fam=%d", ca, ca->family);
     ABTS_STR_EQUAL(tc, a, b);
+
+    /* Check pool of returned sockaddr, as above. */
+    APR_ASSERT_SUCCESS(tc, "get local address of client socket",
+                       apr_socket_addr_get(&sa, APR_LOCAL, cd));
+    APR_ASSERT_SUCCESS(tc, "get remote address of server socket",
+                       apr_socket_addr_get(&ca, APR_REMOTE, sd));
+
+    /* Check equivalence. */
+    a = apr_psprintf(p, "%pI fam=%d", sa, sa->family);
+    b = apr_psprintf(p, "%pI fam=%d", ca, ca->family);
+    ABTS_STR_EQUAL(tc, a, b);
+
+    ABTS_PTR_EQUAL(tc, subp, sa->pool);
+    ABTS_PTR_EQUAL(tc, subp, ca->pool);
                        
     apr_socket_close(cd);
     apr_socket_close(sd);
     apr_socket_close(ld);
+
+    apr_pool_destroy(subp);
 }
 
 abts_suite *testsock(abts_suite *suite)