diff --git a/src/sip/sip_utils.h b/src/sip/sip_utils.h
index 78608ea050f61022798c6bd03769c6c3ea7269f4..0df0f97201a47fb9993c9fa7031d375c5dd1af0f 100644
--- a/src/sip/sip_utils.h
+++ b/src/sip/sip_utils.h
@@ -131,6 +131,12 @@ CONST_PJ_STR(const std::string_view& str) noexcept
     return {const_cast<char*>(str.data()), (pj_ssize_t) str.size()};
 }
 
+inline constexpr std::string_view
+as_view(const pj_str_t& str) noexcept
+{
+    return {str.ptr, (size_t) str.slen};
+}
+
 // PJSIP dialog locking in RAII way
 // Usage: declare local variable like this: sip_utils::PJDialogLock lock {dialog};
 // The lock is kept until the local variable is deleted
diff --git a/src/sip/sipaccount.cpp b/src/sip/sipaccount.cpp
index 7d9298b7f45c8ed8a911f929e4e3bb6049f9bc26..6affb9688fd2c8469910b582d8ef3d0b988216ad 100644
--- a/src/sip/sipaccount.cpp
+++ b/src/sip/sipaccount.cpp
@@ -1434,7 +1434,9 @@ SIPAccount::getFromUri() const
 {
     std::string scheme;
     std::string transport;
-    std::string username(username_);
+
+    // Get login name if username is not specified
+    std::string username(username_.empty() ? getLoginName() : username_);
     std::string hostname(hostname_);
 
     // UDP does not require the transport specification
@@ -1444,18 +1446,15 @@ SIPAccount::getFromUri() const
     } else
         scheme = "sip:";
 
-    // Get login name if username is not specified
-    if (username_.empty())
-        username = getLoginName();
-
     // Get machine hostname if not provided
-    if (hostname_.empty())
-        hostname = std::string(pj_gethostname()->ptr, pj_gethostname()->slen);
+    if (hostname_.empty()) {
+        hostname = sip_utils::as_view(*pj_gethostname());
+    }
 
     if (IpAddr::isIpv6(hostname))
         hostname = IpAddr(hostname).toString(false, true);
 
-    const std::string uri = "<" + scheme + username + "@" + hostname + transport + ">";
+    std::string uri = "<" + scheme + username + "@" + hostname + transport + ">";
     if (not displayName_.empty())
         return "\"" + displayName_ + "\" " + uri;
     return uri;
diff --git a/src/sip/sipvoiplink.cpp b/src/sip/sipvoiplink.cpp
index 73bb067a00f8d1e611b487e42e5cb482ff02afc6..2168b09607fe288f490b013a00e31092e3b690cd 100644
--- a/src/sip/sipvoiplink.cpp
+++ b/src/sip/sipvoiplink.cpp
@@ -1412,8 +1412,7 @@ SIPVoIPLink::findLocalAddressFromTransport(pjsip_transport* transport,
     port = pjsip_transport_get_default_port_for_type(transportType);
 
     // Initialize the sip address with the hostname
-    const auto pjMachineName = pj_gethostname();
-    addr = std::string(pjMachineName->ptr, pjMachineName->slen);
+    addr = sip_utils::as_view(*pj_gethostname());
 
     // Update address and port with active transport
     RETURN_IF_NULL(transport,
@@ -1441,7 +1440,7 @@ SIPVoIPLink::findLocalAddressFromTransport(pjsip_transport* transport,
     }
 
     // Update local address based on the transport type
-    addr = std::string(param.ret_addr.ptr, param.ret_addr.slen);
+    addr = sip_utils::as_view(param.ret_addr);
 
     // Determine the local port based on transport information
     port = param.ret_port;