From 47db2aee98b08fdf516268f45a737994c7a80d04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Thu, 19 Sep 2019 13:00:17 +0200 Subject: [PATCH] Move Lib already loaded check before dlopen Call --- libr/util/lib.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/libr/util/lib.c b/libr/util/lib.c index 3921d448c4fc7..cc0955c5e33bb 100644 --- a/libr/util/lib.c +++ b/libr/util/lib.c @@ -239,6 +239,21 @@ R_API int r_lib_close(RLib *lib, const char *file) { return -1; } +static bool __already_loaded(RLib *lib, const char *file) { + const char *fileName = r_str_rstr (file, R_SYS_DIR); + RLibPlugin *p; + RListIter *iter; + if (fileName) { + r_list_foreach (lib->plugins, iter, p) { + const char *pFileName = r_str_rstr (p->file, R_SYS_DIR); + if (pFileName && !strcmp (fileName, pFileName)) { + return true; + } + } + } + return false; +} + R_API int r_lib_open(RLib *lib, const char *file) { /* ignored by filename */ if (!__lib_dl_check_filename (file)) { @@ -246,6 +261,11 @@ R_API int r_lib_open(RLib *lib, const char *file) { return -1; } + if (__already_loaded (lib, file)) { + eprintf("Not loading library because it has already been loaded from somewhere else: '%s'\n", file); + return -1; + } + void *handler = r_lib_dl_open (file); if (!handler) { IFDBG eprintf ("Cannot open library: '%s'\n", file); @@ -263,21 +283,6 @@ R_API int r_lib_open(RLib *lib, const char *file) { return r_lib_open_ptr (lib, file, handler, stru); } -static bool __alreadyLoaded(RLib *lib, const char *file) { - const char *fileName = r_str_rstr (file, R_SYS_DIR); - RLibPlugin *p; - RListIter *iter; - if (fileName) { - r_list_foreach (lib->plugins, iter, p) { - const char *pFileName = r_str_rstr (p->file, R_SYS_DIR); - if (pFileName && !strcmp (fileName, pFileName)) { - return true; - } - } - } - return false; -} - R_API int r_lib_open_ptr(RLib *lib, const char *file, void *handler, RLibStruct *stru) { r_return_val_if_fail (lib && file && stru, -1); if (stru->version) { @@ -287,9 +292,6 @@ R_API int r_lib_open_ptr(RLib *lib, const char *file, void *handler, RLibStruct return -1; } } - if (__alreadyLoaded (lib, file)) { - return -1; - } RLibPlugin *p = R_NEW0 (RLibPlugin); p->type = stru->type; p->data = stru->data;