From 03430122a49ac37f18ab805ae20f64488a93e8c1 Mon Sep 17 00:00:00 2001 From: "Ignacio R. Morelle" Date: Fri, 12 Dec 2008 04:42:20 +0000 Subject: [PATCH] In version_info's string constructor, initialize the numbers vector... ...early enough to catch any border cases without getting to states that undefine the class' behavior. --- src/version.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/version.cpp b/src/version.cpp index f6ee085f592..6955dc98374 100644 --- a/src/version.cpp +++ b/src/version.cpp @@ -42,29 +42,32 @@ version_info::version_info(unsigned int major, unsigned int minor, unsigned int } version_info::version_info(const std::string& str) - : nums_() - ,special_("") + : nums_(3,0) + , special_("") , special_separator_('\0') , sane_(true) { + if(str.empty()) + return; + + // first two components are required to be valid numbers const std::vector string_parts = utils::split(str,'.'); - // first two components are required to be valid numbers, though - // only first component's existence is checked at all const size_t parts = string_parts.size(); if(parts == 0) return; + if(parts > 3) + nums_.resize(parts, 0); + try { size_t i = 0; - nums_.reserve(parts); // speed up insertion a bit (preallocate memory) - while(i < parts - 1) { - nums_.push_back( lexical_cast(string_parts[i]) ); - i++; - } + for(; i < parts-1; i++) + nums_[i] = lexical_cast(string_parts[i]); + + // Check for special suffix on last number and use it std::string numstr; - // Check for special suffix and use it this->init_special_version(string_parts[i], numstr); - nums_.push_back( lexical_cast(numstr) ); + nums_[i] = lexical_cast(numstr); } catch (bad_lexical_cast const&) { sane_ = false; @@ -72,9 +75,6 @@ version_info::version_info(const std::string& str) catch (std::out_of_range const&) { ; } - - if(nums_.size() < 3) nums_.resize(3,0); - } void version_info::init_special_version(const std::string& full_component, std::string& number_string)