diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2018-11-15 00:04:12 +0000 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2018-11-15 00:04:12 +0000 |
commit | aeb2b1f7fbe1907f6ff6feaac4f042decd895765 (patch) | |
tree | e8802afd1ca8a0f523b7390029cae4b4c84b9196 | |
parent | ded7b411a896034096b8896681ee2e3329d4c946 (diff) | |
download | gcc-aeb2b1f7fbe1907f6ff6feaac4f042decd895765.zip gcc-aeb2b1f7fbe1907f6ff6feaac4f042decd895765.tar.gz gcc-aeb2b1f7fbe1907f6ff6feaac4f042decd895765.tar.bz2 |
Fix test that does undefined shifts greater than width of size_t
* testsuite/20_util/unsynchronized_pool_resource/allocate.cc: Fix
test for 32-bit targets. Test additional allocation sizes.
From-SVN: r266163
-rw-r--r-- | libstdc++-v3/ChangeLog | 5 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/20_util/unsynchronized_pool_resource/allocate.cc | 50 |
2 files changed, 32 insertions, 23 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 9b4bdc6..5262ef5 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,8 @@ +2018-11-15 Jonathan Wakely <jwakely@redhat.com> + + * testsuite/20_util/unsynchronized_pool_resource/allocate.cc: Fix + test for 32-bit targets. Test additional allocation sizes. + 2018-11-14 Jonathan Wakely <jwakely@redhat.com> * scripts/create_testsuite_files: Add special_functions to the list diff --git a/libstdc++-v3/testsuite/20_util/unsynchronized_pool_resource/allocate.cc b/libstdc++-v3/testsuite/20_util/unsynchronized_pool_resource/allocate.cc index 749655b..0325a43 100644 --- a/libstdc++-v3/testsuite/20_util/unsynchronized_pool_resource/allocate.cc +++ b/libstdc++-v3/testsuite/20_util/unsynchronized_pool_resource/allocate.cc @@ -170,7 +170,7 @@ test05() void test06() { - struct custom_mr : std::pmr::memory_resource + struct checking_mr : std::pmr::memory_resource { size_t expected_size = 0; size_t expected_alignment = 0; @@ -178,29 +178,30 @@ test06() struct bad_size { }; struct bad_alignment { }; - void* do_allocate(std::size_t b, std::size_t a) + void* do_allocate(std::size_t bytes, std::size_t align) { - if (expected_size != 0) - { - if (b < expected_size) - throw bad_size(); - else if (a != expected_alignment) - throw bad_alignment(); - // Else just throw, don't try to allocate: - throw std::bad_alloc(); - } - - return std::pmr::new_delete_resource()->allocate(b, a); + // Internal data structures in unsynchronized_pool_resource need to + // allocate memory, so handle those normally: + if (align <= alignof(std::max_align_t)) + return std::pmr::new_delete_resource()->allocate(bytes, align); + + // This is a large, unpooled allocation. Check the arguments: + if (bytes < expected_size) + throw bad_size(); + else if (align != expected_alignment) + throw bad_alignment(); + // Else just throw, don't really try to allocate: + throw std::bad_alloc(); } - void do_deallocate(void* p, std::size_t b, std::size_t a) - { std::pmr::new_delete_resource()->deallocate(p, b, a); } + void do_deallocate(void* p, std::size_t bytes, std::size_t align) + { std::pmr::new_delete_resource()->deallocate(p, bytes, align); } bool do_is_equal(const memory_resource& r) const noexcept { return false; } }; - custom_mr c; + checking_mr c; std::pmr::unsynchronized_pool_resource r({1, 1}, &c); std::pmr::pool_options opts = r.options(); const std::size_t largest_pool = opts.largest_required_pool_block; @@ -214,23 +215,26 @@ test06() // Try allocating various very large sizes and ensure the size requested // from the upstream allocator is at least as large as needed. - for (int i = 1; i < 64; ++i) + for (int i = 0; i < std::numeric_limits<std::size_t>::digits; ++i) { - for (auto b : { -1, 0, 1, 3 }) + for (auto b : { -63, -5, -1, 0, 1, 3, std::numeric_limits<int>::max() }) { std::size_t bytes = std::size_t(1) << i; - bytes += b; + bytes += b; // For negative b this can wrap to a large positive value. c.expected_size = bytes; c.expected_alignment = large_alignment; + bool caught_bad_alloc = false; try { (void) r.allocate(bytes, large_alignment); } catch (const std::bad_alloc&) { // expect to catch bad_alloc - } catch (custom_mr::bad_size) { - VERIFY(false); - } catch (custom_mr::bad_alignment) { - VERIFY(false); + caught_bad_alloc = true; + } catch (checking_mr::bad_size) { + VERIFY( ! "allocation from upstream resource had expected size" ); + } catch (checking_mr::bad_alignment) { + VERIFY( ! "allocation from upstream resource had expected alignment" ); } + VERIFY( caught_bad_alloc ); } } } |