From 2f0a62ff98bb75181be284b1f2b2f4f555d5560c Mon Sep 17 00:00:00 2001
From: Brian Carrier <carrier@sleuthkit.org>
Date: Sun, 5 Nov 2017 17:09:57 -0500
Subject: [PATCH] Added comments and catch exception of vector can't be resized

---
 rejistry++/src/ByteBuffer.cpp         | 17 ++++++++++++++++-
 rejistry++/src/RegistryByteBuffer.cpp |  5 ++++-
 rejistry++/src/RegistryHiveBuffer.cpp |  4 ++++
 3 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/rejistry++/src/ByteBuffer.cpp b/rejistry++/src/ByteBuffer.cpp
index de0ddf405..a0096bb4c 100644
--- a/rejistry++/src/ByteBuffer.cpp
+++ b/rejistry++/src/ByteBuffer.cpp
@@ -36,10 +36,18 @@ namespace Rejistry {
             _buffer.resize(capacity);
         }
 
+        /**
+         * Makes a copy of the passed in buffer.
+         * @throws RegistryParseException if memory can't be allocated
+         */
         ByteBuffer::ByteBuffer(const uint8_t * buf, const uint32_t length) : Buffer(length) {
             initializeBuffer(buf, length);
         }
 
+        /**
+        * Makes a copy of the passed in buffer.
+        * @throws RegistryParseException if memory can't be allocated
+        */
         ByteBuffer::ByteBuffer(const ByteArray& buf, const uint32_t length) : Buffer(length) {
             if (buf.size() > 0) {
                 initializeBuffer(&buf[0], length);
@@ -47,7 +55,14 @@ namespace Rejistry {
         }
 
         void ByteBuffer::initializeBuffer(const uint8_t * buf, const uint32_t length) {
-            _buffer.resize(length);
+            try {
+                _buffer.resize(length);
+            } 
+            catch (std::bad_alloc &e)
+            {
+                throw RegistryParseException("Cannot allocate memory for registry byte buffer.");
+            }
+
             if (buf != NULL) {
                 memcpy(&_buffer[0], buf, length);
             }
diff --git a/rejistry++/src/RegistryByteBuffer.cpp b/rejistry++/src/RegistryByteBuffer.cpp
index 84a6d983d..9b43b3ade 100644
--- a/rejistry++/src/RegistryByteBuffer.cpp
+++ b/rejistry++/src/RegistryByteBuffer.cpp
@@ -39,7 +39,10 @@
 namespace Rejistry {
 
     std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff, std::little_endian>, wchar_t> conv;
-
+    
+    /**
+    * Does NOT make a copy of the passed in buffer, but will free the memory when deleted 
+    */
     RegistryByteBuffer::RegistryByteBuffer(ByteBuffer * buffer) {
         if (buffer == NULL) {
             throw std::invalid_argument("Buffer must not be null.");
diff --git a/rejistry++/src/RegistryHiveBuffer.cpp b/rejistry++/src/RegistryHiveBuffer.cpp
index 313e6d340..d7233c7b7 100644
--- a/rejistry++/src/RegistryHiveBuffer.cpp
+++ b/rejistry++/src/RegistryHiveBuffer.cpp
@@ -31,6 +31,10 @@
 
 namespace Rejistry {
 
+    /**
+    * Makes a copy of the passed in buffer.
+    * @throws RegistryParseException if memory can't be allocated
+    */
     RegistryHiveBuffer::RegistryHiveBuffer(const uint8_t * buffer, const uint32_t size) {
         _buffer = new RegistryByteBuffer(new ByteBuffer(buffer, size));
     }
-- 
GitLab