diff --git a/src/pieces/byte_array.cpp b/src/pieces/byte_array.cpp index a7af46c..8664ce7 100644 --- a/src/pieces/byte_array.cpp +++ b/src/pieces/byte_array.cpp @@ -326,7 +326,7 @@ ByteArray::Data::~Data() void ByteArray::Data::allocate(size_t size) { // Test for super-large size (2 GB) - if (size & 0x80000000) + if (size > 0x80000000) throw IOException("ByteArray::Data::allocate", "Size too large"); size_t wanted = 1; diff --git a/src/test/test_byte_array.cpp b/src/test/test_byte_array.cpp index 7e49a17..2ba5c7a 100644 --- a/src/test/test_byte_array.cpp +++ b/src/test/test_byte_array.cpp @@ -1,5 +1,6 @@ #include "support.h" #include +#include using pcs::ByteArray; @@ -302,13 +303,21 @@ CPPUNIT_TEST_SUITE_REGISTRATION(TestByteArrayExtending); class TestByteArrayResize : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(TestByteArrayResize); + CPPUNIT_TEST(testTrivialResize); CPPUNIT_TEST(testIncreaseSize); CPPUNIT_TEST(testDecreaseSize); CPPUNIT_TEST(testIncreaseSizeAndKeepContent); CPPUNIT_TEST(testDecreaseSizeAndKeepContent); + CPPUNIT_TEST(testSuperSize); CPPUNIT_TEST_SUITE_END(); public: + void testTrivialResize() { + ByteArray ba("foo", 3); + ba.resize(3); + CPPUNIT_ASSERT_EQUAL(ByteArray("foo", 3), ba); + } + void testIncreaseSize() { ByteArray ba; ba.resize(10); @@ -339,5 +348,12 @@ class TestByteArrayResize : public CppUnit::TestFixture CPPUNIT_ASSERT_EQUAL(3ul, ba.size()); CPPUNIT_ASSERT_EQUAL(ByteArray("foo", 3), ba); } + + void testSuperSize() { + ByteArray ba; + CPPUNIT_ASSERT_NO_THROW(ba.resize(0x80000000ul)); + CPPUNIT_ASSERT_THROW(ba.resize(0x100000000ull), pcs::Exception); + CPPUNIT_ASSERT_THROW(ba.resize(0xffffffff00000000ull), pcs::Exception); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(TestByteArrayResize);