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

Viktor Kutuzov vkutuzov at accesssoftek.com
Tue Jul 8 02:35:06 PDT 2014


Updated to reflect recent changes and to test ::environ for NULL.

http://reviews.llvm.org/D4229

Files:
  lib/sanitizer_common/sanitizer_linux.cc

Index: lib/sanitizer_common/sanitizer_linux.cc
===================================================================
--- lib/sanitizer_common/sanitizer_linux.cc
+++ 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 != NULL) {
+    uptr NameLen = internal_strlen(name);
+    for (char **Env = ::environ; *Env != NULL; Env++) {
+      if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
+        return (*Env) + NameLen + 1;
+    }
+  }
+  return NULL;  // Not found.
+#elif SANITIZER_LINUX
   static char *environ;
   static uptr len;
   static bool inited;
@@ -341,13 +351,11 @@
       return p + namelen + 1;  // point after =
     p = endp + 1;
   }
-  return 0;  // Not found.
-}
+  return NULL;  // Not found.
 #else
-const char *GetEnv(const char *name) {
   return getenv(name);
-}
 #endif
+}
 
 extern "C" {
   SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4229.11145.patch
Type: text/x-patch
Size: 1797 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140708/dba2be8a/attachment.bin>


More information about the llvm-commits mailing list