[Libreoffice-commits] core.git: sal/osl

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Thu Feb 7 15:24:47 UTC 2019


 sal/osl/w32/socket.cxx |   86 ++++++++++++++++++++++++++-----------------------
 1 file changed, 46 insertions(+), 40 deletions(-)

New commits:
commit cb22fb0c2e49010ed066c23f0521d10cc850c280
Author:     Stephan Bergmann <sbergman at redhat.com>
AuthorDate: Thu Feb 7 15:36:58 2019 +0100
Commit:     Stephan Bergmann <sbergman at redhat.com>
CommitDate: Thu Feb 7 16:24:23 2019 +0100

    Make osl_getLocalHostname thread-safe
    
    Change-Id: I82b8c49fcbbec161bf968573e28992fa5737b45b
    Reviewed-on: https://gerrit.libreoffice.org/67508
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sbergman at redhat.com>

diff --git a/sal/osl/w32/socket.cxx b/sal/osl/w32/socket.cxx
index 013bf895866e..f8624738e59f 100644
--- a/sal/osl/w32/socket.cxx
+++ b/sal/osl/w32/socket.cxx
@@ -17,6 +17,10 @@
  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
  */
 
+#include <sal/config.h>
+
+#include <utility>
+
 #include "system.h"
 
 #include <osl/socket.h>
@@ -596,57 +600,59 @@ void SAL_CALL osl_destroyHostAddr(oslHostAddr pAddr)
 
 oslSocketResult SAL_CALL osl_getLocalHostname (rtl_uString **strLocalHostname)
 {
-    static sal_Unicode LocalHostname[256] = {0};
+    static auto const init = []() -> std::pair<oslSocketResult, OUString> {
+            sal_Unicode LocalHostname[256] = {0};
 
-    if (rtl_ustr_getLength(LocalHostname) == 0)
-    {
-        sal_Char Host[256]= "";
-        if (gethostname(Host, sizeof(Host)) == 0)
-        {
-            /* check if we have an FQDN; if not, try to determine it via dns first: */
-            if (strchr(Host, '.') == nullptr)
+            sal_Char Host[256]= "";
+            if (gethostname(Host, sizeof(Host)) == 0)
             {
-                oslHostAddr pAddr;
-                rtl_uString     *hostName= nullptr;
+                /* check if we have an FQDN; if not, try to determine it via dns first: */
+                if (strchr(Host, '.') == nullptr)
+                {
+                    oslHostAddr pAddr;
+                    rtl_uString     *hostName= nullptr;
 
-                rtl_string2UString(
-                    &hostName, Host, strlen(Host),
-                    RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
-                OSL_ASSERT(hostName != nullptr);
+                    rtl_string2UString(
+                        &hostName, Host, strlen(Host),
+                        RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
+                    OSL_ASSERT(hostName != nullptr);
 
-                pAddr = osl_createHostAddrByName(hostName);
-                rtl_uString_release (hostName);
+                    pAddr = osl_createHostAddrByName(hostName);
+                    rtl_uString_release (hostName);
 
-                if (pAddr && pAddr->pHostName)
-                    memcpy(LocalHostname, pAddr->pHostName->buffer, sizeof(sal_Unicode)*(rtl_ustr_getLength(pAddr->pHostName->buffer)+1));
-                else
-                    memset(LocalHostname, 0, sizeof(LocalHostname));
+                    if (pAddr && pAddr->pHostName)
+                        memcpy(LocalHostname, pAddr->pHostName->buffer, sizeof(sal_Unicode)*(rtl_ustr_getLength(pAddr->pHostName->buffer)+1));
+                    else
+                        memset(LocalHostname, 0, sizeof(LocalHostname));
 
-                osl_destroyHostAddr (pAddr);
-            }
-            if (LocalHostname[0] == u'\0')
-            {
-                OUString u;
-                if (rtl_convertStringToUString(
-                        &u.pData, Host, strlen(Host), osl_getThreadTextEncoding(),
-                        (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
-                         | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
-                         | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))
-                    && u.getLength() < SAL_N_ELEMENTS(LocalHostname))
+                    osl_destroyHostAddr (pAddr);
+                }
+                if (LocalHostname[0] == u'\0')
                 {
-                    memcpy(LocalHostname, u.getStr(), (u.getLength() + 1) * sizeof sal_Unicode);
+                    OUString u;
+                    if (rtl_convertStringToUString(
+                            &u.pData, Host, strlen(Host), osl_getThreadTextEncoding(),
+                            (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
+                             | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
+                             | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))
+                        && u.getLength() < SAL_N_ELEMENTS(LocalHostname))
+                    {
+                        memcpy(LocalHostname, u.getStr(), (u.getLength() + 1) * sizeof sal_Unicode);
+                    }
                 }
             }
-        }
-    }
 
-    if (rtl_ustr_getLength(LocalHostname) > 0)
-    {
-        rtl_uString_newFromStr (strLocalHostname, LocalHostname);
-        return osl_Socket_Ok;
-    }
+            if (rtl_ustr_getLength(LocalHostname) > 0)
+            {
+                return {osl_Socket_Ok, LocalHostname};
+            }
 
-    return osl_Socket_Error;
+            return {osl_Socket_Error, OUString()};
+        }();
+
+    rtl_uString_assign (strLocalHostname, init.second.pData);
+
+    return init.first;
 }
 
 oslSocketAddr SAL_CALL osl_resolveHostname(rtl_uString* strHostname)


More information about the Libreoffice-commits mailing list