diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2012-09-12 14:10:44 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2012-09-12 14:18:10 +0200 |
commit | d19c40f45dc8e8bcd9db4c6b83bdcf6367f6fbe7 (patch) | |
tree | a6fdb8854d31c4c654768bbe728af478b4bac122 /sal | |
parent | diagnose.c -> diagnose.cxx (to ease further modification) (diff) | |
download | core-d19c40f45dc8e8bcd9db4c6b83bdcf6367f6fbe7.tar.gz core-d19c40f45dc8e8bcd9db4c6b83bdcf6367f6fbe7.zip |
Work around some potential problems with thread-unsafe getenv
I have seen at least one failure of one of our unoapi tests where soffice.bin
crashed in getenv(3). This patch is just a drop in the ocean, though.
Change-Id: Iac8a2283b0a62e4fa95a0d063c1676af6c2390be
Diffstat (limited to 'sal')
-rw-r--r-- | sal/osl/all/log.cxx | 19 | ||||
-rw-r--r-- | sal/osl/unx/diagnose.cxx | 19 | ||||
-rw-r--r-- | sal/osl/unx/file.cxx | 28 | ||||
-rw-r--r-- | sal/osl/unx/file_url.cxx | 3 |
4 files changed, 42 insertions, 27 deletions
diff --git a/sal/osl/all/log.cxx b/sal/osl/all/log.cxx index bac0e9313919..934d88aa0a3e 100644 --- a/sal/osl/all/log.cxx +++ b/sal/osl/all/log.cxx @@ -38,6 +38,7 @@ #include <sstream> #include <stdio.h> // vsnprintf +#include <string.h> // strdup #include "osl/thread.hxx" #include "rtl/string.h" @@ -82,14 +83,24 @@ char const * toString(sal_detail_LogLevel level) { } } +// getenv is not thread safe, so minimize use of result: +char const * getEnvironmentVariable() { + char const * p1 = std::getenv("SAL_LOG"); + if (p1 == 0) { + return "+WARN"; + } + char const * p2 = strdup(p1); // leaked + if (p2 == 0) { + std::abort(); // cannot do much here + } + return p2; +} + bool report(sal_detail_LogLevel level, char const * area) { if (level == SAL_DETAIL_LOG_LEVEL_DEBUG) return true; assert(area != 0); - char const * env = std::getenv("SAL_LOG"); - if (env == 0) { - env = "+WARN"; - } + static char const * env = getEnvironmentVariable(); std::size_t areaLen = std::strlen(area); enum Sense { POSITIVE = 0, NEGATIVE = 1 }; std::size_t senseLen[2] = { 0, 1 }; diff --git a/sal/osl/unx/diagnose.cxx b/sal/osl/unx/diagnose.cxx index 38f11e07896b..8c289f23164a 100644 --- a/sal/osl/unx/diagnose.cxx +++ b/sal/osl/unx/diagnose.cxx @@ -209,6 +209,17 @@ static void osl_diagnose_backtrace_Impl (oslDebugMessageFunc f) /************************************************************************/ /* osl_assertFailedLine */ /************************************************************************/ + +namespace { + +// getenv is not thread safe, so minimize use of result: +bool isEnv(char const * name) { + char * p = getenv(name); + return p != NULL && *p != '\0'; +} + +} + sal_Bool SAL_CALL osl_assertFailedLine ( const sal_Char* pszFileName, sal_Int32 nLine, @@ -219,9 +230,9 @@ sal_Bool SAL_CALL osl_assertFailedLine ( // after reporting the assertion, abort if told so by SAL_DIAGNOSE_ABORT, but *not* if // assertions are routed to some external instance - char const * env = getenv( "SAL_DIAGNOSE_ABORT" ); - char const * envBacktrace = getenv( "SAL_DIAGNOSE_BACKTRACE" ); - sal_Bool const doAbort = ( ( env != NULL ) && ( *env != '\0' ) && ( f == NULL ) ); + static bool envAbort = isEnv( "SAL_DIAGNOSE_ABORT" ); + static bool envBacktrace = isEnv( "SAL_DIAGNOSE_BACKTRACE" ); + sal_Bool const doAbort = envAbort && f == NULL; /* If there's a callback for detailed messages, use it */ if ( g_pDetailedDebugMessageFunc != NULL ) @@ -251,7 +262,7 @@ sal_Bool SAL_CALL osl_assertFailedLine ( OSL_DIAGNOSE_OUTPUTMESSAGE(f, szMessage); /* should we output backtrace? */ - if( envBacktrace != NULL && *envBacktrace != '\0' ) + if( envBacktrace ) osl_diagnose_backtrace_Impl(f); /* release lock and leave */ diff --git a/sal/osl/unx/file.cxx b/sal/osl/unx/file.cxx index 536e1bf8a8b8..699cb311c7a9 100644 --- a/sal/osl/unx/file.cxx +++ b/sal/osl/unx/file.cxx @@ -831,27 +831,19 @@ static int osl_file_adjustLockFlags (const char * path, int flags) /**************************************************************************** * osl_file_queryLocking ***************************************************************************/ -struct Locking_Impl +static bool osl_file_queryLocking (sal_uInt32 uFlags) { - int m_enabled; - Locking_Impl() : m_enabled(0) +#if !defined HAVE_O_EXLOCK + if (!(uFlags & osl_File_OpenFlag_NoLock) + && ((uFlags & osl_File_OpenFlag_Write) + || (uFlags & osl_File_OpenFlag_Create))) { -#ifndef HAVE_O_EXLOCK - m_enabled = (getenv("SAL_ENABLE_FILE_LOCKING") != 0); -#endif /* HAVE_O_EXLOCK */ - } -}; -static int osl_file_queryLocking (sal_uInt32 uFlags) -{ - if (!(uFlags & osl_File_OpenFlag_NoLock)) - { - if ((uFlags & osl_File_OpenFlag_Write) || (uFlags & osl_File_OpenFlag_Create)) - { - static Locking_Impl g_locking; - return (g_locking.m_enabled != 0); - } + static bool enabled = getenv("SAL_ENABLE_FILE_LOCKING") != 0; + // getenv is not thread safe, so minimize use of result + return enabled; } - return 0; +#endif + return false; } #ifdef UNX diff --git a/sal/osl/unx/file_url.cxx b/sal/osl/unx/file_url.cxx index 7addb8e00d24..b58251577d43 100644 --- a/sal/osl/unx/file_url.cxx +++ b/sal/osl/unx/file_url.cxx @@ -633,7 +633,6 @@ oslFileError osl_getAbsoluteFileURL(rtl_uString* ustrBaseDirURL, rtl_uString* u { FileBase::RC rc; rtl::OUString unresolved_path; - static char *allow_symlinks = getenv( "SAL_ALLOW_LINKOO_SYMLINKS" ); rc = FileBase::getSystemPathFromFileURL(rtl::OUString(ustrRelativeURL), unresolved_path); @@ -656,6 +655,8 @@ oslFileError osl_getAbsoluteFileURL(rtl_uString* ustrBaseDirURL, rtl_uString* u rtl::OUString resolved_path; + static bool allow_symlinks = getenv("SAL_ALLOW_LINKOO_SYMLINKS") != 0; + // getenv is not thread safe, so minimize use of result if (!allow_symlinks) { rc = (FileBase::RC) osl_getAbsoluteFileURL_impl_(unresolved_path, resolved_path); |