Fix the arithmetic overflow in Parser::to_linecol

This also changes the calculated line and column numbers. Without this
patch, if an error occurs at the end of a line, the returned line and
column numbers will point at the start of the next line.

After this patch, the line and column numbers will correctly point at
the end of the line where the actual parse error happened.
This commit is contained in:
Alex Gulyás 2015-08-14 14:54:32 +02:00
parent 0f0746396a
commit 64ae43a386

View file

@ -148,7 +148,7 @@ impl<'a> Parser<'a> {
pub fn to_linecol(&self, offset: usize) -> (usize, usize) { pub fn to_linecol(&self, offset: usize) -> (usize, usize) {
let mut cur = 0; let mut cur = 0;
for (i, line) in self.input.lines().enumerate() { for (i, line) in self.input.lines().enumerate() {
if cur + line.len() > offset { if cur + line.len() + 1 > offset {
return (i, offset - cur) return (i, offset - cur)
} }
cur += line.len() + 1; cur += line.len() + 1;