Merge "Fix pthread_getattr_np for the main thread." into lmp-dev
diff --git a/libc/Android.mk b/libc/Android.mk
index 32c9fa6..5098171 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -1070,7 +1070,6 @@
 
 # Make sure that unwind.h comes from libunwind.
 LOCAL_C_INCLUDES := \
-    external/libunwind/include \
     $(libc_common_c_includes) \
 
 LOCAL_SRC_FILES := \
@@ -1086,7 +1085,9 @@
 
 LOCAL_SHARED_LIBRARIES := libc libdl
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
-LOCAL_WHOLE_STATIC_LIBRARIES := libunwindbacktrace
+# Only need this for arm since libc++ uses its own unwind code that
+# doesn't mix with the other default unwind code.
+LOCAL_STATIC_LIBRARIES_arm := libc++
 LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
 
 # Don't install on release build
diff --git a/linker/debugger.cpp b/linker/debugger.cpp
index 079682c..c316151 100644
--- a/linker/debugger.cpp
+++ b/linker/debugger.cpp
@@ -170,9 +170,9 @@
   if (info != NULL) {
     // For a rethrown signal, this si_code will be right and the one debuggerd shows will
     // always be SI_TKILL.
-    snprintf(code_desc, sizeof(code_desc), ", code %d", info->si_code);
+    __libc_format_buffer(code_desc, sizeof(code_desc), ", code %d", info->si_code);
     if (has_address) {
-      snprintf(addr_desc, sizeof(addr_desc), ", fault addr %p", info->si_addr);
+      __libc_format_buffer(addr_desc, sizeof(addr_desc), ", fault addr %p", info->si_addr);
     }
   }
   __libc_format_log(ANDROID_LOG_FATAL, "libc",
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 52eb56a..2d8e07e 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -866,7 +866,21 @@
 }
 
 void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
-  snprintf(buffer, buffer_size, "%s:%s", kDefaultLdPaths[0], kDefaultLdPaths[1]);
+  // Use basic string manipulation calls to avoid snprintf.
+  // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
+  // When debug malloc is enabled, this call returns 0. This in turn causes
+  // snprintf to do nothing, which causes libraries to fail to load.
+  // See b/17302493 for further details.
+  // Once the above bug is fixed, this code can be modified to use
+  // snprintf again.
+  size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
+  if (buffer_size < required_len) {
+    __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
+                 buffer_size, required_len);
+  }
+  char* end = stpcpy(buffer, kDefaultLdPaths[0]);
+  *end = ':';
+  strcpy(end + 1, kDefaultLdPaths[1]);
 }
 
 void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
diff --git a/tests/locale_test.cpp b/tests/locale_test.cpp
index 325f6ce..7ec607a 100644
--- a/tests/locale_test.cpp
+++ b/tests/locale_test.cpp
@@ -114,11 +114,12 @@
   locale_t cloc = newlocale(LC_ALL, "C", 0);
   locale_t cloc_utf8 = newlocale(LC_ALL, "C.UTF-8", 0);
 
-  uselocale(cloc);
+  locale_t old_locale = uselocale(cloc);
   ASSERT_EQ(1U, MB_CUR_MAX);
   uselocale(cloc_utf8);
   ASSERT_EQ(4U, MB_CUR_MAX);
 
+  uselocale(old_locale);
   freelocale(cloc);
   freelocale(cloc_utf8);
 }
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index bb86509..8c8c235 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -428,7 +428,7 @@
 
 TEST(stdio, snprintf_utf8_15439554) {
   locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
-  uselocale(cloc);
+  locale_t old_locale = uselocale(cloc);
 
   // http://b/15439554
   char buf[BUFSIZ];
@@ -446,6 +446,7 @@
   snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
   EXPECT_STREQ("1𤭢2", buf);
 
+  uselocale(old_locale);
   freelocale(cloc);
 }