Opened 15 years ago

Closed 15 years ago

#1070 closed Bugs (fixed)

[iostreams]boost\iostreams\copy.hpp line80 copy_impl

Reported by: qbowater@… Owned by: Jonathan Turkanis
Milestone: To Be Determined Component: iostreams
Version: Boost 1.34.0 Severity: Problem
Keywords: Cc:

Description

boost\iostreams\copy.hpp line 80 - line 100

template<typename Source, typename Sink>
std::streamsize copy_impl( Source& src, Sink& snk,
                            std::streamsize buffer_size,
                            mpl::false_, mpl::true_ )
{   // Copy from an indirect Source to a direct Sink.
     using namespace std;
     typedef typename char_type_of<Source>::type  char_type;
     typedef pair<char_type*, char_type*>         pair_type;
     detail::basic_buffer<char_type>  buf(buffer_size);
     pair_type                        p = snk.output_sequence();
     streamsize                       total = 0;
     bool                             done  = false;
     while (!done) {
         streamsize amt;
         done = (amt = iostreams::read(src, buf.data(), buffer_size)) == -1;
         std::copy(buf.data(), buf.data() + amt, p.first + total);
         if (amt != -1)
             total += amt;
     }
     return total;
}

When "iostreams::read" return -1 ,amt equal -1,it will assert in std::copy because iterator last < first.

Please read http://article.gmane.org/gmane.comp.lib.boost.devel/161764 for detail.

Change History (7)

comment:1 by qbowater@…, 15 years ago

  • .hpp

    old new  
    8888    detail::basic_buffer<char_type>  buf(buffer_size);
    8989    pair_type                        p = snk.output_sequence();
    9090    streamsize                       total = 0;
    91     bool                             done  = false;
    92     while (!done) {
    93         streamsize amt;
    94         done = (amt = iostreams::read(src, buf.data(), buffer_size)) == -1;
     91    while (true) {
     92        streamsize amt = iostreams::read(src, buf.data(), buffer_size);
     93        if(amt == -1)
     94            break;
    9595        std::copy(buf.data(), buf.data() + amt, p.first + total);
    96         if (amt != -1)
    97             total += amt;
     96        total += amt;
    9897    }
    9998    return total;
    10099}
     
    109108    detail::basic_buffer<char_type>  buf(buffer_size);
    110109    non_blocking_adapter<Sink>       nb(snk);
    111110    std::streamsize                  total = 0;
    112     bool                             done = false;
    113     while (!done) {
    114         std::streamsize amt;
    115         done = (amt = iostreams::read(src, buf.data(), buffer_size)) == -1;
    116         if (amt != -1) {
    117             iostreams::write(nb, buf.data(), amt);
    118             total += amt;
    119         }
     111    while (true) {
     112        std::streamsize amt = iostreams::read(src, buf.data(), buffer_size);
     113        if (amt == -1)
     114            break;
     115        iostreams::write(nb, buf.data(), amt);
     116        total += amt;
    120117    }
    121118    return total;
    122119}
--- copy.hpp	2005-05-26 01:37:30.000000000 +0800
+++ copy1.hpp	2007-07-06 15:55:17.859375000 +0800
@@ -88,13 +88,12 @@
     detail::basic_buffer<char_type>  buf(buffer_size);
     pair_type                        p = snk.output_sequence();
     streamsize                       total = 0;
-    bool                             done  = false;
-    while (!done) {
-        streamsize amt;
-        done = (amt = iostreams::read(src, buf.data(), buffer_size)) == -1;
+    while (true) {
+        streamsize amt = iostreams::read(src, buf.data(), buffer_size);
+        if(amt == -1)
+            break;
         std::copy(buf.data(), buf.data() + amt, p.first + total);
-        if (amt != -1)
-            total += amt;
+        total += amt;
     }
     return total;
 }
@@ -109,14 +108,12 @@
     detail::basic_buffer<char_type>  buf(buffer_size);
     non_blocking_adapter<Sink>       nb(snk);
     std::streamsize                  total = 0;
-    bool                             done = false;
-    while (!done) {
-        std::streamsize amt;
-        done = (amt = iostreams::read(src, buf.data(), buffer_size)) == -1;
-        if (amt != -1) {
-            iostreams::write(nb, buf.data(), amt);
-            total += amt;
-        }
+    while (true) {
+        std::streamsize amt = iostreams::read(src, buf.data(), buffer_size);
+        if (amt == -1)
+            break;
+        iostreams::write(nb, buf.data(), amt);
+        total += amt;
     }
     return total;
 }

comment:2 by anonymous, 15 years ago

I think this is the same as Trac ticket # 786

comment:3 by anonymous, 15 years ago

The proposed change at ticket #786 seems a little cleaner than the change here.

comment:4 by Peter Dimov, 15 years ago

Component: Noneiostreams
Owner: set to Douglas Gregor
Severity: ShowstopperProblem

comment:5 by Peter Dimov, 15 years ago

Owner: Douglas Gregor removed

How did the owner become dgregor? A Trac mystery.

comment:6 by Marshall Clow, 15 years ago

Owner: set to Jonathan Turkanis

comment:7 by Jonathan Turkanis, 15 years ago

Resolution: fixed
Status: newclosed

Fixed in [42294] in branches/iostreams_dev, to be merged into trunk shortly. The fix is a slight modification of the suggestion in #786.

Note: See TracTickets for help on using tickets.