From b5e7cba598302d3d35ba8a3b735501b1be23b67f Mon Sep 17 00:00:00 2001 From: Emanuel Rylke Date: Sat, 6 Dec 2014 20:59:04 +0100 Subject: [PATCH] Fix bug of TextInput.adjust_horizontal causing stack overflow or wraparound When the edit_point is in the first position of a multiline TextInput adjust_horizontal(-1) moves the edit_point to the end of the first line. When the first line is empty this causes a stack overflow. When the edit_point is in the last position adjust_horizontal(1) causes a stack overflow. --- components/script/textinput.rs | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/components/script/textinput.rs b/components/script/textinput.rs index c925c0a5d82..d94ae6c4ee6 100644 --- a/components/script/textinput.rs +++ b/components/script/textinput.rs @@ -178,29 +178,20 @@ impl TextInput { /// adjusted vertically and the process repeats with the remaining adjustment requested. fn adjust_horizontal(&mut self, adjust: int) { if adjust < 0 { - if self.multiline { - let remaining = self.edit_point.index; - if adjust.abs() as uint > remaining { - self.edit_point.index = 0; - self.adjust_vertical(-1); - self.edit_point.index = self.current_line_length(); - self.adjust_horizontal(adjust + remaining as int); - } else { - self.edit_point.index = (self.edit_point.index as int + adjust) as uint; - } + let remaining = self.edit_point.index; + if adjust.abs() as uint > remaining && self.edit_point.line > 0 { + self.adjust_vertical(-1); + self.edit_point.index = self.current_line_length(); + self.adjust_horizontal(adjust + remaining as int); } else { self.edit_point.index = max(0, self.edit_point.index as int + adjust) as uint; } } else { - if self.multiline { - let remaining = self.current_line_length() - self.edit_point.index; - if adjust as uint > remaining { - self.edit_point.index = 0; - self.adjust_vertical(1); - self.adjust_horizontal(adjust - remaining as int); - } else { - self.edit_point.index += adjust as uint; - } + let remaining = self.current_line_length() - self.edit_point.index; + if adjust as uint > remaining && self.edit_point.line < self.lines.len() - 1 { + self.edit_point.index = 0; + self.adjust_vertical(1); + self.adjust_horizontal(adjust - remaining as int); } else { self.edit_point.index = min(self.current_line_length(), self.edit_point.index + adjust as uint);