diff --git a/src/memray/_memray/hooks.cpp b/src/memray/_memray/hooks.cpp index 6eed7d0ee1..00990e3b93 100644 --- a/src/memray/_memray/hooks.cpp +++ b/src/memray/_memray/hooks.cpp @@ -321,14 +321,13 @@ dlopen(const char* filename, int flag) noexcept if (dladdr(callerAddr, &info)) { const char* dlname = info.dli_fname; { - struct stat dlstat, exestat; - // Check fi we are being called from the main executable + // Check if we are being called from the main executable Dl_info main_info; void* main_sym = NULL; - void* self_handle = dlopen(nullptr, RTLD_LAZY | RTLD_NOLOAD); + void* self_handle = MEMRAY_ORIG(dlopen)(nullptr, RTLD_LAZY | RTLD_NOLOAD); if (self_handle) { main_sym = dlsym(self_handle, "main"); - dlclose(self_handle); + MEMRAY_ORIG(dlclose)(self_handle); } if (main_sym && dladdr(main_sym, &main_info) && strcmp(main_info.dli_fname, info.dli_fname) == 0) @@ -341,22 +340,26 @@ dlopen(const char* filename, int flag) noexcept if (caller != nullptr) { Dl_serinfo size; if (dlinfo(caller, RTLD_DI_SERINFOSIZE, &size) == 0) { - auto* paths = reinterpret_cast(new char[size.dls_size]); + std::vector paths_buf; + paths_buf.resize(size.dls_size); + auto paths = reinterpret_cast(&paths_buf[0]); *paths = size; if (dlinfo(caller, RTLD_DI_SERINFO, paths) == 0) { - for (unsigned i = 0; i != paths->dls_cnt; ++i) { + for (unsigned int i = 0; i != paths->dls_cnt; ++i) { const char* name = paths->dls_serpath[i].dls_name; std::string path; if (name == nullptr || name[0] == '\0') { // In the dynamic linking search path, an // empty entry typically represents the // current working directory ($PWD). - path = filename; - } - path = name; - if (path.back() != '/') { - path += '/'; + path = "./"; + } else { + path = name; + if (path.back() != '/') { + path += '/'; + } } + path += filename; ret = MEMRAY_ORIG(dlopen)(path.c_str(), flag); if (ret) { @@ -364,9 +367,8 @@ dlopen(const char* filename, int flag) noexcept } } } - delete[] reinterpret_cast(paths); } - dlclose(caller); + MEMRAY_ORIG(dlclose)(caller); } } }