From f2f98510153f44dad2609e74e75e0d5f03ba6f6c Mon Sep 17 00:00:00 2001 From: nekobit Date: Sat, 21 May 2022 20:23:39 +0000 Subject: [PATCH] Cleanup search results FossilOrigin-Name: fb5fda3e58ff8ca0425cd82f4a0a49b115713c465ad0beccd606f45edc4ca486 --- include/mastodont_account.h | 1 + include/mastodont_search.h | 2 ++ src/account.c | 9 +++++++++ src/query.c | 3 +++ src/request.c | 2 ++ src/search.c | 8 ++++++++ 6 files changed, 25 insertions(+) diff --git a/include/mastodont_account.h b/include/mastodont_account.h index 459e189..cb7fcaa 100644 --- a/include/mastodont_account.h +++ b/include/mastodont_account.h @@ -107,5 +107,6 @@ void _mstdnt_val_malloc_account_call(cJSON* v, void* _type); // Cleanup void mstdnt_cleanup_account(struct mstdnt_account* acct); +void mstdnt_cleanup_accounts(struct mstdnt_account* accts, size_t len); #endif /* MASTODONT_ACCOUNT */ diff --git a/include/mastodont_search.h b/include/mastodont_search.h index 6ae3b4e..826fd26 100644 --- a/include/mastodont_search.h +++ b/include/mastodont_search.h @@ -60,4 +60,6 @@ int mastodont_search(mastodont_t* data, struct mstdnt_search_args* args, struct mstdnt_search_results* results); +void mstdnt_cleanup_search_results(struct mstdnt_search_results* res); + #endif /* MASTODONT_SEARCH_H */ diff --git a/src/account.c b/src/account.c index f650fab..c6f6771 100644 --- a/src/account.c +++ b/src/account.c @@ -187,5 +187,14 @@ MSTDNT_ACCOUNT_ACTION_FUNC_URL("unsubscribe") void mstdnt_cleanup_account(struct mstdnt_account* acct) { + if (!acct) return; cleanup_emojis(acct->emojis); } + +void mstdnt_cleanup_accounts(struct mstdnt_account* accts, size_t len) +{ + if (!accts) return; + for (int i = 0; i < len; ++i) + mstdnt_cleanup_account(accts + i); +} + diff --git a/src/query.c b/src/query.c index c2cd69d..a286483 100644 --- a/src/query.c +++ b/src/query.c @@ -107,6 +107,9 @@ char* _mstdnt_query_string(mastodont_t* data, val_ptr = escape_str; } + if (val_ptr == NULL) + break; + /* Get lengths */ key_len = strlen(key_ptr); val_len = strlen(val_ptr); diff --git a/src/request.c b/src/request.c index e14c916..9f91091 100644 --- a/src/request.c +++ b/src/request.c @@ -130,6 +130,8 @@ int mastodont_request(mastodont_t* data, struct mastodont_request_args* args) /* Optional */ if (args->callback) res = args->callback(storage->root, args->args); } + else + res = 1; cleanup_res: mastodont_fetch_results_cleanup(&results); diff --git a/src/search.c b/src/search.c index de07a9f..047ac85 100644 --- a/src/search.c +++ b/src/search.c @@ -93,3 +93,11 @@ int mastodont_search(mastodont_t* data, return mastodont_request(data, &req_args); } + +void mstdnt_cleanup_search_results(struct mstdnt_search_results* res) +{ + if (!res) return; + mstdnt_cleanup_accounts(res->accts, res->accts_len); + mstdnt_cleanup_statuses(res->statuses, res->statuses_len); + mstdnt_cleanup_tags(res->tags, res->tags_len); +}