[vlc-devel] [RFC 33/38] config/core: replaced usage of x{malloc, strdup}
Filip Roséen
filip at videolabs.io
Mon Jun 27 13:43:44 CEST 2016
Also cleaned up the implementation a little bit by increasing `n` by two
at the start, instead of having to deal with this type of "shift"
throughout the function.
---
src/config/core.c | 50 +++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 39 insertions(+), 11 deletions(-)
diff --git a/src/config/core.c b/src/config/core.c
index 67a3526..048db20 100644
--- a/src/config/core.c
+++ b/src/config/core.c
@@ -405,27 +405,55 @@ static ssize_t config_ListModules (const char *cap, char ***restrict values,
module_list_free (list);
return n;
}
+ else
+ n += 2; /* reserve two entities for catch-all entry */
+
+ char **vals = calloc( n, sizeof (*vals) );
+ char **txts = calloc( n, sizeof (*txts) );
- char **vals = xmalloc ((n + 2) * sizeof (*vals));
- char **txts = xmalloc ((n + 2) * sizeof (*txts));
+ if( unlikely( !vals || !txts ) )
+ goto error;
- vals[0] = xstrdup ("any");
- txts[0] = xstrdup (_("Automatic"));
+ vals[0] = strdup ("any");
+ txts[0] = strdup (_("Automatic"));
- for (ssize_t i = 0; i < n; i++)
+ for (ssize_t i = 1; i < n; i++)
{
- vals[i + 1] = xstrdup (module_get_object (list[i]));
- txts[i + 1] = xstrdup (module_gettext (list[i],
- module_get_name (list[i], true)));
+ vals[i] = strdup (module_get_object (list[i]));
+ txts[i] = strdup (module_gettext (list[i],
+ module_get_name (list[i], true)));
}
- vals[n + 1] = xstrdup ("none");
- txts[n + 1] = xstrdup (_("Disable"));
+ vals[n - 1] = strdup ("none");
+ txts[n - 1] = strdup (_("Disable"));
+
+ /* make sure we had enough memory for all entries */
+ for( ssize_t i = 0; i < n; ++i )
+ if( unlikely( !vals[i] || !txts[i] ) )
+ goto error;
+ /* populate out variables */
*values = vals;
*texts = txts;
module_list_free (list);
- return n + 2;
+ return n;
+
+error:
+ for( size_t i = 0; i < n; ++i )
+ {
+ if( vals ) free( vals[i] );
+ if( txts ) free( txts[i] );
+ }
+
+ free( txts );
+ free( vals );
+
+ *values = NULL;
+ *texts = NULL;
+
+ module_list_free (list);
+
+ return -1;
}
/**
--
2.9.0
More information about the vlc-devel
mailing list