From ce454573e630a123d47797df1d3d594e9cfa656d Mon Sep 17 00:00:00 2001
From: Aaron Cuevas Lopez <aacuelo@teleco.upv.es>
Date: Sun, 15 Jan 2017 22:07:46 +0100
Subject: [PATCH] Improve security on spike buffer

---
 .../SpikeDetector/SpikeDetector.cpp           |  6 +-
 Source/Processors/Events/Events.cpp           | 69 +++++++++++++++++--
 Source/Processors/Events/Events.h             | 17 ++++-
 3 files changed, 83 insertions(+), 9 deletions(-)

diff --git a/Source/Plugins/BasicSpikeDisplay/SpikeDetector/SpikeDetector.cpp b/Source/Plugins/BasicSpikeDisplay/SpikeDetector/SpikeDetector.cpp
index 1908013f5..2b4b2a4c1 100644
--- a/Source/Plugins/BasicSpikeDisplay/SpikeDetector/SpikeDetector.cpp
+++ b/Source/Plugins/BasicSpikeDisplay/SpikeDetector/SpikeDetector.cpp
@@ -365,14 +365,12 @@ void SpikeDetector::addWaveformToSpikeObject (SpikeEvent::SpikeBuffer& s,
 
     const int chan = *(electrodes[electrodeNumber]->channels + currentChannel);
 
-    // cycle through buffer
-	float* data = s[currentChannel];
     if (isChannelActive (electrodeNumber, currentChannel))
     {
 		
         for (int sample = 0; sample < spikeLength; ++sample)
         {
-            data[sample] = getNextSample (*(electrodes[electrodeNumber]->channels+currentChannel));
+            s.set(currentChannel,sample, getNextSample (*(electrodes[electrodeNumber]->channels+currentChannel)));
             ++sampleIndex;
 
             //std::cout << currentIndex << std::endl;
@@ -383,7 +381,7 @@ void SpikeDetector::addWaveformToSpikeObject (SpikeEvent::SpikeBuffer& s,
         for (int sample = 0; sample < spikeLength; ++sample)
         {
             // insert a blank spike if the
-           data[sample] = 0;
+			s.set(currentChannel, sample, 0);
             ++sampleIndex;
             //std::cout << currentIndex << std::endl;
         }
diff --git a/Source/Processors/Events/Events.cpp b/Source/Processors/Events/Events.cpp
index 9c36f5107..07e28f415 100644
--- a/Source/Processors/Events/Events.cpp
+++ b/Source/Processors/Events/Events.cpp
@@ -1003,19 +1003,80 @@ SpikeEvent::SpikeBuffer::SpikeBuffer(const SpikeChannel* channelInfo)
 	m_data.malloc(m_nChans*m_nSamps);
 }
 
-float* SpikeEvent::SpikeBuffer::operator[](const int index)
+void  SpikeEvent::SpikeBuffer::set(const int chan, const int samp, const float value)
 {
 	if (!m_ready)
 	{
 		jassertfalse;
-		return nullptr;
+		return;
+	}
+	jassert(chan >= 0 && samp >= 0 && chan < m_nChans && samp < m_nSamps);
+	m_data[samp + chan*m_nSamps] = value;
+}
+
+void  SpikeEvent::SpikeBuffer::set(const int index, const float value)
+{
+	if (!m_ready)
+	{
+		jassertfalse;
+		return;
+	}
+	jassert(index >= 0 && index < m_nChans * m_nSamps);
+	m_data[index] = value;
+}
+
+void  SpikeEvent::SpikeBuffer::set(const int chan, const float* source, const int n)
+{
+	if (!m_ready)
+	{
+		jassertfalse;
+		return;
 	}
-	if (index < 0 || index >= m_nChans)
+	jassert(chan >= 0 && chan < m_nChans && n <= m_nSamps);
+	memcpy(m_data.getData(), source, n*sizeof(float));
+}
+
+void  SpikeEvent::SpikeBuffer::set(const int chan, const int start, const float* source, const int n)
+{
+	if (!m_ready)
+	{
+		jassertfalse;
+		return;
+	}
+	jassert(chan >= 0 && chan < m_nChans && (n + start) <= m_nSamps);
+	memcpy(m_data.getData() + start, source, n*sizeof(float));
+}
+
+float SpikeEvent::SpikeBuffer::get(const int chan, const int samp)
+{
+	if (!m_ready)
+	{
+		jassertfalse;
+		return 0;
+	}
+	jassert(chan >= 0 && samp >= 0 && chan < m_nChans && samp < m_nSamps);
+	return m_data[chan*m_nSamps + samp];
+}
+
+float SpikeEvent::SpikeBuffer::get(const int index)
+{
+	if (!m_ready)
+	{
+		jassertfalse;
+		return;
+	}
+	jassert(index >= 0 && index < m_nChans * m_nSamps);
+	return m_data[index];
+}
+
+const float* SpikeEvent::SpikeBuffer::getRawPointer()
+{
+	if (!m_ready)
 	{
 		jassertfalse;
 		return nullptr;
 	}
-	return m_data.getData() + (index * m_nSamps);
+	return m_data.getData();
 }
 
 //Template definitions
diff --git a/Source/Processors/Events/Events.h b/Source/Processors/Events/Events.h
index b91357fd8..14b51f7dd 100644
--- a/Source/Processors/Events/Events.h
+++ b/Source/Processors/Events/Events.h
@@ -163,6 +163,8 @@ class PLUGIN_API TTLEvent
 public:
 	TTLEvent(const TTLEvent& other);
 	~TTLEvent();
+	TTLEvent& operator=(const TTLEvent&) = delete;
+
 	void serialize(void* dstBuffer, size_t dstSize) const override;
 
 	/** Gets the state true ='1' false = '0'*/
@@ -188,6 +190,8 @@ class PLUGIN_API TextEvent
 public:
 	TextEvent(const TextEvent& other);
 	~TextEvent();
+	TextEvent& operator=(const TextEvent&) = delete;
+
 	void serialize(void* dstBuffer, size_t dstSize) const override;
 	String getText() const;
 
@@ -209,6 +213,8 @@ class PLUGIN_API BinaryEvent
 public:
 	BinaryEvent(const BinaryEvent& other);
 	~BinaryEvent();
+	BinaryEvent& operator=(const BinaryEvent&) = delete;
+
 	void serialize(void* dstBuffer, size_t dstSize) const override;
 
 	const void* getBinaryDataPointer() const;
@@ -254,7 +260,14 @@ public:
 		friend SpikeEvent;
 	public:
 		SpikeBuffer(const SpikeChannel* channelInfo);
-		float* operator[] (const int index);
+		void set(const int chan, const int samp, const float value);
+		void set(const int index, const float value);
+		void set(const int chan, const float* source, const int n);
+		void set(const int chan, const int start, const float* source, const int n);
+		float get(const int chan, const int samp);
+		float get(const int index);
+		//Caution advised with this method, as the pointer can become inaccessible
+		const float* getRawPointer();
 	private:
 		SpikeBuffer() = delete;
 		HeapBlock<float> m_data;
@@ -264,6 +277,8 @@ public:
 	};
 	SpikeEvent(const SpikeEvent& other);
 	~SpikeEvent();
+	SpikeEvent& operator=(const SpikeEvent&) = delete;
+
 	void serialize(void* dstBuffer, size_t dstSize) const override;
 
 	const SpikeChannel* getChannelInfo() const;
-- 
GitLab