sal/osl/unx/socket.cxx |   11 +++++++----
 sal/qa/osl/socket.cxx  |   22 +++++++++++++++++++---
 2 files changed, 26 insertions(+), 7 deletions(-)

New commits:
commit 1fef071c01caf6c293dd941ee7c8340e6894afc3
Author:     Stephan Bergmann <sberg...@redhat.com>
AuthorDate: Mon May 31 11:29:48 2021 +0200
Commit:     Stephan Bergmann <sberg...@redhat.com>
CommitDate: Mon May 31 15:22:33 2021 +0200

    Replace inet_addr with inet_pton
    
    ...as inet_addr is deprecated (it does not allow to distinguish successful
    return for "255.255.255.255" from -1 error return); and update tests
    
    Change-Id: I605cb2ba18fe9bd11d2d68c8f1c94271c4503509
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116441
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sberg...@redhat.com>

diff --git a/sal/osl/unx/socket.cxx b/sal/osl/unx/socket.cxx
index c78e43a9de93..cc1798e4eb1d 100644
--- a/sal/osl/unx/socket.cxx
+++ b/sal/osl/unx/socket.cxx
@@ -426,7 +426,10 @@ oslSocketAddr SAL_CALL osl_createInetBroadcastAddr (
             &pDottedAddr, strDottedAddr->buffer, strDottedAddr->length,
             RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
 
-        nAddr = inet_addr (pDottedAddr->buffer);
+        in_addr buf;
+        if (inet_pton (AF_INET, pDottedAddr->buffer, &buf) == 1) {
+            nAddr = buf.s_addr;
+        }
         rtl_string_release (pDottedAddr);
     }
 
@@ -494,11 +497,11 @@ oslSocketAddr osl_psz_createInetSocketAddr (
     sal_Int32       Port)
 {
     oslSocketAddr pAddr = nullptr;
-    sal_Int32 Addr = inet_addr(pszDottedAddr);
-    if(Addr != -1)
+    in_addr buf;
+    if(inet_pton(AF_INET, pszDottedAddr, &buf) == 1)
     {
         /* valid dotted addr */
-        pAddr = createSocketAddrWithFamily( osl_Socket_FamilyInet, htons(Port) 
, Addr );
+        pAddr = createSocketAddrWithFamily( osl_Socket_FamilyInet, htons(Port) 
, buf.s_addr );
     }
     return pAddr;
 }
diff --git a/sal/qa/osl/socket.cxx b/sal/qa/osl/socket.cxx
index dab2621f2293..813702f4ca21 100644
--- a/sal/qa/osl/socket.cxx
+++ b/sal/qa/osl/socket.cxx
@@ -19,18 +19,34 @@ namespace
 class SocketTest : public CppUnit::TestFixture
 {
     CPPUNIT_TEST_SUITE(SocketTest);
-    CPPUNIT_TEST(test_getDottedInetAddrOfSocketAddr);
+    CPPUNIT_TEST(test_createInetSocketAddr);
+    CPPUNIT_TEST(test_createInetBroadcastAddr);
     CPPUNIT_TEST_SUITE_END();
 
-    void test_getDottedInetAddrOfSocketAddr()
+    void test_createInetSocketAddr()
     {
         OUString const in("123.4.56.78");
-        auto const addr = osl_createInetSocketAddr(in.pData, 0);
+        auto const addr = osl_createInetSocketAddr(in.pData, 100);
         CPPUNIT_ASSERT(addr != nullptr);
+        CPPUNIT_ASSERT_EQUAL(osl_Socket_FamilyInet, 
osl_getFamilyOfSocketAddr(addr));
         OUString out;
         auto const res = osl_getDottedInetAddrOfSocketAddr(addr, &out.pData);
         CPPUNIT_ASSERT_EQUAL(osl_Socket_Ok, res);
         CPPUNIT_ASSERT_EQUAL(in, out);
+        CPPUNIT_ASSERT_EQUAL(sal_Int32(100), 
osl_getInetPortOfSocketAddr(addr));
+    }
+
+    void test_createInetBroadcastAddr()
+    {
+        OUString const in("123.4.56.78");
+        auto const addr = osl_createInetBroadcastAddr(in.pData, 100);
+        CPPUNIT_ASSERT(addr != nullptr);
+        CPPUNIT_ASSERT_EQUAL(osl_Socket_FamilyInet, 
osl_getFamilyOfSocketAddr(addr));
+        OUString out;
+        auto const res = osl_getDottedInetAddrOfSocketAddr(addr, &out.pData);
+        CPPUNIT_ASSERT_EQUAL(osl_Socket_Ok, res);
+        CPPUNIT_ASSERT_EQUAL(OUString("123.255.255.255"), out);
+        CPPUNIT_ASSERT_EQUAL(sal_Int32(100), 
osl_getInetPortOfSocketAddr(addr));
     }
 };
 
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to