From 90519437c075d0db66c9e8d6074f852b0150309f Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Fri, 6 Apr 2012 12:36:45 -0400 Subject: [PATCH] Fixed off-by-one error in btparser and llnlexer. --- source/lexer/llnlexer/llnlexer.cpp | 13 +++++++++---- source/parser/btparser/btparser.cpp | 11 ++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/source/lexer/llnlexer/llnlexer.cpp b/source/lexer/llnlexer/llnlexer.cpp index 553f303..9b26563 100644 --- a/source/lexer/llnlexer/llnlexer.cpp +++ b/source/lexer/llnlexer/llnlexer.cpp @@ -12,7 +12,7 @@ LLNLexer::~LLNLexer() void LLNLexer::consume(void) { cur_idx++; - if(cur_idx == la_buffer.size()) + if(cur_idx >= la_buffer.size()) { cur_idx = 0; la_buffer.clear(); @@ -37,8 +37,13 @@ void LLNLexer::match(char match) void LLNLexer::sync(unsigned int i) { unsigned int next_index = cur_idx + i - 1; - unsigned int max_index = (la_buffer.size() == 0) ? 0 : (la_buffer.size() - 1); - if( next_index >= max_index ) + unsigned int max_index = (la_buffer.size() - 1); + + if( la_buffer.size() == 0 ) + { + fill(i); + } + else if( next_index >= max_index ) { fill( next_index - max_index); } @@ -47,7 +52,7 @@ void LLNLexer::sync(unsigned int i) void LLNLexer::fill(unsigned int n) { unsigned int i = 0; - for (i = 0; i <= n; i++) + for (i = 0; i < n; i++) { la_buffer.push_back( in_stream.get() ); } diff --git a/source/parser/btparser/btparser.cpp b/source/parser/btparser/btparser.cpp index 3bf3e3c..4e3a8d5 100644 --- a/source/parser/btparser/btparser.cpp +++ b/source/parser/btparser/btparser.cpp @@ -25,8 +25,13 @@ void BTParser::consume(void) void BTParser::sync(unsigned int i) { unsigned int next_index = current + i - 1; - unsigned int max_index = (lookahead.size() == 0) ? 0 : (lookahead.size() - 1); - if( next_index >= max_index ) + unsigned int max_index = (lookahead.size() - 1); + + if( lookahead.size() == 0 ) + { + fill(i); + } + else if( next_index >= max_index ) { fill( next_index - max_index); } @@ -35,7 +40,7 @@ void BTParser::sync(unsigned int i) void BTParser::fill(unsigned int n) { unsigned int i = 0; - for (i = 0; i <= n; i++) + for (i = 0; i < n; i++) { lookahead.push_back( lexer->next() ); } -- 2.51.0