[PPL-devel] [GIT] ppl/ppl(master): Bug fix: ostringstream.str() returns a temporary.

Abramo Bagnara abramo.bagnara at gmail.com
Mon Mar 23 19:52:22 CET 2009


Module: ppl/ppl
Branch: master
Commit: 0b1464817aa9d7c28687d32705cb4fb157b06efe
URL:    http://www.cs.unipr.it/git/gitweb.cgi?p=ppl/ppl.git;a=commit;h=0b1464817aa9d7c28687d32705cb4fb157b06efe

Author: Abramo Bagnara <abramo.bagnara at gmail.com>
Date:   Mon Mar 23 19:42:46 2009 +0100

Bug fix: ostringstream.str() returns a temporary.

---

 src/c_stream.cc           |   32 +++++++++++++++++---------------
 src/c_stream.h            |    2 +-
 src/c_streambuf_format.cc |    7 +++++--
 3 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/src/c_stream.cc b/src/c_stream.cc
index ab2d8aa..0c25bbb 100644
--- a/src/c_stream.cc
+++ b/src/c_stream.cc
@@ -1,4 +1,5 @@
 #include <cstdio>
+#include <cstring>
 #include <iostream>
 #include <sstream>
 #include "stdiobuf.defs.hh"
@@ -33,18 +34,18 @@ ppl_io_format_settings ppl_io_format_default_settings = {
   0,           // bottom
   {
     // length, left, right, alignment, fill_char
-    { 80, "", "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // FIRST
-    { 80, "", "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // FIRSTLAST
-    { 80, "", "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // NEXT
-    { 80, "", "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // LAST
-    { 80, "", "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // FORCED_FIRST
-    { 80, "", "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // FORCED_NEXT
-    { 80, "", "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // LONGER_FIRST
-    { 80, "", "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // LONGER_FIRSTLAST
-    { 80, "", "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // LONGER_NEXT
-    { 80, "", "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // LONGER_LAST
-    { 80, "", "",   PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // UNTERMINATED_FIRST
-    { 80, "", "",   PPL_IO_FORMAT_ALIGN_LEFT, 0 }  // UNTERMINATED_NEXT
+    { 80, 0, "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // FIRST
+    { 80, 0, "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // FIRSTLAST
+    { 80, 0, "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // NEXT
+    { 80, 0, "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // LAST
+    { 80, 0, "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // FORCED_FIRST
+    { 80, 0, "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // FORCED_NEXT
+    { 80, 0, "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // LONGER_FIRST
+    { 80, 0, "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // LONGER_FIRSTLAST
+    { 80, 0, "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // LONGER_NEXT
+    { 80, 0, "\n", PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // LONGER_LAST
+    { 80, 0, 0,    PPL_IO_FORMAT_ALIGN_LEFT, 0 }, // UNTERMINATED_FIRST
+    { 80, 0, 0,    PPL_IO_FORMAT_ALIGN_LEFT, 0 }  // UNTERMINATED_NEXT
   }
 };
 
@@ -78,10 +79,11 @@ void ppl_io_ostream_format_replace_settings(ppl_io_ostream* stream, ppl_io_forma
   static_cast<c_streambuf_format*>(stream->sbuf)->replace_settings(settings);
 }
 
-size_t ppl_io_ostream_buffer_get(struct ppl_io_ostream* s, const char **buf) {
+size_t ppl_io_ostream_buffer_get(struct ppl_io_ostream* s, char **buf) {
   std::ostringstream* ss = static_cast<std::ostringstream*>(s->stream);
-  *buf = ss->str().c_str();
-  return ss->str().size();
+  std::string str = ss->str();
+  *buf = strdup(str.c_str());
+  return str.size();
 }
 
 void ppl_io_ostream_buffer_clear(struct ppl_io_ostream* s) {
diff --git a/src/c_stream.h b/src/c_stream.h
index b0bb5eb..d54d532 100644
--- a/src/c_stream.h
+++ b/src/c_stream.h
@@ -60,7 +60,7 @@ struct ppl_io_ostream* ppl_io_ostream_format_new(struct ppl_io_ostream* stream,
 void ppl_io_ostream_format_replace_settings(struct ppl_io_ostream* stream, struct ppl_io_format_settings* settings);
 
 void ppl_io_ostream_delete(struct ppl_io_ostream* stream);
-size_t ppl_io_ostream_buffer_get(struct ppl_io_ostream* stream, const char** buf);
+size_t ppl_io_ostream_buffer_get(struct ppl_io_ostream* stream, char** buf);
 void ppl_io_ostream_buffer_clear(struct ppl_io_ostream* stream);
 
 int ppl_io_write_endl(struct ppl_io_ostream* s);
diff --git a/src/c_streambuf_format.cc b/src/c_streambuf_format.cc
index 3325dff..96b9a64 100644
--- a/src/c_streambuf_format.cc
+++ b/src/c_streambuf_format.cc
@@ -124,6 +124,7 @@ int c_streambuf_format::cb_flush() {
   if (n > 0) {
     if (!output_line(str.c_str(), n, first ? PPL_IO_FORMAT_LINE_UNTERMINATED_FIRST : PPL_IO_FORMAT_LINE_UNTERMINATED_NEXT))
       return -1;
+    str.clear();
   }
   return 0;
 }
@@ -142,7 +143,8 @@ bool c_streambuf_format::output_line(const char *s, unsigned int n, ppl_io_forma
   default:
     break;
   }
-  stream << settings->lines[type].left;
+  if (settings->lines[type].left)
+    stream << settings->lines[type].left;
   if (settings->lines[type].fill_char && n < settings->lines[type].length) {
     unsigned int left = 0;
     unsigned int right = 0;
@@ -158,7 +160,8 @@ bool c_streambuf_format::output_line(const char *s, unsigned int n, ppl_io_forma
   }
   else
     stream.write(s, n);
-  stream << settings->lines[type].right;
+  if (settings->lines[type].right)
+    stream << settings->lines[type].right;
   switch (type) {
   case PPL_IO_FORMAT_LINE_FIRSTLAST:
   case PPL_IO_FORMAT_LINE_LAST:




More information about the PPL-devel mailing list