summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2019-01-15 14:02:02 +0100
committerStephan Bergmann <sbergman@redhat.com>2019-01-15 17:52:11 +0100
commitcec518ca610022684e4ddf11cadab4d1eb6f3e11 (patch)
tree58172e05d969180966100e0b264d22e51197a0a0 /sal
parent0623861685dd1c9daa72a1002f5b018d5c5ef03c (diff)
Avoid unnecessary string copying in osl_getUserName
...while being careful to keep initializing *ustrName to an empty string when returning false (whether or not client code relies on that detail) Change-Id: Idd079a0b5a31b844287048dce57bf0fafcc32d1d Reviewed-on: https://gerrit.libreoffice.org/66388 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'sal')
-rw-r--r--sal/osl/unx/security.cxx35
1 files changed, 18 insertions, 17 deletions
diff --git a/sal/osl/unx/security.cxx b/sal/osl/unx/security.cxx
index 16f3b0ee561d..3ea5c96b7bdd 100644
--- a/sal/osl/unx/security.cxx
+++ b/sal/osl/unx/security.cxx
@@ -20,6 +20,7 @@
#include <sal/config.h>
#include <cstddef>
+#include <cstring>
#include <limits>
#ifdef IOS
@@ -52,7 +53,6 @@
#define getpwuid_r(uid, pwd, buf, buflen, result) (*(result) = getpwuid(uid), (*(result) ? (memcpy (buf, *(result), sizeof (struct passwd)), 0) : errno))
#endif
-static bool osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32 nMax);
static bool osl_psz_getHomeDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax);
static bool osl_psz_getConfigDir(oslSecurity Security, sal_Char* pszDirectory, sal_uInt32 nMax);
@@ -196,13 +196,26 @@ bool osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 n
sal_Bool SAL_CALL osl_getUserName(oslSecurity Security, rtl_uString **ustrName)
{
bool bRet = false;
- sal_Char pszName[1024];
+ sal_Char * pszName;
+ sal_Int32 len;
- pszName[0] = '\0';
+ oslSecurityImpl *pSecImpl = static_cast<oslSecurityImpl *>(Security);
- bRet = osl_psz_getUserName(Security,pszName,sizeof(pszName));
+ if (pSecImpl != nullptr && pSecImpl->m_pPasswd.pw_name != nullptr) {
+ pszName = pSecImpl->m_pPasswd.pw_name;
+ auto const n = std::strlen(pszName);
+ if (n <= sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
+ len = n;
+ bRet = true;
+ }
+ }
- rtl_string2UString( ustrName, pszName, rtl_str_getLength( pszName ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
+ if (!bRet) {
+ pszName = nullptr;
+ len = 0;
+ }
+
+ rtl_string2UString( ustrName, pszName, len, osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
SAL_WARN_IF(*ustrName == nullptr, "sal.osl", "ustrName == NULL");
return bRet;
@@ -213,18 +226,6 @@ sal_Bool SAL_CALL osl_getShortUserName(oslSecurity Security, rtl_uString **ustrN
return osl_getUserName(Security, ustrName); // No domain name on unix
}
-static bool osl_psz_getUserName(oslSecurity Security, sal_Char* pszName, sal_uInt32 nMax)
-{
- oslSecurityImpl *pSecImpl = static_cast<oslSecurityImpl *>(Security);
-
- if (pSecImpl == nullptr || pSecImpl->m_pPasswd.pw_name == nullptr)
- return false;
-
- strncpy(pszName, pSecImpl->m_pPasswd.pw_name, nMax);
-
- return true;
-}
-
sal_Bool SAL_CALL osl_getHomeDir(oslSecurity Security, rtl_uString **pustrDirectory)
{
bool bRet = false;