diff options
author | Remko Tronçon <git@el-tramo.be> | 2009-09-30 20:25:27 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2009-09-30 20:25:27 (GMT) |
commit | 6c8f9f9619f464d21c81f850f7a73498568af5c4 (patch) | |
tree | 17013a2a5e904b45bb69cc3f0df3715c76921aa9 /Swiften/Base | |
parent | d8397995e67661c8ed6b6abddd2deb2e70ea15e6 (diff) | |
download | swift-6c8f9f9619f464d21c81f850f7a73498568af5c4.zip swift-6c8f9f9619f464d21c81f850f7a73498568af5c4.tar.bz2 |
Fix ByteArray::getData() when bytearray is empty.
Also did similar fix for ByteArray constructor.
Diffstat (limited to 'Swiften/Base')
-rw-r--r-- | Swiften/Base/ByteArray.h | 15 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/ByteArrayTest.cpp | 21 |
2 files changed, 28 insertions, 8 deletions
diff --git a/Swiften/Base/ByteArray.h b/Swiften/Base/ByteArray.h index bcc3756..ea96d09 100644 --- a/Swiften/Base/ByteArray.h +++ b/Swiften/Base/ByteArray.h @@ -1,5 +1,4 @@ -#ifndef SWIFTEN_BYTEARRAY_H -#define SWIFTEN_BYTEARRAY_H +#pragma once #include <cstring> #include <vector> @@ -25,16 +24,18 @@ namespace Swift { } ByteArray(const char* c, size_t n) { - data_.resize(n); - memcpy(&data_[0], c, n); + if (n > 0) { + data_.resize(n); + memcpy(&data_[0], c, n); + } } const char* getData() const { - return &data_[0]; + return data_.empty() ? NULL : &data_[0]; } char* getData() { - return &data_[0]; + return data_.empty() ? NULL : &data_[0]; } size_t getSize() const { @@ -93,5 +94,3 @@ namespace Swift { } std::ostream& operator<<(std::ostream& os, const Swift::ByteArray& s); - -#endif diff --git a/Swiften/Base/UnitTest/ByteArrayTest.cpp b/Swiften/Base/UnitTest/ByteArrayTest.cpp new file mode 100644 index 0000000..bf893bd --- /dev/null +++ b/Swiften/Base/UnitTest/ByteArrayTest.cpp @@ -0,0 +1,21 @@ +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include "Swiften/Base/ByteArray.h" + +using namespace Swift; + +class ByteArrayTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(ByteArrayTest); + CPPUNIT_TEST(testGetData_NoData); + CPPUNIT_TEST_SUITE_END(); + + public: + void testGetData_NoData() { + ByteArray testling; + + CPPUNIT_ASSERT_EQUAL(static_cast<const char*>(NULL), static_cast<const char*>(testling.getData())); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(ByteArrayTest); |