diff options
author | Breno Rodrigues Guimaraes <brenorg@gmail.com> | 2023-02-21 06:37:47 -0300 |
---|---|---|
committer | Breno Rodrigues Guimaraes <brenorg@gmail.com> | 2023-02-21 06:37:47 -0300 |
commit | b09b28f3c732ce25354511b7b2165dd3e4ecf31c (patch) | |
tree | 9925b82ed18c62d119d83bfe98645d0a7bae429f | |
parent | b9474398c5cff5ff5288bd6a26016973d49ed745 (diff) | |
download | patchelf-b09b28f3c732ce25354511b7b2165dd3e4ecf31c.tar.gz |
Add out-of-range check for span
-rw-r--r-- | src/patchelf.h | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/patchelf.h b/src/patchelf.h index 1914d96..faa6e1a 100644 --- a/src/patchelf.h +++ b/src/patchelf.h @@ -6,14 +6,21 @@ using FileContents = std::shared_ptr<std::vector<unsigned char>>; template<class T> struct span { - span(T* d = {}, size_t l = {}) : data(d), len(l) {} - span(T* from, T* to) : data(from), len(to-from) {} - T& operator[](std::size_t i) { return data[i]; } + explicit span(T* d = {}, size_t l = {}) : data(d), len(l) {} + span(T* from, T* to) : data(from), len(to-from) { assert(from <= to); } + T& operator[](std::size_t i) { checkRange(i); return data[i]; } T* begin() { return data; } T* end() { return data + len; } - auto size() { return len; } - explicit operator bool() { return size() > 0; } + auto size() const { return len; } + explicit operator bool() const { return size() > 0; } +private: + void checkRange(std::size_t i) + { + bool oor = i >= size(); + assert(!oor); + if (oor) throw std::out_of_range("error: Access out of range."); + } T* data; size_t len; }; |