Replace snprintf calls in linker.
When enabling debug malloc, the snprintf calls in the linker fails to
update the buffer.
The problem is that snprintf makes a call to pthread_getspecific that
returns a valid pointer, but the data it points to is zero. This should
never happen and causes the snprintf to stop and do nothing.
Temporarily replace snprintf with a different implementation to work
around this issue.
Bug: 16874447
Bug: 17302493
(cherry pick from commit 172955a4e30b88ce8239a7ef426b4e8903e9923c)
Change-Id: Idca9d417978403d61debfd0434aaa82fd770f33b
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 5f5823b..91ec49f 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -892,7 +892,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) {