summaryrefslogtreecommitdiff
path: root/sal/osl/all
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2012-09-12 14:10:44 +0200
committerStephan Bergmann <sbergman@redhat.com>2012-09-12 14:18:10 +0200
commitd19c40f45dc8e8bcd9db4c6b83bdcf6367f6fbe7 (patch)
treea6fdb8854d31c4c654768bbe728af478b4bac122 /sal/osl/all
parent2738355f9f10d31ba942d4c2142e3c6dd20055d3 (diff)
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/osl/all')
-rw-r--r--sal/osl/all/log.cxx19
1 files changed, 15 insertions, 4 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 };