Skip to content

Commit

Permalink
Merge pull request #9272 from frazze-jobb/frazze/kernel/prim_tty_unic…
Browse files Browse the repository at this point in the history
…ode_char_not_part_of_escape_sequence/OTP-19414

kernel: unicode character cannot be part of escape sequences
OTP-9272
  • Loading branch information
frazze-jobb authored Jan 27, 2025
2 parents e78831e + 8141530 commit 0ff9d32
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/kernel/src/prim_tty.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,10 @@ insert_buf(State, Bin, LineAcc, Acc) ->
State#state.buffer_expand =:= undefined ->
[PrevChar | BB] = State#state.buffer_before,
case string:next_grapheme([PrevChar | Bin]) of
[$\e | _] ->
%% Ansi escape sequences can never have unicode characters in them
%% so Char cannot be part of this cluster
insert_buf(State, Rest, [Char | LineAcc], Acc);
[PrevChar | _] ->
%% It was not part of the previous cluster, so just insert
%% it as a normal character
Expand Down
27 changes: 25 additions & 2 deletions lib/kernel/test/interactive_shell_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@
remsh_basic/1, remsh_error/1, remsh_longnames/1, remsh_no_epmd/1,
remsh_expand_compatibility_25/1, remsh_expand_compatibility_later_version/1,
external_editor/1, external_editor_visual/1,
external_editor_unicode/1, shell_ignore_pager_commands/1]).
external_editor_unicode/1, shell_ignore_pager_commands/1,
shell_escape_sequence_end_of_prompt_followed_by_unicode/1]).

-export([get_until/2]).

-export([test_invalid_keymap/1, test_valid_keymap/1]).
%% Exports for custom shell history module
-export([load/0, add/1]).
%% For custom prompt testing
-export([prompt/1]).
-export([prompt/1, prompt_2/1]).
-export([output_to_stdout_slowly/1]).
-record(tmux, {peer, node, name, ssh_server_name, orig_location }).
suite() ->
Expand Down Expand Up @@ -153,6 +154,7 @@ groups() ->
{tty_unicode,[parallel],
[{group,tty_tests},
shell_invalid_unicode,
shell_escape_sequence_end_of_prompt_followed_by_unicode,
external_editor_unicode
%% unicode wrapping does not work right yet
%% shell_unicode_wrap,
Expand Down Expand Up @@ -1407,6 +1409,27 @@ shell_help(Config) ->
stop_tty(Term),
ok
end.

prompt_2(_) ->
["\e[94m",54620,44397,50612,47,51312,49440,47568,"\e[0m"].
shell_escape_sequence_end_of_prompt_followed_by_unicode(Config) ->
%% If the prompt ended with an escape sequence, and the user input a unicode character
%% prim_tty tried to put that character in the escape sequence. This test checks that
%% the unicode character will not be part of the escape sequence.
Term = start_tty(Config),

try
send_tty(Term,"shell:prompt_func(\n"),
send_tty(Term,"{interactive_shell_SUITE,\n"),
send_tty(Term,"prompt_2}).\n"),
send_tty(Term,"÷.\n"),
check_content(Term, "syntax error before: '÷'"),
ok
after
stop_tty(Term),
ok
end.

%% Test the we can handle invalid ansi escape chars.
%% tmux cannot handle this... so we test this using to_erl
shell_invalid_ansi(_Config) ->
Expand Down

0 comments on commit 0ff9d32

Please sign in to comment.