[PATCH] Fix getting environment variables for sanitizers needs on FreeBSD

Viktor Kutuzov vkutuzov at accesssoftek.com
Thu Jul 10 02:02:24 PDT 2014


Closed by commit rL212690 (authored by vkutuzov).

REPOSITORY
  rL LLVM

http://reviews.llvm.org/D4229

Files:
  compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc

Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
===================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
@@ -48,14 +48,14 @@
 #include <unistd.h>
 
 #if SANITIZER_FREEBSD
-#include <stdlib.h>  // for getenv
 #include <sys/sysctl.h>
 #include <machine/atomic.h>
 extern "C" {
 // <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
 // FreeBSD 9.2 and 10.0.
 #include <sys/umtx.h>
 }
+extern char **environ;  // provided by crt1
 #endif  // SANITIZER_FREEBSD
 
 #if !SANITIZER_ANDROID
@@ -315,10 +315,20 @@
   return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
 }
 
-#if SANITIZER_LINUX
-// Like getenv, but reads env directly from /proc and does not use libc.
-// This function should be called first inside __asan_init.
+// Like getenv, but reads env directly from /proc (on Linux) or parses the
+// 'environ' array (on FreeBSD) and does not use libc. This function should be
+// called first inside __asan_init.
 const char *GetEnv(const char *name) {
+#if SANITIZER_FREEBSD
+  if (::environ != 0) {
+    uptr NameLen = internal_strlen(name);
+    for (char **Env = ::environ; *Env != 0; Env++) {
+      if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
+        return (*Env) + NameLen + 1;
+    }
+  }
+  return 0;  // Not found.
+#elif SANITIZER_LINUX
   static char *environ;
   static uptr len;
   static bool inited;
@@ -342,12 +352,10 @@
     p = endp + 1;
   }
   return 0;  // Not found.
-}
 #else
-const char *GetEnv(const char *name) {
-  return getenv(name);
-}
+#error "Unsupported platform"
 #endif
+}
 
 extern "C" {
   SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4229.11246.patch
Type: text/x-patch
Size: 1793 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140710/c69dfa9a/attachment.bin>


More information about the llvm-commits mailing list