Skip to content

Commit 0bfad0d

Browse files
committed
Fix alias infinite loop
1 parent 3010152 commit 0bfad0d

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/core.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ impl ShellCore {
236236

237237
let mut ans = false;
238238
let mut prev_head = "".to_string();
239+
let history = vec![word.clone()];
239240

240241
loop {
241242
let head = match word.replace("\n", " ").split(' ').nth(0) {
@@ -249,6 +250,9 @@ impl ShellCore {
249250

250251
if let Some(value) = self.aliases.get(&head) {
251252
*word = word.replacen(&head, value, 1);
253+
if history.contains(word) {
254+
return false;
255+
}
252256
ans = true;
253257
}
254258
prev_head = head;

src/elements/command/simple.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub struct SimpleCommand {
2424
substitutions_as_args: Vec<Substitution>,
2525
command_name: String,
2626
lineno: usize,
27+
continue_alias_check: bool,
2728
}
2829

2930

src/elements/command/simple/parser.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ impl SimpleCommand {
4444
ans.command_name = w.text.clone();
4545
}
4646

47-
if ans.words.is_empty() {
48-
if Self::set_alias(&w, &mut ans.words, &mut ans.text, core, feeder)? {
47+
if ans.words.is_empty() || ans.continue_alias_check {
48+
if ans.set_alias(&w, core, feeder)? {
4949
return Ok(true);
5050
}
5151
}
@@ -56,26 +56,29 @@ impl SimpleCommand {
5656
Ok(true)
5757
}
5858

59-
fn set_alias(word: &Word, words: &mut Vec<Word>, text: &mut String,
59+
fn set_alias(&mut self, word: &Word,
6060
core: &mut ShellCore, feeder: &mut Feeder) -> Result<bool, ParseError> {
61+
self.continue_alias_check = false;
6162
let mut w = word.text.clone();
6263
if ! core.replace_alias(&mut w) {
6364
return Ok(false);
6465
}
6566

67+
self.continue_alias_check = w.ends_with(" ");
68+
6669
let mut feeder_local = Feeder::new(&mut w);
6770
loop {
6871
match Word::parse(&mut feeder_local, core, None) {
6972
Ok(Some(w)) => {
70-
text.push_str(&w.text);
71-
words.push(w);
73+
self.text.push_str(&w.text);
74+
self.words.push(w);
7275
},
7376
_ => break,
7477
}
75-
command::eat_blank_with_comment(&mut feeder_local, core, text);
78+
command::eat_blank_with_comment(&mut feeder_local, core, &mut self.text);
7679
}
7780

78-
if words.is_empty() {
81+
if self.words.is_empty() {
7982
return Err(ParseError::WrongAlias(w));
8083
}
8184

0 commit comments

Comments
 (0)