From 09d8110ffd984fb7b469e0ab96ecea22581aa3f8 Mon Sep 17 00:00:00 2001 From: Shivani Bhardwaj Date: Mon, 12 Aug 2024 12:12:04 +0530 Subject: [PATCH] htp/table: only fetch element when needed It is unnecessary to always fetch the element as it is only needed when there is a match. Get the list item only when the key candidate matches the key parameter and the element needs to be returned. This will ensure lesser calls to htp_list_get fn. --- htp/htp_table.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htp/htp_table.c b/htp/htp_table.c index 535b961a..27321d5c 100644 --- a/htp/htp_table.c +++ b/htp/htp_table.c @@ -191,8 +191,8 @@ void *htp_table_get(const htp_table_t *table, const bstr *key) { // keys with the parameter, return data if found. for (size_t i = 0, n = htp_list_size(&table->list); i < n; i += 2) { bstr *key_candidate = htp_list_get(&table->list, i); - void *element = htp_list_get(&table->list, i + 1); if (bstr_cmp_nocase(key_candidate, key) == 0) { + void *element = htp_list_get(&table->list, i + 1); return element; } } @@ -207,8 +207,8 @@ void *htp_table_get_c(const htp_table_t *table, const char *ckey) { // keys with the parameter, return data if found. for (size_t i = 0, n = htp_list_size(&table->list); i < n; i += 2) { bstr *key_candidate = htp_list_get(&table->list, i); - void *element = htp_list_get(&table->list, i + 1); if (bstr_cmp_c_nocasenorzero(key_candidate, ckey) == 0) { + void *element = htp_list_get(&table->list, i + 1); return element; } } @@ -235,8 +235,8 @@ void *htp_table_get_mem(const htp_table_t *table, const void *key, size_t key_le // keys with the parameter, return data if found. for (size_t i = 0, n = htp_list_size(&table->list); i < n; i += 2) { bstr *key_candidate = htp_list_get(&table->list, i); - void *element = htp_list_get(&table->list, i + 1); if (bstr_cmp_mem_nocase(key_candidate, key, key_len) == 0) { + void *element = htp_list_get(&table->list, i + 1); return element; } }