From 03fb543ba83013888adfc7500bed7e04636cc7ff Mon Sep 17 00:00:00 2001 From: firest Date: Thu, 24 Nov 2022 17:28:53 +0800 Subject: [PATCH 1/2] fix(ctl): fix unsafe `list_to_atom` A command can be called only when it is exists --- apps/emqx/src/emqx_ctl.erl | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/apps/emqx/src/emqx_ctl.erl b/apps/emqx/src/emqx_ctl.erl index 8e8477f7c..1087d9282 100644 --- a/apps/emqx/src/emqx_ctl.erl +++ b/apps/emqx/src/emqx_ctl.erl @@ -103,7 +103,13 @@ cast(Msg) -> gen_server:cast(?SERVER, Msg). run_command([]) -> run_command(help, []); run_command([Cmd | Args]) -> - run_command(list_to_atom(Cmd), Args). + case emqx_misc:safe_to_existing_atom(Cmd) of + {ok, Cmd1} -> + run_command(Cmd1, Args); + _ -> + help(), + {error, cmd_not_found} + end. -spec run_command(cmd(), list(string())) -> ok | {error, term()}. run_command(help, []) -> @@ -220,12 +226,13 @@ init([]) -> handle_call({register_command, Cmd, MF, Opts}, _From, State = #state{seq = Seq}) -> case ets:match(?CMD_TAB, {{'$1', Cmd}, '_', '_'}) of [] -> - ets:insert(?CMD_TAB, {{Seq, Cmd}, MF, Opts}); + ets:insert(?CMD_TAB, {{Seq, Cmd}, MF, Opts}), + {reply, ok, next_seq(State)}; [[OriginSeq] | _] -> ?SLOG(warning, #{msg => "CMD_overidden", cmd => Cmd, mf => MF}), - true = ets:insert(?CMD_TAB, {{OriginSeq, Cmd}, MF, Opts}) - end, - {reply, ok, next_seq(State)}; + true = ets:insert(?CMD_TAB, {{OriginSeq, Cmd}, MF, Opts}), + {reply, ok, State} + end; handle_call(Req, _From, State) -> ?SLOG(error, #{msg => "unexpected_call", call => Req}), {reply, ignored, State}. From 723959fe0df498d67cb568318f5d975fae49052a Mon Sep 17 00:00:00 2001 From: firest Date: Thu, 24 Nov 2022 17:49:35 +0800 Subject: [PATCH 2/2] chore: update changes --- changes/v5.0.12-en.md | 2 ++ changes/v5.0.12-zh.md | 3 +++ 2 files changed, 5 insertions(+) diff --git a/changes/v5.0.12-en.md b/changes/v5.0.12-en.md index 52af23519..e3217a89a 100644 --- a/changes/v5.0.12-en.md +++ b/changes/v5.0.12-en.md @@ -2,4 +2,6 @@ ## Enhancements +- Improve the CLI to avoid waste atom table when typing erros [#9416](https://github.com/emqx/emqx/pull/9416). + ## Bug fixes diff --git a/changes/v5.0.12-zh.md b/changes/v5.0.12-zh.md index 49f479a1e..0254619c4 100644 --- a/changes/v5.0.12-zh.md +++ b/changes/v5.0.12-zh.md @@ -3,3 +3,6 @@ ## 增强 ## 修复 + +- 优化命令行实现, 避免输入错误指令时, 产生不必要的原子表消耗 [#9416](https://github.com/emqx/emqx/pull/9416)。 +