on master help sign in
Fix crash due to cycles when replaying macros (#2647)

In certain circumstances it was possible to get into an infinite loop when replaying macros such as when different macros attempt to replay each other. This commit adds changes to track which macros are currently being replayed and prevent getting into infinite loops.

commitb2bd87df81756d4925bf1f4da6962b9dd83a807c authorgavynriebau <gavyn.riebau@gmail.com> dateSun 5 Jun 2022, 18:49 (4 years ago) parent1c2aaf3
2 files changed+15
helix-term/src/commands.rs+13 helix-view/src/editor.rs+2
helix-term/src/commands.rs+13
@@ -4617,6 +4617,17 @@ fn record_macro(cx: &mut Context) {
46174617
46184618fn replay_macro(cx: &mut Context) {
46194619 let reg = cx.register.unwrap_or('@');
4620+
4621+ if cx.editor.macro_replaying.contains(&reg) {
4622+ cx.editor.set_error(format!(
4623+ "Cannot replay from register [{}] because already replaying from same register",
4624+ reg
4625+ ));
4626+ return;
4627+ }
4628+
4629+ cx.editor.macro_replaying.push(reg);
4630+
46204631 let keys: Vec<KeyEvent> = if let Some([keys_str]) = cx.editor.registers.read(reg) {
46214632 match helix_view::input::parse_macro(keys_str) {
46224633 Ok(keys) => keys,
@@ -4638,4 +4649,6 @@ fn replay_macro(cx: &mut Context) {
46384649 }
46394650 }
46404651 }));
4652+
4653+ cx.editor.macro_replaying.pop();
46414654}
helix-view/src/editor.rs+2
@@ -433,6 +433,7 @@ pub struct Editor {
433433 pub selected_register: Option<char>,
434434 pub registers: Registers,
435435 pub macro_recording: Option<(char, Vec<KeyEvent>)>,
436+ pub macro_replaying: Vec<char>,
436437 pub theme: Theme,
437438 pub language_servers: helix_lsp::Registry,
438439
@@ -503,6 +504,7 @@ impl Editor {
503504 count: None,
504505 selected_register: None,
505506 macro_recording: None,
507+ macro_replaying: Vec::new(),
506508 theme: theme_loader.default(),
507509 language_servers,
508510 debugger: None,