From 8ee391bbd0610c376e914ddbf236dbd68d5f40cb Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 3 May 2021 22:03:47 +0200 Subject: [PATCH] tevent: Remove single-use ev_str_list_[length|add] This also adds proper error checks, the previous code could (very theoretically) have leaked memory if an intermediate _add had failed. Signed-off-by: Volker Lendecke Reviewed-by: Jeremy Allison --- lib/tevent/tevent.c | 18 ++++++++++++++++-- lib/tevent/tevent_util.c | 30 ------------------------------ lib/tevent/tevent_util.h | 2 -- 3 files changed, 16 insertions(+), 34 deletions(-) diff --git a/lib/tevent/tevent.c b/lib/tevent/tevent.c index dbec1821e41..a94bb440b9b 100644 --- a/lib/tevent/tevent.c +++ b/lib/tevent/tevent.c @@ -166,14 +166,28 @@ const char **tevent_backend_list(TALLOC_CTX *mem_ctx) { const char **list = NULL; struct tevent_ops_list *e; + size_t idx = 0; tevent_backend_init(); for (e=tevent_backends;e;e=e->next) { - list = ev_str_list_add(list, e->name); + idx += 1; } - talloc_steal(mem_ctx, list); + list = talloc_zero_array(mem_ctx, const char *, idx+1); + if (list == NULL) { + return NULL; + } + + idx = 0; + for (e=tevent_backends;e;e=e->next) { + list[idx] = talloc_strdup(list, e->name); + if (list[idx] == NULL) { + TALLOC_FREE(list); + return NULL; + } + idx += 1; + } return list; } diff --git a/lib/tevent/tevent_util.c b/lib/tevent/tevent_util.c index 16af8f3b908..7519e118ab9 100644 --- a/lib/tevent/tevent_util.c +++ b/lib/tevent/tevent_util.c @@ -29,36 +29,6 @@ #include "tevent_util.h" #include -/** - return the number of elements in a string list -*/ -size_t ev_str_list_length(const char **list) -{ - size_t ret; - for (ret=0;list && list[ret];ret++) /* noop */ ; - return ret; -} - -/** - add an entry to a string list -*/ -const char **ev_str_list_add(const char **list, const char *s) -{ - size_t len = ev_str_list_length(list); - const char **ret; - - ret = talloc_realloc(NULL, list, const char *, len+2); - if (ret == NULL) return NULL; - - ret[len] = talloc_strdup(ret, s); - if (ret[len] == NULL) return NULL; - - ret[len+1] = NULL; - - return ret; -} - - /** Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available, else diff --git a/lib/tevent/tevent_util.h b/lib/tevent/tevent_util.h index eef4a00f98b..128c231e25c 100644 --- a/lib/tevent/tevent_util.h +++ b/lib/tevent/tevent_util.h @@ -173,9 +173,7 @@ do { \ #endif /* _DLINKLIST_H */ -const char **ev_str_list_add(const char **list, const char *s); int ev_set_blocking(int fd, bool set); -size_t ev_str_list_length(const char **list); bool ev_set_close_on_exec(int fd); /* Defined here so we can build against older talloc versions that don't