From 6935033db55115f452b1807b49950f0803982f02 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 12 Nov 2021 02:02:05 -0800 Subject: [PATCH] Prevent dangling pointers --- native/jni/zygisk/hook.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/native/jni/zygisk/hook.cpp b/native/jni/zygisk/hook.cpp index 2a541eda5..0959d9385 100644 --- a/native/jni/zygisk/hook.cpp +++ b/native/jni/zygisk/hook.cpp @@ -331,6 +331,11 @@ void ZygiskModule::setOption(zygisk::Option opt) { void HookContext::run_modules_pre(const vector &fds) { char buf[256]; + + // Since we directly use the pointer to elements in the vector, in order to prevent dangling + // pointers, the vector has to be pre-allocated to ensure reallocation does not occur + modules.reserve(fds.size()); + for (int i = 0; i < fds.size(); ++i) { snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fds[i]); if (void *h = dlopen(buf, RTLD_LAZY)) {