diff --git a/src/Client/ClientBase.h b/src/Client/ClientBase.h index 6c85d6a5f2be..2b15bfa7cacf 100644 --- a/src/Client/ClientBase.h +++ b/src/Client/ClientBase.h @@ -171,6 +171,11 @@ class ClientBase : public Poco::Util::Application, public IHints<2, ClientBase> void initTtyBuffer(ProgressOption progress); + /// Should be one of the first, to be destroyed the last, + /// since other members can use them. + SharedContextHolder shared_context; + ContextMutablePtr global_context; + bool is_interactive = false; /// Use either interactive line editing interface or batch mode. bool is_multiquery = false; bool delayed_interactive = false; @@ -208,9 +213,6 @@ class ClientBase : public Poco::Util::Application, public IHints<2, ClientBase> /// Settings specified via command line args Settings cmd_settings; - SharedContextHolder shared_context; - ContextMutablePtr global_context; - /// thread status should be destructed before shared context because it relies on process list. std::optional thread_status; diff --git a/tests/queries/0_stateless/02492_clickhouse_local_context_uaf.reference b/tests/queries/0_stateless/02492_clickhouse_local_context_uaf.reference new file mode 100644 index 000000000000..d00491fd7e5b --- /dev/null +++ b/tests/queries/0_stateless/02492_clickhouse_local_context_uaf.reference @@ -0,0 +1 @@ +1 diff --git a/tests/queries/0_stateless/02492_clickhouse_local_context_uaf.sh b/tests/queries/0_stateless/02492_clickhouse_local_context_uaf.sh new file mode 100755 index 000000000000..00e7a426fd00 --- /dev/null +++ b/tests/queries/0_stateless/02492_clickhouse_local_context_uaf.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CUR_DIR"/../shell_config.sh + +# This is the regression test for clickhouse-local, that may use already freed +# context from the suggestion thread on error. + +grep_options=( + -e ^$ + -e 'Cannot create file: /no/such/directory' + -e 'Cannot load data for command line suggestions:' + -e 'ClickHouse local version' +) + +ASAN_OPTIONS=$ASAN_OPTIONS:exitcode=3 $CLICKHOUSE_LOCAL --history_file /no/such/directory |& grep -v "${grep_options[@]}" +# on sanitizer error the code will be not 1, but 3 +echo $?