Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3889)

Unified Diff: base/version.cc

Issue 1364002: Fixed bug where an empty version string is considered valid. (Closed)
Patch Set: addressed comments Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/version.h ('k') | base/version_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/version.cc
diff --git a/base/version.cc b/base/version.cc
index 83dda50b62b25718ca56b0b423f854c18402a4cc..2271fcf33cced60bcef0dc71ee358382f154083d 100644
--- a/base/version.cc
+++ b/base/version.cc
@@ -2,10 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include <vector>
+#include "base/version.h"
+#include "base/logging.h"
#include "base/string_util.h"
-#include "base/version.h"
// static
Version* Version::GetVersionFromString(const std::wstring& version_str) {
@@ -17,38 +17,46 @@ Version* Version::GetVersionFromString(const std::wstring& version_str) {
// static
Version* Version::GetVersionFromString(const std::string& version_str) {
Version* vers = new Version();
- if (vers->InitFromString(version_str))
+ if (vers->InitFromString(version_str)) {
+ DCHECK(vers->is_valid_);
return vers;
+ }
delete vers;
return NULL;
}
+Version::Version() : is_valid_(false) {}
+
bool Version::Equals(const Version& that) const {
+ DCHECK(is_valid_);
+ DCHECK(that.is_valid_);
return CompareTo(that) == 0;
}
int Version::CompareTo(const Version& other) const {
- std::vector<uint16> other_components = other.components();
- size_t count = std::min(components_.size(), other_components.size());
+ DCHECK(is_valid_);
+ DCHECK(other.is_valid_);
+ size_t count = std::min(components_.size(), other.components_.size());
for (size_t i = 0; i < count; ++i) {
- if (components_[i] > other_components[i])
+ if (components_[i] > other.components_[i])
return 1;
- if (components_[i] < other_components[i])
+ if (components_[i] < other.components_[i])
return -1;
}
- if (components_.size() > other_components.size()) {
+ if (components_.size() > other.components_.size()) {
for (size_t i = count; i < components_.size(); ++i)
if (components_[i] > 0)
return 1;
- } else if (components_.size() < other_components.size()) {
- for (size_t i = count; i < other_components.size(); ++i)
- if (other_components[i] > 0)
+ } else if (components_.size() < other.components_.size()) {
+ for (size_t i = count; i < other.components_.size(); ++i)
+ if (other.components_[i] > 0)
return -1;
}
return 0;
}
const std::string Version::GetString() const {
+ DCHECK(is_valid_);
std::string version_str;
int count = components_.size();
for (int i = 0; i < count - 1; ++i) {
@@ -60,8 +68,11 @@ const std::string Version::GetString() const {
}
bool Version::InitFromString(const std::string& version_str) {
+ DCHECK(!is_valid_);
std::vector<std::string> numbers;
SplitString(version_str, '.', &numbers);
+ if (numbers.empty())
+ return false;
for (std::vector<std::string>::iterator i = numbers.begin();
i != numbers.end(); ++i) {
int num;
@@ -78,5 +89,6 @@ bool Version::InitFromString(const std::string& version_str) {
uint16 component = static_cast<uint16>(num);
components_.push_back(component);
}
+ is_valid_ = true;
return true;
}
« no previous file with comments | « base/version.h ('k') | base/version_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698