From 78e6771148ce1c88eb66baba27aa382c96566292 Mon Sep 17 00:00:00 2001 From: q66 Date: Sun, 19 Feb 2017 18:31:08 +0100 Subject: [PATCH] bounds checking pointer/string ranges --- ostd/range.hh | 11 +++++++++++ ostd/string.hh | 3 +++ 2 files changed, 14 insertions(+) diff --git a/ostd/range.hh b/ostd/range.hh index b959e21..3ea889a 100644 --- a/ostd/range.hh +++ b/ostd/range.hh @@ -1731,9 +1731,20 @@ struct iterator_range: input_range> { /* satisfy output_range */ void put(value_type const &v) { + /* rely on iterators to do their own checks */ + if constexpr(std::is_pointer_v) { + if (p_beg == p_end) { + throw std::out_of_range{"put into an empty range"}; + } + } *(p_beg++) = v; } void put(value_type &&v) { + if constexpr(std::is_pointer_v) { + if (p_beg == p_end) { + throw std::out_of_range{"put into an empty range"}; + } + } *(p_beg++) = std::move(v); } diff --git a/ostd/string.hh b/ostd/string.hh index 648c945..10227eb 100644 --- a/ostd/string.hh +++ b/ostd/string.hh @@ -148,6 +148,9 @@ public: T &operator[](size_t i) const { return p_beg[i]; } void put(T v) { + if (p_beg == p_end) { + throw std::out_of_range{"put into an empty range"}; + } *(p_beg++) = v; }