Kaydet (Commit) d19c40f4 authored tarafından Stephan Bergmann's avatar Stephan Bergmann

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
üst 2738355f
......@@ -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 };
......
......@@ -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 */
......
......@@ -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
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment