message-params: add read length param reference to array read methods
authorIgor V. Kovalenko <igor.v.kovalenko@gmail.com>
Mon, 30 Nov 2020 19:39:23 +0000 (22:39 +0300)
committerTanu Kaskinen <tanuk@iki.fi>
Thu, 3 Dec 2020 14:41:39 +0000 (14:41 +0000)
Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/51>

src/pulse/message-params.c
src/pulse/message-params.h

index 7d2dd80..d5cafab 100644 (file)
@@ -344,22 +344,24 @@ int pa_message_params_read_bool(char *c, bool *result, void **state) {
     return PA_MESSAGE_PARAMS_OK;
 }
 
-/* Converts a parameter list to a string array. Escaping is removed from
- * the strings. Returns an array of pointers to sub-strings within c in
- * *results. The returned array must be freed, but not the strings
- * within the array. The function returns the number of strings in the
- * array. */
-int pa_message_params_read_string_array(char *c, const char ***results) {
+/* Converts a parameter list to a string array. */
+int pa_message_params_read_string_array(char *c, const char ***results, int *length) {
     void *state = NULL;
     uint32_t element_count, i;
     int err;
     const char **values;
 
     pa_assert(results);
+    pa_assert(length);
 
     /* Count elements, return if no element was found or parse error. */
-    if ((element_count = count_elements(c)) <= 0)
-        return element_count;
+    element_count = count_elements(c);
+    if (element_count < 0) {
+        return PA_MESSAGE_PARAMS_PARSE_ERROR;
+    } else if (element_count == 0) {
+        *length = 0;
+        return PA_MESSAGE_PARAMS_OK;
+    }
 
     /* Allocate array */
     values = pa_xmalloc0(element_count * sizeof(char *));
@@ -373,21 +375,29 @@ int pa_message_params_read_string_array(char *c, const char ***results) {
     }
 
     *results = values;
-    return element_count;
+    *length = element_count;
+
+    return PA_MESSAGE_PARAMS_OK;
 }
 
 /* Converts a parameter list to a double array. */
-int pa_message_params_read_double_array(char *c, double **results) {
+int pa_message_params_read_double_array(char *c, double **results, int *length) {
     double  *values;
     void *state = NULL;
     uint32_t element_count, i;
     int err;
 
     pa_assert(results);
+    pa_assert(length);
 
     /* Count elements, return if no element was found or parse error. */
-    if ((element_count = count_elements(c)) <= 0)
-        return element_count;
+    element_count = count_elements(c);
+    if (element_count < 0) {
+        return PA_MESSAGE_PARAMS_PARSE_ERROR;
+    } else if (element_count == 0) {
+        *length = 0;
+        return PA_MESSAGE_PARAMS_OK;
+    }
 
     /* Allocate array */
     values = pa_xmalloc0(element_count * sizeof(double));
@@ -401,21 +411,29 @@ int pa_message_params_read_double_array(char *c, double **results) {
     }
 
     *results = values;
-    return element_count;
+    *length = element_count;
+
+    return PA_MESSAGE_PARAMS_OK;
 }
 
 /* Converts a parameter list to an int64 array. */
-int pa_message_params_read_int64_array(char *c, int64_t **results) {
+int pa_message_params_read_int64_array(char *c, int64_t **results, int *length) {
     int64_t  *values;
     void *state = NULL;
     uint32_t element_count, i;
     int err;
 
     pa_assert(results);
+    pa_assert(length);
 
     /* Count elements, return if no element was found or parse error. */
-    if ((element_count = count_elements(c)) <= 0)
-        return element_count;
+    element_count = count_elements(c);
+    if (element_count < 0) {
+        return PA_MESSAGE_PARAMS_PARSE_ERROR;
+    } else if (element_count == 0) {
+        *length = 0;
+        return PA_MESSAGE_PARAMS_OK;
+    }
 
     /* Allocate array */
     values = pa_xmalloc0(element_count * sizeof(int64_t));
@@ -429,21 +447,29 @@ int pa_message_params_read_int64_array(char *c, int64_t **results) {
     }
 
     *results = values;
-    return element_count;
+    *length = element_count;
+
+    return PA_MESSAGE_PARAMS_OK;
 }
 
 /* Converts a parameter list to an uint64 array. */
-int pa_message_params_read_uint64_array(char *c, uint64_t **results) {
+int pa_message_params_read_uint64_array(char *c, uint64_t **results, int *length) {
     uint64_t  *values;
     void *state = NULL;
     uint32_t element_count, i;
     int err;
 
     pa_assert(results);
+    pa_assert(length);
 
     /* Count elements, return if no element was found or parse error. */
-    if ((element_count = count_elements(c)) <= 0)
-        return element_count;
+    element_count = count_elements(c);
+    if (element_count < 0) {
+        return PA_MESSAGE_PARAMS_PARSE_ERROR;
+    } else if (element_count == 0) {
+        *length = 0;
+        return PA_MESSAGE_PARAMS_OK;
+    }
 
     /* Allocate array */
     values = pa_xmalloc0(element_count * sizeof(uint64_t));
@@ -457,7 +483,9 @@ int pa_message_params_read_uint64_array(char *c, uint64_t **results) {
     }
 
     *results = values;
-    return element_count;
+    *length = element_count;
+
+    return PA_MESSAGE_PARAMS_OK;
 }
 
 /* Write functions. The functions are wrapper functions around pa_strbuf,
index 3d90eac..18addee 100644 (file)
@@ -75,17 +75,17 @@ int pa_message_params_read_bool(char *c, bool *result, void **state);
 int pa_message_params_read_double(char *c, double *result, void **state);
 
 /** Converts a parameter list to a double array. Empty elements in the parameter
- * list are treated as error. Before the call, results must be initialized, either
- * to NULL or to an array with default values. \since 15.0 */
-int pa_message_params_read_double_array(char *c, double **results);
+ * list are treated as error. Returns allocated array in *results and array size in *length.
+ * The returned array must be freed with pa_xfree(). \since 15.0 */
+int pa_message_params_read_double_array(char *c, double **results, int *length);
 
 /** Read an integer from parameter list in c. \since 15.0 */
 int pa_message_params_read_int64(char *c, int64_t *result, void **state);
 
 /** Converts a parameter list to an int64 array. Empty elements in the parameter
- * list are treated as error. Before the call, results must be initialized, either
- * to NULL or to an array with default values. \since 15.0 */
-int pa_message_params_read_int64_array(char *c, int64_t **results);
+ * list are treated as error. Returns allocated array in *results and array size in *length.
+ * The returned array must be freed with pa_xfree(). \since 15.0 */
+int pa_message_params_read_int64_array(char *c, int64_t **results, int *length);
 
 /** Read raw data from parameter list in c. Used to split a message parameter
  * string into list elements. The string returned in *result must not be freed.  \since 15.0 */
@@ -96,19 +96,18 @@ int pa_message_params_read_raw(char *c, char **result, void **state);
 int pa_message_params_read_string(char *c, const char **result, void **state);
 
 /** Convert a parameter list to a string array. Escaping is removed from
- * the strings. Returns an array of pointers to sub-strings within c in
- * *results. The returned array must be freed, but not the strings
- * within the array. Before the call, results must be initialized, either
- * to NULL or to an array with default values. \since 15.0 */
-int pa_message_params_read_string_array(char *c, const char ***results);
+ * the strings. Returns allocated array of pointers to sub-strings within c in
+ * *results and stores array size in *length. The returned array must be
+ * freed with pa_xfree(), but not the strings within the array. \since 15.0 */
+int pa_message_params_read_string_array(char *c, const char ***results, int *length);
 
 /** Read an unsigned integer from parameter list in c. \since 15.0 */
 int pa_message_params_read_uint64(char *c, uint64_t *result, void **state);
 
 /** Converts a parameter list to an uint64 array. Empty elements in the parameter
- * list are treated as error. Before the call, results must be initialized, either
- * to NULL or to an array with default values. \since 15.0 */
-int pa_message_params_read_uint64_array(char *c, uint64_t **results);
+ * list are treated as error. Returns allocated array in *results and array size in *length.
+ * The returned array must be freed with pa_xfree(). \since 15.0 */
+int pa_message_params_read_uint64_array(char *c, uint64_t **results, int *length);
 
 /** @} */