From f81f88ae569c6724da18868d1abe92036b895fc6 Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Fri, 26 Jan 2024 23:18:51 -0600 Subject: Limit results --- src/clog.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src/clog.c') diff --git a/src/clog.c b/src/clog.c index 04f5bb2..c9aab60 100644 --- a/src/clog.c +++ b/src/clog.c @@ -23,6 +23,10 @@ KORE_SECCOMP_FILTER("clog", KORE_SYSCALL_ALLOW(uname) ) +#define STR_HELPER(s) #s +#define STR(s) STR_HELPER(s) +#define MAX_ENTRIES 15 + enum query_status { QUERY_STATUS_OK, QUERY_STATUS_ERROR, QUERY_STATUS_NOT_FOUND }; struct entry { @@ -102,8 +106,7 @@ void entry_query_init(struct entry_query *eq, const char *id, const char *query) eq->query = NULL; } - // FIXME: define MAX_ENTRIES and LIMIT queries with it. - eq->entries = kore_malloc(sizeof(struct entry *) * 100); + eq->entries = kore_malloc(sizeof(struct entry *) * MAX_ENTRIES); eq->num_entries = 0; eq->status = QUERY_STATUS_OK; @@ -142,8 +145,9 @@ void entry_query_cleanup(struct entry_query *eq) { int validate_uuid(const char *uuid) { size_t i = 0; - if (strlen(uuid) != 36) + if (strlen(uuid) != 36) { return KORE_RESULT_ERROR; + } for (i = 0; i <= 36; i++) { if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) { @@ -535,15 +539,21 @@ int sql_select(struct entry_query *eq) { ); } else if (eq->query) { + // Search for entries. err = kore_pgsql_query_params(&sql, q_search_entries, 0, // return string data - 2, // param count - KORE_PGSQL_PARAM_TEXT(eq->query), - KORE_PGSQL_PARAM_TEXT(eq->query) + 3, // param count + KORE_PGSQL_PARAM_TEXT(eq->query), KORE_PGSQL_PARAM_TEXT(eq->query), + KORE_PGSQL_PARAM_TEXT(STR(MAX_ENTRIES)) ); } - else { // Query for index. - err = kore_pgsql_query(&sql, q_select_entries); + else { + // Query for index. + err = kore_pgsql_query_params(&sql, q_select_entries, + 0, // return string data + 1, // param count + KORE_PGSQL_PARAM_TEXT(STR(MAX_ENTRIES)) + ); } if (err == KORE_RESULT_ERROR) { -- cgit v1.2.3