[glibmm] Fix MemoryInputStream::addData (Bug #589683)



commit 6e16f9368b2220ce50e9fa62f0f0e660727bf3e9
Author: Jonathon Jongsma <jonathon quotidian org>
Date:   Sun Jul 26 23:05:53 2009 -0500

    Fix MemoryInputStream::addData (Bug #589683)

 ChangeLog                     |   15 +++++++++++++++
 gio/src/memoryinputstream.ccg |   18 ++++++++++++++++--
 gio/src/memoryinputstream.hg  |   13 +++++++++++++
 3 files changed, 44 insertions(+), 2 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 70d1b24..6af7689 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2009-07-26  Jonathon Jongsma  <jonathon quotidian org>
+
+	* gio/src/memoryinputstream.ccg:
+	* gio/src/memoryinputstream.hg: fix MemoryInputStream::add_data() so
+	  that it copies the memory internally.  This function was basically
+	  unusable before except with statically allocated memory.
+	  Otherwise the memory would have been leaked (if the caller did not
+	  free it) or the memory could have been corrupted (if the caller
+	  freed it before the MemoryInputStream was finished using it).  The
+	  std::string version was susceptible to memory corruption as well,
+	  so we take a copy of that string as well.  To avoid needing to
+	  always copy the memory, I also introduced an overload which
+	  accepts a GDestroyNotify function that will free the memory when
+	  the object is finished with it. Fixes Bug #589683.
+
 2009-07-26  Krzesimir Nowak  <krnowak svn gnome org>
 
 	* glib/glibmm/timeval.cc:
diff --git a/gio/src/memoryinputstream.ccg b/gio/src/memoryinputstream.ccg
index 220ddb1..4c3c276 100644
--- a/gio/src/memoryinputstream.ccg
+++ b/gio/src/memoryinputstream.ccg
@@ -24,13 +24,27 @@ namespace Gio
 
 void MemoryInputStream::add_data(const std::string& data)
 {
-  g_memory_input_stream_add_data(gobj(), data.c_str(), data.size(), NULL);
+  char *data_copy = g_strdup (data.c_str ());
+  g_memory_input_stream_add_data(gobj(), data_copy, -1, g_free);
 }
 
 
 void MemoryInputStream::add_data(const void* data, gssize len)
 {
-  g_memory_input_stream_add_data(gobj(), data, len, NULL);
+  char *data_copy = 0;
+
+  // copy the data so that the caller doesn't need to keep the data alive
+  if (len > 0)
+    data_copy = g_strdup (data);
+  else
+    data_copy = g_strndup (data, len);
+
+  g_memory_input_stream_add_data(gobj(), data_copy, len, g_free);
+}
+
+void MemoryInputStream::add_data(const void* data, gssize len, GDestroyNotify destroy)
+{
+  g_memory_input_stream_add_data(gobj(), data, len, destroy);
 }
 
 } // namespace Gio
diff --git a/gio/src/memoryinputstream.hg b/gio/src/memoryinputstream.hg
index 33f2f7a..bc1a9b4 100644
--- a/gio/src/memoryinputstream.hg
+++ b/gio/src/memoryinputstream.hg
@@ -54,10 +54,23 @@ public:
 
   /** Appends to data that can be read from the input stream.
    *
+   * Note that the data will copied internally and freed when no longer needed.
+   *
    * @param data Input data.
    * @param len Length of the data, may be -1 if data is a null-terminated string.
    */
   void add_data(const void* data, gssize len);
+
+  /** Appends to data that can be read from the input stream.
+   *
+   * @param data Input data.
+   * @param len Length of the data, may be -1 if data is a null-terminated string.
+   * @param destroy A function to be called to free the data when it is no
+   * longer needed
+   *
+   * @deprecated Use version with destroy notification
+   */
+  void add_data(const void* data, gssize len, GDestroyNotify destroy);
 };
 
 } // namespace Gio



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]