[vlc-devel] [PATCH 1/2] modules: simplify vlc_module_match() error handling

RĂ©mi Denis-Courmont remi at remlab.net
Fri Sep 25 21:15:37 CEST 2020


Set the modules table to NULL on error, to simplify call sites.
---
 include/vlc_modules.h      | 2 +-
 modules/codec/avcodec/va.c | 4 +---
 src/modules/modules.c      | 5 +++--
 src/network/tls.c          | 6 ------
 4 files changed, 5 insertions(+), 12 deletions(-)

diff --git a/include/vlc_modules.h b/include/vlc_modules.h
index ade8135265..ec3c602934 100644
--- a/include/vlc_modules.h
+++ b/include/vlc_modules.h
@@ -46,7 +46,7 @@ struct vlc_logger;
  * \param names string of comma-separated requested module shortcut names
  * \param strict whether to exclude modules with no unmatching shortcut names
  * \param modules storage location for the base address of a sorted table
- *                of candidate modules [OUT]
+ *                of candidate modules (NULL on error) [OUT]
  * \param strict_matches storage location for the count of strictly matched
  *                       modules [OUT]
  * \return number of modules found or a strictly negative value on error
diff --git a/modules/codec/avcodec/va.c b/modules/codec/avcodec/va.c
index b9bfe7b101..f9497759e9 100644
--- a/modules/codec/avcodec/va.c
+++ b/modules/codec/avcodec/va.c
@@ -121,9 +121,7 @@ vlc_va_t *vlc_va_New(vlc_object_t *obj, AVCodecContext *avctx,
         }
     }
 
-    if (likely(total >= 0))
-        free(mods);
-
+    free(mods);
     vlc_object_delete(va);
     return NULL;
 }
diff --git a/src/modules/modules.c b/src/modules/modules.c
index 9c21fa2b74..531f96ad96 100644
--- a/src/modules/modules.c
+++ b/src/modules/modules.c
@@ -112,17 +112,18 @@ ssize_t vlc_module_match(const char *capability, const char *names,
     module_t **sorted = malloc(total * sizeof (*sorted));
     size_t matches = 0;
 
-    *modules = sorted;
-
     if (total > 0) {
         if (unlikely(unsorted == NULL || sorted == NULL)) {
             free(unsorted);
             free(sorted);
+            *modules = NULL;
             return -1;
         }
         memcpy(unsorted, tab, total * sizeof (*unsorted));
     }
 
+    *modules = sorted;
+
     /* Go through the list of module shortcut names. */
     while (names[0] != '\0') {
         const char *shortcut = names;
diff --git a/src/network/tls.c b/src/network/tls.c
index 27ec95e6f5..25f1c95bdc 100644
--- a/src/network/tls.c
+++ b/src/network/tls.c
@@ -61,9 +61,6 @@ vlc_tls_ServerCreate (vlc_object_t *obj, const char *cert_path,
     module_t **mods;
     ssize_t total = vlc_module_match("tls server", "any", false, &mods, NULL);
 
-    if (unlikely(total < 0))
-        return NULL;
-
     for (ssize_t i = 0; i < total; i++) {
         int (*probe)(vlc_tls_server_t *, const char *, const char *);
 
@@ -103,9 +100,6 @@ vlc_tls_client_t *vlc_tls_ClientCreate(vlc_object_t *obj)
     module_t **mods;
     ssize_t total = vlc_module_match("tls client", "any", false, &mods, NULL);
 
-    if (unlikely(total < 0))
-        return NULL;
-
     for (ssize_t i = 0; i < total; i++) {
         int (*probe)(vlc_tls_client_t *);
 
-- 
2.28.0



More information about the vlc-devel mailing list