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

Unified Diff: sandbox/linux/seccomp-bpf/syscall_iterator.cc

Issue 659723002: SyscallIterator: support C++11 range-based for loops (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add 'explicit' Created 6 years, 2 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
Index: sandbox/linux/seccomp-bpf/syscall_iterator.cc
diff --git a/sandbox/linux/seccomp-bpf/syscall_iterator.cc b/sandbox/linux/seccomp-bpf/syscall_iterator.cc
index 6701d50d71f297b14a21c4c33abc6a0336482a9a..1b4e0678ab7b0fced954fa3c4291cbd456b5198b 100644
--- a/sandbox/linux/seccomp-bpf/syscall_iterator.cc
+++ b/sandbox/linux/seccomp-bpf/syscall_iterator.cc
@@ -4,6 +4,7 @@
#include "sandbox/linux/seccomp-bpf/syscall_iterator.h"
+#include "base/logging.h"
#include "base/macros.h"
#include "sandbox/linux/seccomp-bpf/linux_seccomp.h"
@@ -38,14 +39,16 @@ const SyscallRange kValidSyscallRanges[] = {
#endif
};
+// NextSyscall returns the next system call in the specified system
+// call set after |cur|, or 0 if no such system call exists.
uint32_t NextSyscall(uint32_t cur, bool invalid_only) {
for (const SyscallRange& range : kValidSyscallRanges) {
if (range.first > 0 && cur < range.first - 1) {
return range.first - 1;
}
if (cur <= range.last) {
- if (invalid_only && cur < range.last) {
- return range.last;
+ if (invalid_only) {
+ return range.last + 1;
}
return cur + 1;
}
@@ -62,27 +65,22 @@ uint32_t NextSyscall(uint32_t cur, bool invalid_only) {
if (cur < 0x80000000u)
return 0x80000000u;
- return 0xFFFFFFFFu;
+ if (cur < 0xFFFFFFFFu)
+ return 0xFFFFFFFFu;
+ return 0;
}
} // namespace
-uint32_t SyscallIterator::Next() {
- if (done_) {
- return num_;
- }
-
- uint32_t val;
- do {
- val = num_;
- num_ = NextSyscall(num_, invalid_only_);
- } while (invalid_only_ && IsValid(val));
+SyscallSet::Iterator SyscallSet::begin() const {
+ return Iterator(set_, false);
+}
- done_ |= val == 0xFFFFFFFFu;
- return val;
+SyscallSet::Iterator SyscallSet::end() const {
+ return Iterator(set_, true);
}
-bool SyscallIterator::IsValid(uint32_t num) {
+bool SyscallSet::IsValid(uint32_t num) {
for (const SyscallRange& range : kValidSyscallRanges) {
if (num >= range.first && num <= range.last) {
return true;
@@ -91,4 +89,42 @@ bool SyscallIterator::IsValid(uint32_t num) {
return false;
}
+bool operator==(const SyscallSet& lhs, const SyscallSet& rhs) {
+ return (lhs.set_ == rhs.set_);
+}
+
+SyscallSet::Iterator::Iterator(Set set, bool done)
+ : set_(set), done_(done), num_(0) {
+ if (set_ == Set::INVALID_ONLY && !done_ && IsValid(num_)) {
+ ++*this;
+ }
+}
+
+uint32_t SyscallSet::Iterator::operator*() const {
+ DCHECK(!done_);
+ return num_;
+}
+
+SyscallSet::Iterator& SyscallSet::Iterator::operator++() {
+ DCHECK(!done_);
+
+ num_ = NextSyscall(num_, set_ == Set::INVALID_ONLY);
+ if (num_ == 0) {
+ done_ = true;
+ }
+
+ return *this;
+}
+
+bool operator==(const SyscallSet::Iterator& lhs,
+ const SyscallSet::Iterator& rhs) {
+ DCHECK(lhs.set_ == rhs.set_);
+ return (lhs.done_ == rhs.done_) && (lhs.num_ == rhs.num_);
+}
+
+bool operator!=(const SyscallSet::Iterator& lhs,
+ const SyscallSet::Iterator& rhs) {
+ return !(lhs == rhs);
+}
+
} // namespace sandbox
« no previous file with comments | « sandbox/linux/seccomp-bpf/syscall_iterator.h ('k') | sandbox/linux/seccomp-bpf/syscall_iterator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698