From 6c8f9f9619f464d21c81f850f7a73498568af5c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Wed, 30 Sep 2009 22:25:27 +0200
Subject: Fix ByteArray::getData() when bytearray is empty.

Also did similar fix for ByteArray constructor.

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);
diff --git a/Swiften/SConscript b/Swiften/SConscript
index 12fbe15..28af98d 100644
--- a/Swiften/SConscript
+++ b/Swiften/SConscript
@@ -101,6 +101,7 @@ env.Append(UNITTEST_SOURCES = [
 		File("Application/UnitTest/ApplicationTest.cpp"),
 		File("Base/UnitTest/IDGeneratorTest.cpp"),
 		File("Base/UnitTest/StringTest.cpp"),
+		File("Base/UnitTest/ByteArrayTest.cpp"),
 		File("Client/UnitTest/ClientSessionTest.cpp"),
 		File("Compress/UnitTest/ZLibCompressorTest.cpp"),
 		File("Compress/UnitTest/ZLibDecompressorTest.cpp"),
-- 
cgit v0.10.2-6-g49f6