From 0cfcd8ac5d60feba738bf9f8854679efb727cbe5 Mon Sep 17 00:00:00 2001
From: "U-BASIS\\cray" <cray@hpa-win7-mtg1.basistech.net>
Date: Wed, 23 Jan 2019 11:02:28 -0500
Subject: [PATCH] Changes from standup

Brian had concerns with provding a pointer to memory we did not own
to from_bytes(). We add a character to the string so we can refernce
data.size() -2 and still get back the original string.
---
 rejistry++/src/RegistryByteBuffer.cpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/rejistry++/src/RegistryByteBuffer.cpp b/rejistry++/src/RegistryByteBuffer.cpp
index 5e036383a..d297c09c3 100644
--- a/rejistry++/src/RegistryByteBuffer.cpp
+++ b/rejistry++/src/RegistryByteBuffer.cpp
@@ -130,9 +130,23 @@ namespace Rejistry {
 			return L"";
 		}
 
+		// We do this so we can reference the last character in the string
+		// data.size() -2. if we didn't add a char to the string then returned
+		// string would be missing the last character. 
+		data.push_back('\0');
+		data.push_back('\0');
+
+		// We are unsure how from_bytes() works. Microsofts docs seem to indicate that the second pointer
+		// should point to the last character which will be included in the conversion.[1] However, another
+		// reference indicates that the data pointed to by the second pointer will not be included, which is 
+		// what our testing has shown.[2] We previously had the second pointer point to data.size() but there were
+		// concerns that we were pointing to memory we did not own. As a result, we add a char to the end of every
+		// string so we can use data.size() - 2 and still get the original string back.  
+		// 1. https://docs.microsoft.com/en-us/cpp/standard-library/wstring-convert-class?view=vs-2017#from_bytes
+		// 2. http://www.cplusplus.com/reference/locale/wstring_convert/from_bytes/
 		std::wstring result;
 		try {
-			result = conv.from_bytes(reinterpret_cast<const char*>(&data[0]), reinterpret_cast<const char*>(&data[data.size()]));
+			result = conv.from_bytes(reinterpret_cast<const char*>(&data[0]), reinterpret_cast<const char*>(&data[data.size()-2]));
 		}
 		catch (std::exception&)
 		{
-- 
GitLab