From 8a1abe398167ed497225fcffd89445fabf58a6dd Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Tue, 12 May 2020 10:52:03 +0200
Subject: [PATCH] Corrections in reg_pick

---
 src/config.c | 22 ++++++-------------
 src/reg.c    | 61 ++++++++++++++++++++++++++--------------------------
 2 files changed, 37 insertions(+), 46 deletions(-)

diff --git a/src/config.c b/src/config.c
index 8d31fa4a..7fd842ed 100644
--- a/src/config.c
+++ b/src/config.c
@@ -4,6 +4,8 @@
 #include <lektor/config.h>
 #include <lektor/database.h>
 #include <lektor/net.h>
+#include <lektor/reg.h>
+
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
@@ -121,23 +123,13 @@ load_so(const char *const mod_path, const char *const mod_init, void *mod, struc
        Uppon successfull completion, the function shall return 0, and return
        a non zero value if something bad happened. */
     int (*module_set_function)(void *const, struct lkt_state *, void *const);
-    char *error;
-    void *handle = dlopen(mod_path, RTLD_NOW);
+    void *handle = NULL;
+    *(void **) (&module_set_function) = reg_pick(mod_path, &handle, mod_init);
 
-    if (NULL == handle) {
-        LOG_ERROR("libdl error in dlopen on file '%s': %s", mod_path, dlerror());
+    if (module_set_function)
+        return ! module_set_function(mod, srv, handle);
+    else
         return 1;
-    }
-
-    dlerror();
-    *(void **) (&module_set_function) = dlsym(handle, mod_init);
-
-    if ((error = dlerror()) != NULL) {
-        LOG_ERROR("libdl error in dlsym on file '%s': %s\n", mod_path, error);
-        return 1;
-    }
-
-    return ! module_set_function(mod, srv, handle);
 }
 
 inline int
diff --git a/src/reg.c b/src/reg.c
index e1fcf303..c19c0391 100644
--- a/src/reg.c
+++ b/src/reg.c
@@ -12,47 +12,46 @@ struct module_reg *reg;
 void *
 reg_pick(const char *file, void **handle, const char *symbol)
 {
+    int i;
     if (!symbol)
         return NULL;
 
+    /* Use the register instead of dlfcn? */
+    if (!file || STR_MATCH(file, "STATIC") || handle == NULL)
+        goto use_reg;
+
     /* Use dlsym */
-    if (handle) {
-        if (!file)
-            return NULL;
-        char *error;
-        *handle = dlopen(file, RTLD_NOW);
-
-        if (NULL == *handle) {
-            LOG_ERROR("libdl error in dlopen on file '%s': %s", file, dlerror());
-            return NULL;
-        }
-
-        dlerror();
-        void *sym = dlsym(handle, symbol);
-
-        if ((error = dlerror()) != NULL) {
-            LOG_ERROR("libdl error in dlsym on file '%s': %s\n", file, error);
-            return NULL;
-        }
-
-        return sym;
+    char *error;
+    *handle = dlopen(file, RTLD_NOW);
+
+    if (NULL == *handle) {
+        LOG_ERROR("libdl error in dlopen on file '%s': %s", file, dlerror());
+        return NULL;
     }
 
-    /* Read the reg */
-    else {
-        int i;
-        for (i = 0; reg[i].name && reg[i].func && !STR_MATCH(reg[i].name, symbol); ++i)
-            continue;
-
-        if (reg[i].func)
-            return (void *) reg[i].func;
-        else
-            return NULL;
+    dlerror();
+    void *sym = dlsym(*handle, symbol);
+
+    if ((error = dlerror()) != NULL) {
+        LOG_ERROR("libdl error in dlsym on file '%s': %s\n", file, error);
+        return NULL;
     }
+
+    return sym;
+
+    /* Read the reg */
+use_reg:
+    for (i = 0; reg[i].name && reg[i].func && !STR_MATCH(reg[i].name, symbol); ++i)
+        continue;
+
+    if (reg[i].func)
+        return (void *) reg[i].func;
+    else
+        return NULL;
 }
 
 void
-reg_set(struct module_reg * _reg)
+reg_set(struct module_reg *_reg)
 {
     reg = _reg;
 }
-- 
GitLab