diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2019-05-30 10:42:20 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2019-05-30 10:42:20 +0000 |
commit | 424eb11e430403960384226bf8c2c0197f6a0f94 (patch) | |
tree | 0af87943636f1a46d120581b488355ed84405c41 /linker/linker.cpp | |
parent | c3c492965995a2bd5cfe12846f5038c4d7d2c91e (diff) | |
parent | d8bef67153c51984380cf25edaf1bb9710fcb000 (diff) |
Merge "Staticlly allocate string buffers for realpath_fd()"
Diffstat (limited to 'linker/linker.cpp')
-rw-r--r-- | linker/linker.cpp | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/linker/linker.cpp b/linker/linker.cpp index 32dce3805..7bc3529a0 100644 --- a/linker/linker.cpp +++ b/linker/linker.cpp @@ -393,16 +393,23 @@ static void parse_LD_LIBRARY_PATH(const char* path) { } static bool realpath_fd(int fd, std::string* realpath) { - std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX); - async_safe_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd); - if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) { + // proc_self_fd needs to be large enough to hold "/proc/self/fd/" plus an + // integer, plus the NULL terminator. + char proc_self_fd[32]; + // We want to statically allocate this large buffer so that we don't grow + // the stack by too much. + static char buf[PATH_MAX]; + + async_safe_format_buffer(proc_self_fd, sizeof(proc_self_fd), "/proc/self/fd/%d", fd); + auto length = readlink(proc_self_fd, buf, sizeof(buf)); + if (length == -1) { if (!is_first_stage_init()) { - PRINT("readlink(\"%s\") failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd); + PRINT("readlink(\"%s\") failed: %s [fd=%d]", proc_self_fd, strerror(errno), fd); } return false; } - *realpath = &buf[0]; + realpath->assign(buf, length); return true; } |