From 7b19a06c9aa3e658eea4a87622ac74d67c10173f Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Fri, 24 May 2024 15:37:26 +0100 Subject: tstr: Constify the 'str' parameter to nxt_tstr_compile() This allows you to then define strings like static const nxt_str_t my_str = nxt_string("string"); Signed-off-by: Andrew Clayton --- src/nxt_tstr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nxt_tstr.c') diff --git a/src/nxt_tstr.c b/src/nxt_tstr.c index edf6860a..fde4822d 100644 --- a/src/nxt_tstr.c +++ b/src/nxt_tstr.c @@ -81,7 +81,7 @@ nxt_tstr_state_new(nxt_mp_t *mp, nxt_bool_t test) nxt_tstr_t * -nxt_tstr_compile(nxt_tstr_state_t *state, nxt_str_t *str, +nxt_tstr_compile(nxt_tstr_state_t *state, const nxt_str_t *str, nxt_tstr_flags_t flags) { u_char *p; -- cgit From 8e254a4d6762da22bc1967140ef85cf8fedfa02d Mon Sep 17 00:00:00 2001 From: Arjun Date: Tue, 25 Jun 2024 14:51:30 +0530 Subject: tstr, conf: Ensure error strings are nul-terminated This issue was found with oss-fuzz. ==18420==WARNING: MemorySanitizer: use-of-uninitialized-value #0 0x55dd798a5797 in nxt_vsprintf unit/src/nxt_sprintf.c:163:31 #1 0x55dd798d5bdb in nxt_conf_vldt_error unit/src/nxt_conf_validation.c:1525:11 #2 0x55dd798dd4cd in nxt_conf_vldt_var unit/src/nxt_conf_validation.c:1560:16 #3 0x55dd798dd4cd in nxt_conf_vldt_if unit/src/nxt_conf_validation.c:1592:16 #4 0x55dd798d55f4 in nxt_conf_vldt_object unit/src/nxt_conf_validation.c:2815:23 #5 0x55dd798d6f84 in nxt_conf_vldt_access_log unit/src/nxt_conf_validation.c:3426:11 #6 0x55dd798d55f4 in nxt_conf_vldt_object unit/src/nxt_conf_validation.c:2815:23 #7 0x55dd798d47bd in nxt_conf_validate unit/src/nxt_conf_validation.c:1421:11 #8 0x55dd79871c82 in LLVMFuzzerTestOneInput unit/fuzzing/nxt_json_fuzz.c:67:5 #9 0x55dd79770620 in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerLoop.cpp:614:13 #10 0x55dd7975adb4 in fuzzer::RunOneTest(fuzzer::Fuzzer*, char const*, unsigned long) /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerDriver.cpp:327:6 #11 0x55dd7976084a in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long)) /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerDriver.cpp:862:9 #12 0x55dd7978cc42 in main /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerMain.cpp:20:10 #13 0x7e8192213082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/libc-start.c:308:16 #14 0x55dd7975188d in _start Uninitialized value was created by an allocation of 'error.i' in the stack frame #0 0x55dd798dd42b in nxt_conf_vldt_var unit/src/nxt_conf_validation.c:1557:5 #1 0x55dd798dd42b in nxt_conf_vldt_if unit/src/nxt_conf_validation.c:1592:16 The issue was in nxt_tstr_test() where we create an error message with nxt_sprintf(), where this error message is then later used with the '%s' format specifier which expects a nul-terminated string, but by default nxt_sprintf() doesn't nul-terminate, you must use the '%Z' specifier to signify a '\0' at the end of the string. Signed-off-by: Arjun Co-developed-by: Zhidao HONG Signed-off-by: Zhidao HONG Link: Reviewed-by: Andrew Clayton [ Commit message/subject - Andrew ] Signed-off-by: Andrew Clayton --- src/nxt_tstr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nxt_tstr.c') diff --git a/src/nxt_tstr.c b/src/nxt_tstr.c index fde4822d..6f39cff2 100644 --- a/src/nxt_tstr.c +++ b/src/nxt_tstr.c @@ -159,7 +159,7 @@ nxt_tstr_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error) #else nxt_sprintf(error, error + NXT_MAX_ERROR_STR, "Unit is built without support of njs: " - "\"--njs\" ./configure option is missing."); + "\"--njs\" ./configure option is missing.%Z"); return NXT_ERROR; #endif -- cgit From 2eecee7520558a8f72e98e839a77330712a946b4 Mon Sep 17 00:00:00 2001 From: Zhidao HONG Date: Thu, 18 Apr 2024 18:16:01 +0800 Subject: var: Restrict nxt_tstr_query() to only support synchronous operation Initially, variable query was designed to accomodate both synchronous and asynchronous operations. However, upon consideration of actual requirements, we recognized that asynchronous support was not needed. The refactoring ensures that the success or failure of the variable query operation is now directly indicated by its return value. This change streamlines the function's usage and enhances code clarity, as it facilitates immediate error handling without the need for asynchronous callbacks or additional error checking functions. Note the patch only works for Unit native variables but not njs variables. --- src/nxt_tstr.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/nxt_tstr.c') diff --git a/src/nxt_tstr.c b/src/nxt_tstr.c index 6f39cff2..a72d7ddc 100644 --- a/src/nxt_tstr.c +++ b/src/nxt_tstr.c @@ -246,7 +246,7 @@ nxt_tstr_query_init(nxt_tstr_query_t **query_p, nxt_tstr_state_t *state, } -void +nxt_int_t nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, nxt_str_t *val) { @@ -254,11 +254,11 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, if (nxt_tstr_is_const(tstr)) { nxt_tstr_str(tstr, val); - return; + return NXT_OK; } if (nxt_slow_path(query->failed)) { - return; + return NXT_ERROR; } if (tstr->type == NXT_TSTR_VAR) { @@ -268,7 +268,7 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, if (nxt_slow_path(ret != NXT_OK)) { query->failed = 1; - return; + return NXT_ERROR; } } else { @@ -278,7 +278,7 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, if (nxt_slow_path(ret != NXT_OK)) { query->failed = 1; - return; + return NXT_ERROR; } #endif } @@ -294,6 +294,8 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, nxt_debug(task, "tstr query: \"%V\", result: \"%V\"", &str, val); #endif + + return NXT_OK; } -- cgit From 5f6ae1a189b1736eb7cc35f1ff8018fd8606db9b Mon Sep 17 00:00:00 2001 From: Zhidao HONG Date: Thu, 18 Apr 2024 18:34:07 +0800 Subject: var: Remove unused functions and structure fields --- src/nxt_tstr.c | 49 ------------------------------------------------- 1 file changed, 49 deletions(-) (limited to 'src/nxt_tstr.c') diff --git a/src/nxt_tstr.c b/src/nxt_tstr.c index a72d7ddc..a6d2e7ad 100644 --- a/src/nxt_tstr.c +++ b/src/nxt_tstr.c @@ -36,14 +36,8 @@ struct nxt_tstr_query_s { nxt_tstr_state_t *state; nxt_tstr_cache_t *cache; - nxt_uint_t waiting; - nxt_uint_t failed; /* 1 bit */ - void *ctx; void *data; - - nxt_work_handler_t ready; - nxt_work_handler_t error; }; @@ -257,17 +251,12 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, return NXT_OK; } - if (nxt_slow_path(query->failed)) { - return NXT_ERROR; - } - if (tstr->type == NXT_TSTR_VAR) { ret = nxt_var_interpreter(task, query->state, &query->cache->var, tstr->u.var, val, query->ctx, tstr->flags & NXT_TSTR_LOGGING); if (nxt_slow_path(ret != NXT_OK)) { - query->failed = 1; return NXT_ERROR; } @@ -277,7 +266,6 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, tstr->u.js, val, query->ctx); if (nxt_slow_path(ret != NXT_OK)) { - query->failed = 1; return NXT_ERROR; } #endif @@ -299,43 +287,6 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, } -nxt_bool_t -nxt_tstr_query_failed(nxt_tstr_query_t *query) -{ - return query->failed; -} - - -void -nxt_tstr_query_resolve(nxt_task_t *task, nxt_tstr_query_t *query, void *data, - nxt_work_handler_t ready, nxt_work_handler_t error) -{ - query->data = data; - query->ready = ready; - query->error = error; - - if (query->waiting == 0) { - nxt_work_queue_add(&task->thread->engine->fast_work_queue, - query->failed ? query->error : query->ready, - task, query->ctx, query->data); - } -} - - -void -nxt_tstr_query_handle(nxt_task_t *task, nxt_tstr_query_t *query, - nxt_bool_t failed) -{ - query->failed |= failed; - - if (--query->waiting == 0) { - nxt_work_queue_add(&task->thread->engine->fast_work_queue, - query->failed ? query->error : query->ready, - task, query->ctx, query->data); - } -} - - void nxt_tstr_query_release(nxt_tstr_query_t *query) { -- cgit From 82e168fe01306cf03f8eaba284f31f7285347424 Mon Sep 17 00:00:00 2001 From: Zhidao HONG Date: Fri, 16 Aug 2024 00:06:45 +0800 Subject: http: Refactor out nxt_tstr_cond_t from the access log module This nxt_tstr_cond_t will be reused for the feature of adding "if" option to the "match" object. The two "if" options have the same usage. --- src/nxt_tstr.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/nxt_tstr.c') diff --git a/src/nxt_tstr.c b/src/nxt_tstr.c index a6d2e7ad..50df4c47 100644 --- a/src/nxt_tstr.c +++ b/src/nxt_tstr.c @@ -196,6 +196,26 @@ nxt_tstr_state_release(nxt_tstr_state_t *state) } +nxt_int_t +nxt_tstr_cond_compile(nxt_tstr_state_t *state, nxt_str_t *str, + nxt_tstr_cond_t *cond) +{ + if (str->length > 0 && str->start[0] == '!') { + cond->negate = 1; + + str->start++; + str->length--; + } + + cond->expr = nxt_tstr_compile(state, str, 0); + if (nxt_slow_path(cond->expr == NULL)) { + return NXT_ERROR; + } + + return NXT_OK; +} + + nxt_bool_t nxt_tstr_is_const(nxt_tstr_t *tstr) { -- cgit