summaryrefslogtreecommitdiffstats
path: root/otautil/rangeset.cpp
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2017-11-04 08:08:08 +0100
committerTao Bao <tbao@google.com>2017-11-07 21:50:02 +0100
commit6798315327690fdbe93add15159be5b925779bfe (patch)
tree521c5e1d88928db38f3a7aaa8e3dea76e307e78d /otautil/rangeset.cpp
parentMerge "Clean up fuse_sideload and add a testcase." (diff)
downloadandroid_bootable_recovery-6798315327690fdbe93add15159be5b925779bfe.tar
android_bootable_recovery-6798315327690fdbe93add15159be5b925779bfe.tar.gz
android_bootable_recovery-6798315327690fdbe93add15159be5b925779bfe.tar.bz2
android_bootable_recovery-6798315327690fdbe93add15159be5b925779bfe.tar.lz
android_bootable_recovery-6798315327690fdbe93add15159be5b925779bfe.tar.xz
android_bootable_recovery-6798315327690fdbe93add15159be5b925779bfe.tar.zst
android_bootable_recovery-6798315327690fdbe93add15159be5b925779bfe.zip
Diffstat (limited to 'otautil/rangeset.cpp')
-rw-r--r--otautil/rangeset.cpp85
1 files changed, 56 insertions, 29 deletions
diff --git a/otautil/rangeset.cpp b/otautil/rangeset.cpp
index a121a4efc..532cba4a8 100644
--- a/otautil/rangeset.cpp
+++ b/otautil/rangeset.cpp
@@ -16,8 +16,10 @@
#include "otautil/rangeset.h"
+#include <limits.h>
#include <stddef.h>
+#include <algorithm>
#include <string>
#include <utility>
#include <vector>
@@ -28,47 +30,79 @@
#include <android-base/strings.h>
RangeSet::RangeSet(std::vector<Range>&& pairs) {
- CHECK_NE(pairs.size(), static_cast<size_t>(0)) << "Invalid number of tokens";
+ blocks_ = 0;
+ if (pairs.empty()) {
+ LOG(ERROR) << "Invalid number of tokens";
+ return;
+ }
- // Sanity check the input.
- size_t result = 0;
for (const auto& range : pairs) {
- CHECK_LT(range.first, range.second) << "Empty or negative range: " << range.first << ", "
- << range.second;
- size_t sz = range.second - range.first;
- CHECK_LE(result, SIZE_MAX - sz) << "RangeSet size overflow";
- result += sz;
+ if (!PushBack(range)) {
+ Clear();
+ return;
+ }
}
-
- ranges_ = pairs;
- blocks_ = result;
}
RangeSet RangeSet::Parse(const std::string& range_text) {
std::vector<std::string> pieces = android::base::Split(range_text, ",");
- CHECK_GE(pieces.size(), static_cast<size_t>(3)) << "Invalid range text: " << range_text;
+ if (pieces.size() < 3) {
+ LOG(ERROR) << "Invalid range text: " << range_text;
+ return {};
+ }
size_t num;
- CHECK(android::base::ParseUint(pieces[0], &num, static_cast<size_t>(INT_MAX)))
- << "Failed to parse the number of tokens: " << range_text;
-
- CHECK_NE(num, static_cast<size_t>(0)) << "Invalid number of tokens: " << range_text;
- CHECK_EQ(num % 2, static_cast<size_t>(0)) << "Number of tokens must be even: " << range_text;
- CHECK_EQ(num, pieces.size() - 1) << "Mismatching number of tokens: " << range_text;
+ if (!android::base::ParseUint(pieces[0], &num, static_cast<size_t>(INT_MAX))) {
+ LOG(ERROR) << "Failed to parse the number of tokens: " << range_text;
+ return {};
+ }
+ if (num == 0) {
+ LOG(ERROR) << "Invalid number of tokens: " << range_text;
+ return {};
+ }
+ if (num % 2 != 0) {
+ LOG(ERROR) << "Number of tokens must be even: " << range_text;
+ return {};
+ }
+ if (num != pieces.size() - 1) {
+ LOG(ERROR) << "Mismatching number of tokens: " << range_text;
+ return {};
+ }
std::vector<Range> pairs;
for (size_t i = 0; i < num; i += 2) {
size_t first;
- CHECK(android::base::ParseUint(pieces[i + 1], &first, static_cast<size_t>(INT_MAX)));
size_t second;
- CHECK(android::base::ParseUint(pieces[i + 2], &second, static_cast<size_t>(INT_MAX)));
-
+ if (!android::base::ParseUint(pieces[i + 1], &first, static_cast<size_t>(INT_MAX)) ||
+ !android::base::ParseUint(pieces[i + 2], &second, static_cast<size_t>(INT_MAX))) {
+ return {};
+ }
pairs.emplace_back(first, second);
}
-
return RangeSet(std::move(pairs));
}
+bool RangeSet::PushBack(Range range) {
+ if (range.first >= range.second) {
+ LOG(ERROR) << "Empty or negative range: " << range.first << ", " << range.second;
+ return false;
+ }
+ size_t sz = range.second - range.first;
+ if (blocks_ >= SIZE_MAX - sz) {
+ LOG(ERROR) << "RangeSet size overflow";
+ return false;
+ }
+
+ ranges_.push_back(std::move(range));
+ blocks_ += sz;
+ return true;
+}
+
+void RangeSet::Clear() {
+ ranges_.clear();
+ blocks_ = 0;
+}
+
std::string RangeSet::ToString() const {
if (ranges_.empty()) {
return "";
@@ -114,8 +148,6 @@ bool RangeSet::Overlaps(const RangeSet& other) const {
return false;
}
-static constexpr size_t kBlockSize = 4096;
-
// Ranges in the the set should be mutually exclusive; and they're sorted by the start block.
SortedRangeSet::SortedRangeSet(std::vector<Range>&& pairs) : RangeSet(std::move(pairs)) {
std::sort(ranges_.begin(), ranges_.end());
@@ -158,11 +190,6 @@ void SortedRangeSet::Insert(size_t start, size_t len) {
Insert(to_insert);
}
-void SortedRangeSet::Clear() {
- blocks_ = 0;
- ranges_.clear();
-}
-
bool SortedRangeSet::Overlaps(size_t start, size_t len) const {
RangeSet rs({ { start / kBlockSize, (start + len - 1) / kBlockSize + 1 } });
return Overlaps(rs);