From 8a9e379465d2f60cec5ff91811db1eb661e4c9e3 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Wed, 27 Oct 2021 20:40:07 +0200 Subject: [PATCH] string: Introduce arr_zalloc(). Adjust all callers which pass a product of two integers to zalloc() to call the new function instead and reduce zalloc() to a one-line wrapper. --- fecdec_filter.c | 2 +- play.c | 2 +- string.c | 55 +++++++++++++++++++++++++++++++++---------------- string.h | 1 + write.c | 2 +- 5 files changed, 41 insertions(+), 21 deletions(-) diff --git a/fecdec_filter.c b/fecdec_filter.c index a720a7a7..a3498e02 100644 --- a/fecdec_filter.c +++ b/fecdec_filter.c @@ -226,7 +226,7 @@ static int add_slice(char *buf, struct fecdec_group *fg) if (fg->num_slices == 0) { fg->num_slices = fg->h.slices_per_group; fg->idx = arr_alloc(fg->num_slices, sizeof(int)); - fg->data = zalloc(fg->num_slices * sizeof(unsigned char *)); + fg->data = arr_zalloc(fg->num_slices, sizeof(unsigned char *)); } r = fg->num_received_slices; /* Check if we already have this slice. */ diff --git a/play.c b/play.c index 92685013..69b463ac 100644 --- a/play.c +++ b/play.c @@ -1259,7 +1259,7 @@ int main(int argc, char *argv[]) session_open(); num_inputs = lls_num_inputs(play_lpr); init_shuffle_map(); - pt->invalid = zalloc(sizeof(*pt->invalid) * num_inputs); + pt->invalid = arr_zalloc(num_inputs, sizeof(*pt->invalid)); pt->rq = CRT_FILE_CHANGE; pt->playing = true; pt->task = task_register(&(struct task_info){ diff --git a/string.c b/string.c index f01c8392..7c8b2f00 100644 --- a/string.c +++ b/string.c @@ -62,6 +62,43 @@ __must_check __malloc void *arr_alloc(size_t nmemb, size_t size) return arr_realloc(NULL, nmemb, size); } +/** + * Allocate and initialize an array, abort on failure or bugs. + * + * \param nmemb See \ref arr_realloc(). + * \param size See \ref arr_realloc(). + * + * This calls \ref arr_alloc() and zeroes-out the array. + * + * \return See \ref arr_alloc(). + */ +__must_check __malloc void *arr_zalloc(size_t nmemb, size_t size) +{ + void *ptr = arr_alloc(nmemb, size); + + /* + * This multiplication can not overflow because the above call to \ref + * arr_alloc() aborts on overflow. + */ + memset(ptr, 0, nmemb * size); + return ptr; +} + +/** + * Allocate and initialize memory. + * + * \param size The desired new size. + * + * \return A pointer to the allocated and zeroed-out memory, which is suitably + * aligned for any kind of variable. + * + * \sa \ref alloc(), calloc(3). + */ +__must_check void *zalloc(size_t size) +{ + return arr_zalloc(1, size); +} + /** * Paraslash's version of realloc(). * @@ -98,24 +135,6 @@ __must_check __malloc void *alloc(size_t size) return arr_alloc(1, size); } -/** - * Allocate and initialize memory. - * - * \param size The desired new size. - * - * \return A pointer to the allocated and zeroed-out memory, which is suitably - * aligned for any kind of variable. - * - * \sa \ref alloc(), calloc(3). - */ -__must_check __malloc void *zalloc(size_t size) -{ - void *ret = alloc(size); - - memset(ret, 0, size); - return ret; -} - /** * Paraslash's version of strdup(). * diff --git a/string.h b/string.h index 3efe6878..ef7c3ee8 100644 --- a/string.h +++ b/string.h @@ -72,6 +72,7 @@ __must_check void *para_realloc(void *p, size_t size); __must_check __malloc void *alloc(size_t size); __must_check __malloc void *zalloc(size_t size); __must_check __malloc void *arr_alloc(size_t nmemb, size_t size); +__must_check __malloc void *arr_zalloc(size_t nmemb, size_t size); __must_check __malloc char *para_strdup(const char *s); __printf_2_0 unsigned xvasprintf(char **result, const char *fmt, va_list ap); diff --git a/write.c b/write.c index 9e0f22bd..356f35d4 100644 --- a/write.c +++ b/write.c @@ -89,7 +89,7 @@ static int setup_and_schedule(struct lls_parse_result *lpr) }, &s); n = writer_given? writer_given : 1; - wns = zalloc(n * sizeof(*wns)); + wns = arr_zalloc(n, sizeof(*wns)); for (i = 0; i < n; i++) { const char *arg = i < writer_given? lls_string_val(i, OPT_RESULT(WRITER, lpr)) : NULL; -- 2.39.2