sal/osl/unx/socket.cxx |   79 ++++++++++++++++++++++++-------------------------
 1 file changed, 40 insertions(+), 39 deletions(-)

New commits:
commit eb83239ef26a8ddf77d82d1f6805c58ee366c172
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Fri Dec 20 11:07:40 2024 +0200
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Fri Dec 20 18:33:44 2024 +0100

    correct caching in osl_getLocalHostname*
    
    the change
    
        commit eac00017e34e77343b9ac3638bed9c75115a23fe
        Author: Samuel Mehrbrodt <samuel.mehrbr...@cib.de>
        Date:   Thu Dec 10 14:24:05 2020 +0100
        [API CHANGE] Do not call getaddrinfo if we just want the hostname
    
    means that the result returned from osl_getLocalHostname and
    osl_getLocalHostnameFQDN is always the same, but depends on which one is
    called first.
    
    Rather cache them independently, so they always return the right thing
    
    Change-Id: Ifa2f731a6002550cf08f57b53fd6e25e8b79295f
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178872
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>
    (cherry picked from commit 46533f9c308bb57545fd25527f3141daec0080d7)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178930
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/sal/osl/unx/socket.cxx b/sal/osl/unx/socket.cxx
index e875e415e709..2b3a877ec977 100644
--- a/sal/osl/unx/socket.cxx
+++ b/sal/osl/unx/socket.cxx
@@ -862,66 +862,67 @@ void SAL_CALL osl_destroyHostAddr (oslHostAddr pAddr)
 
 namespace
 {
-oslSocketResult lcl_getLocalHostname(rtl_uString **ustrLocalHostname, bool 
bUseFQDN)
+std::pair<oslSocketResult, OUString> lcl_getLocalHostname(bool bUseFQDN)
 {
-    static auto const init = [bUseFQDN]() -> std::pair<oslSocketResult, 
OUString> {
-            char LocalHostname[256] = "";
+    char LocalHostname[256] = "";
 
 #ifdef SYSV
-            struct utsname uts;
+    struct utsname uts;
 
-            if (uname(&uts) < 0)
-                return {osl_Socket_Error, OUString()};
+    if (uname(&uts) < 0)
+        return {osl_Socket_Error, OUString()};
 
-            if ((strlen(uts.nodename) + 1) > nBufLen)
-                return {osl_Socket_Error, OUString()};
+    if ((strlen(uts.nodename) + 1) > nBufLen)
+        return {osl_Socket_Error, OUString()};
 
-            strncpy(LocalHostname, uts.nodename, sizeof( LocalHostname ));
+    strncpy(LocalHostname, uts.nodename, sizeof( LocalHostname ));
 #else  /* BSD compatible */
-            if (gethostname(LocalHostname, sizeof(LocalHostname)-1) != 0)
-                return {osl_Socket_Error, OUString()};
+    if (gethostname(LocalHostname, sizeof(LocalHostname)-1) != 0)
+        return {osl_Socket_Error, OUString()};
 #endif /* SYSV */
-            LocalHostname[sizeof(LocalHostname)-1] = 0;
-
-            /* check if we have an FQDN */
-            if (bUseFQDN && strchr(LocalHostname, '.') == nullptr)
-            {
-                oslHostAddr Addr;
-
-                /* no, determine it via dns */
-                Addr = osl_psz_createHostAddrByName(LocalHostname);
+    LocalHostname[sizeof(LocalHostname)-1] = 0;
 
-                const char *pStr;
-                if ((pStr = osl_psz_getHostnameOfHostAddr(Addr)) != nullptr)
-                {
-                    strncpy(LocalHostname, pStr, sizeof( LocalHostname ));
-                    LocalHostname[sizeof(LocalHostname)-1] = 0;
-                }
-                osl_destroyHostAddr(Addr);
-            }
+    /* check if we have an FQDN */
+    if (bUseFQDN && strchr(LocalHostname, '.') == nullptr)
+    {
+        oslHostAddr Addr;
 
-            if (LocalHostname[0] != '
-            {
-                return {osl_Socket_Ok, 
OUString::createFromAscii(LocalHostname)};
-            }
+        /* no, determine it via dns */
+        Addr = osl_psz_createHostAddrByName(LocalHostname);
 
-            return {osl_Socket_Error, OUString()};
-        }();
+        const char *pStr;
+        if ((pStr = osl_psz_getHostnameOfHostAddr(Addr)) != nullptr)
+        {
+            strncpy(LocalHostname, pStr, sizeof( LocalHostname ));
+            LocalHostname[sizeof(LocalHostname)-1] = 0;
+        }
+        osl_destroyHostAddr(Addr);
+    }
 
-    rtl_uString_assign(ustrLocalHostname,init.second.pData);
+    if (LocalHostname[0] != '
+    {
+        return {osl_Socket_Ok, OUString::createFromAscii(LocalHostname)};
+    }
 
-    return init.first;
-}
+    return {osl_Socket_Error, OUString()};
 }
 
+} // anonymous namespace
+
 oslSocketResult SAL_CALL osl_getLocalHostname(rtl_uString **ustrLocalHostname)
 {
-    return lcl_getLocalHostname(ustrLocalHostname, false);
+    static auto const init = lcl_getLocalHostname(/*bUseFQDN*/false);
+
+    rtl_uString_assign(ustrLocalHostname,init.second.pData);
+    return init.first;
 }
 
 oslSocketResult osl_getLocalHostnameFQDN(rtl_uString **ustrLocalHostname)
 {
-    return lcl_getLocalHostname(ustrLocalHostname, true);
+    static auto const init = lcl_getLocalHostname(/*bUseFQDN*/true);
+
+    rtl_uString_assign(ustrLocalHostname,init.second.pData);
+    return init.first;
 }
 
 oslSocketAddr SAL_CALL osl_resolveHostname(rtl_uString *ustrHostname)

Reply via email to