File 0195-kernel-unicode-character-cannot-be-part-of-escape-se.patch of Package erlang

From 8141530eeda0fa98a8aa3224bcdf508b25056829 Mon Sep 17 00:00:00 2001
From: Fredrik Frantzen <frazze@erlang.org>
Date: Wed, 8 Jan 2025 09:30:40 +0100
Subject: [PATCH] kernel: unicode character cannot be part of escape sequences

---
 lib/kernel/src/prim_tty.erl                 |  4 +++
 lib/kernel/test/interactive_shell_SUITE.erl | 27 +++++++++++++++++++--
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/lib/kernel/src/prim_tty.erl b/lib/kernel/src/prim_tty.erl
index 89d5d71aca..51b799788b 100644
--- a/lib/kernel/src/prim_tty.erl
+++ b/lib/kernel/src/prim_tty.erl
@@ -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
diff --git a/lib/kernel/test/interactive_shell_SUITE.erl b/lib/kernel/test/interactive_shell_SUITE.erl
index 981285a030..5158841efc 100644
--- a/lib/kernel/test/interactive_shell_SUITE.erl
+++ b/lib/kernel/test/interactive_shell_SUITE.erl
@@ -67,13 +67,14 @@
          remsh_dont_terminate_remote/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([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]).
 -record(tmux, {peer, node, name, orig_location }).
 suite() ->
     [{ct_hooks,[ts_install_cth]},
@@ -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,
@@ -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) ->
-- 
2.43.0

openSUSE Build Service is sponsored by