From 67f5b16ce38ae6188d7c989b35b95f3ab4dc2b22 Mon Sep 17 00:00:00 2001 From: Adam Michalski Date: Mon, 12 Dec 2022 14:57:35 +0100 Subject: [PATCH] libsessiond: fix a memory leak in the `query_dir_via_readdir` routine `subsession_get_user_list` has non-obvious semantics. When it is called with the `user_list` parameter set to NULL, the caller is only interested in the number of subsessions associated with the `session_uid` user. In this case it is unnecessary and undesirable to allocate memory for the user list, as it leads to a memory leak, since we are not passing the pointer to the caller. Change-Id: Iac30507f6e3ac826ac66254b9404edbf17d95230 --- src/library/src/lib.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/library/src/lib.c b/src/library/src/lib.c index 4e3eda7..be98108 100644 --- a/src/library/src/lib.c +++ b/src/library/src/lib.c @@ -939,18 +939,19 @@ static int query_dir_via_readdir(DIR *const dir_ptr, subsession_user_t **const u if (!error_on_bad_user_id(entry_ptr->d_name)) ++elems; - *user_count = 1; - - subsession_user_t *const list = calloc(elems, sizeof *list); - if (list == NULL) - return SUBSESSION_ERROR_OUT_OF_MEMORY; if (user_list != NULL) { + subsession_user_t *const list = calloc(elems, sizeof *list); + if (list == NULL) + return SUBSESSION_ERROR_OUT_OF_MEMORY; + *user_list = list; - subsession_user_copy((*user_list)[0], SUBSESSION_INITIAL_SID); + subsession_user_copy(list[0], SUBSESSION_INITIAL_SID); } - if (elems == 1) + if (elems == 1) { + *user_count = 1; return SUBSESSION_ERROR_NONE; + } rewinddir(dir_ptr); -- 2.34.1