diff options
author | Jack Lloyd <[email protected]> | 2017-09-22 20:23:36 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-09-22 20:23:36 -0400 |
commit | 08e47c5e550c1135535011f178a84d54613fb976 (patch) | |
tree | b1fc7e469ca5699cda21f3dbc119bfc120ef2627 | |
parent | 3464804dbad8fdc44500082b224c54704cf2fcf6 (diff) |
Deprecate modification of Pipe objects
This is a feature that is hairy to implement and really makes
application code confusing.
-rw-r--r-- | src/lib/filters/filter.h | 2 | ||||
-rw-r--r-- | src/lib/filters/pipe.cpp | 32 | ||||
-rw-r--r-- | src/lib/filters/pipe.h | 5 | ||||
-rw-r--r-- | src/tests/test_filters.cpp | 34 |
4 files changed, 44 insertions, 29 deletions
diff --git a/src/lib/filters/filter.h b/src/lib/filters/filter.h index 8cd045f6e..773552b77 100644 --- a/src/lib/filters/filter.h +++ b/src/lib/filters/filter.h @@ -139,7 +139,7 @@ class BOTAN_PUBLIC_API(2,0) Filter Filter* get_next() const; secure_vector<uint8_t> m_write_queue; - std::vector<Filter*> m_next; + std::vector<Filter*> m_next; // not owned size_t m_port_num, m_filter_owns; // true if filter belongs to a pipe --> prohibit filter sharing! diff --git a/src/lib/filters/pipe.cpp b/src/lib/filters/pipe.cpp index 09ebd2165..51f2cace5 100644 --- a/src/lib/filters/pipe.cpp +++ b/src/lib/filters/pipe.cpp @@ -30,13 +30,9 @@ class Null_Filter final : public Filter /* * Pipe Constructor */ -Pipe::Pipe(Filter* f1, Filter* f2, Filter* f3, Filter* f4) +Pipe::Pipe(Filter* f1, Filter* f2, Filter* f3, Filter* f4) : + Pipe({f1,f2,f3,f4}) { - init(); - append(f1); - append(f2); - append(f3); - append(f4); } /* @@ -47,7 +43,7 @@ Pipe::Pipe(std::initializer_list<Filter*> args) init(); for(auto i = args.begin(); i != args.end(); ++i) - append(*i); + do_append(*i); } /* @@ -213,13 +209,16 @@ void Pipe::clear_endpoints(Filter* f) } } +void Pipe::append(Filter* filter) + { + do_append(filter); + } + /* * Append a Filter to the Pipe */ -void Pipe::append(Filter* filter) +void Pipe::do_append(Filter* filter) { - if(m_inside_msg) - throw Invalid_State("Cannot append to a Pipe while it is processing"); if(!filter) return; if(dynamic_cast<SecureQueue*>(filter)) @@ -227,6 +226,9 @@ void Pipe::append(Filter* filter) if(filter->m_owned) throw Invalid_Argument("Filters cannot be shared among multiple Pipes"); + if(m_inside_msg) + throw Invalid_State("Cannot append to a Pipe while it is processing"); + filter->m_owned = true; if(!m_pipe) m_pipe = filter; @@ -267,16 +269,12 @@ void Pipe::pop() if(m_pipe->total_ports() > 1) throw Invalid_State("Cannot pop off a Filter with multiple ports"); - Filter* f = m_pipe; - size_t owns = f->owns(); - m_pipe = m_pipe->m_next[0]; - delete f; + size_t to_remove = m_pipe->owns() + 1; - while(owns--) + while(to_remove--) { - f = m_pipe; + std::unique_ptr<Filter> to_destroy(m_pipe); m_pipe = m_pipe->m_next[0]; - delete f; } } diff --git a/src/lib/filters/pipe.h b/src/lib/filters/pipe.h index 8dcb1e95c..060b5c1ca 100644 --- a/src/lib/filters/pipe.h +++ b/src/lib/filters/pipe.h @@ -271,22 +271,26 @@ class BOTAN_PUBLIC_API(2,0) Pipe final : public DataSource * Insert a new filter at the front of the pipe * @param filt the new filter to insert */ + BOTAN_DEPRECATED("Runtime modification of Pipe deprecated") void prepend(Filter* filt); /** * Insert a new filter at the back of the pipe * @param filt the new filter to insert */ + BOTAN_DEPRECATED("Runtime modification of Pipe deprecated") void append(Filter* filt); /** * Remove the first filter at the front of the pipe. */ + BOTAN_DEPRECATED("Runtime modification of Pipe deprecated") void pop(); /** * Reset this pipe to an empty pipe. */ + BOTAN_DEPRECATED("Runtime modification of Pipe deprecated") void reset(); /** @@ -309,6 +313,7 @@ class BOTAN_PUBLIC_API(2,0) Pipe final : public DataSource private: void init(); void destruct(Filter*); + void do_append(Filter* filt); void find_endpoints(Filter*); void clear_endpoints(Filter*); diff --git a/src/tests/test_filters.cpp b/src/tests/test_filters.cpp index d3993cce3..0fac1d4e3 100644 --- a/src/tests/test_filters.cpp +++ b/src/tests/test_filters.cpp @@ -7,6 +7,8 @@ * Botan is released under the Simplified BSD License (see license.txt) */ +#define BOTAN_NO_DEPRECATED_WARNINGS + #include "tests.h" #if defined(BOTAN_HAS_FILTERS) @@ -194,20 +196,30 @@ class Filter_Tests final : public Test std::unique_ptr<Botan::Filter> queue_filter(new Botan::SecureQueue); // can't explicitly insert a queue into the pipe because they are implicit - result.test_throws("pipe error", "Invalid argument Pipe::append: SecureQueue cannot be used", [&]() - { pipe.append(queue_filter.get()); }); - result.test_throws("pipe error", "Invalid argument Pipe::prepend: SecureQueue cannot be used", [&]() - { pipe.prepend(queue_filter.get()); }); + result.test_throws("pipe error", + "Invalid argument Pipe::append: SecureQueue cannot be used", + [&]() { pipe.append(queue_filter.get()); }); + + result.test_throws("pipe error", + "Invalid argument Pipe::prepend: SecureQueue cannot be used", + [&]() { pipe.prepend(queue_filter.get()); }); pipe.start_msg(); + std::unique_ptr<Botan::Filter> filter(new Botan::BitBucket); + // now inside a message, cannot modify pipe structure - result.test_throws("pipe error", "Cannot append to a Pipe while it is processing", [&]() - { pipe.append(nullptr); }); - result.test_throws("pipe error", "Cannot prepend to a Pipe while it is processing", [&]() - { pipe.prepend(nullptr); }); - result.test_throws("pipe error", "Cannot pop off a Pipe while it is processing", [&]() - { pipe.pop(); }); + result.test_throws("pipe error", + "Cannot append to a Pipe while it is processing", + [&]() { pipe.append(filter.get()); }); + + result.test_throws("pipe error", + "Cannot prepend to a Pipe while it is processing", + [&]() { pipe.prepend(filter.get()); }); + + result.test_throws("pipe error", + "Cannot pop off a Pipe while it is processing", + [&]() { pipe.pop(); }); pipe.end_msg(); @@ -247,7 +259,7 @@ class Filter_Tests final : public Test Test::Result result("Pipe"); #if defined(BOTAN_HAS_SHA2_32) - Botan::Pipe pipe(new Botan::Hash_Filter("SHA-224")); + Botan::Pipe pipe(new Botan::Chain(new Botan::Hash_Filter("SHA-224"), new Botan::Hex_Encoder)); pipe.pop(); pipe.append(new Botan::Hash_Filter("SHA-256")); |