From ee09160000d99cd90e7c7137600c2f0e0087f991 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 8 Apr 2016 21:24:57 +0200 Subject: Prevent negative result of substraction of unsigned lengths Furthermore `LineAccumulator::getMissing` returning 0 if the line is longer than the maximum length is what its name implies. --- src/line_accumulator.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/line_accumulator.cc b/src/line_accumulator.cc index 31b8ddc..4398c50 100644 --- a/src/line_accumulator.cc +++ b/src/line_accumulator.cc @@ -68,20 +68,24 @@ LineAccumulator::~LineAccumulator() { } std::uint8_t LineAccumulator::getMissing() const { - return this->max_length_ - this->length_; + if ( this->length_ < this->max_length_ ) { + return this->max_length_ - this->length_; + } else { + return 0; + } } void LineAccumulator::operator()(const std::string& token) { const std::size_t tokenLength = getCharacterLength(token); - if ( ( this->length_ + tokenLength ) > this->max_length_ ) { + if ( tokenLength > this->getMissing() ) { this->discharge(true); } this->tokens_.emplace_back(token, 0); this->length_ += tokenLength; - if ( this->length_ < this->max_length_ ) { + if ( this->getMissing() > 0 ) { this->tokens_.back().second += 1; this->length_ += 1; } -- cgit v1.2.3