falcong / pugixml

Automatically exported from code.google.com/p/pugixml
0 stars 0 forks source link

Non-seekable streams and the use of tellg #224

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi, this in relation with issue #93 upon resulted in supporting non-seekable 
streams. AFAICT, streams are discriminated in pugixml based on the result of 
tellg, where those that return -1 are considered non-seekable. The problem is 
that upon failure such streams may set badbit and the following reading from 
the stream fails. This failure has popped up for me while using pugixml to read 
from a boost iostreams file_source that wraps a pipe (that particular streambuf 
throws and the stream sets its badbit). 

I propose that the behavior for streams that fail this test should be to clear 
badbit before resuming reading, perhaps along these lines:

Index: src/pugixml.cpp
===================================================================
--- src/pugixml.cpp     (revision 959)
+++ src/pugixml.cpp     (working copy)
@@ -3629,7 +3629,16 @@
                size_t size = 0;

                // load stream to memory (using seek-based implementation if possible, since it's faster and takes less memory)
-               xml_parse_status status = (stream.tellg() < 0) ? 
load_stream_data_noseek(stream, &buffer, &size) : load_stream_data_seek(stream, 
&buffer, &size);
+        xml_parse_status status;
+
+        if (stream.tellg() < 0) {
+            stream.clear (stream.rdstate () & ~std::ios_base::badbit);
+            status = load_stream_data_noseek(stream, &buffer, &size);
+        }
+        else {
+            status = load_stream_data_seek(stream, &buffer, &size);
+        }
+
                if (status != status_ok) return make_parse_result(status);

                return doc.load_buffer_inplace_own(buffer, size, options, encoding);

Thanks!

Original issue reported on code.google.com by programm...@gmail.com on 27 Jan 2014 at 12:39

GoogleCodeExporter commented 9 years ago
Looks reasonable; I'll need to check badbit before doing this to make sure that 
the error is not swallowed.

Original comment by arseny.k...@gmail.com on 27 Jan 2014 at 12:46

GoogleCodeExporter commented 9 years ago
Fixed in r961.

Original comment by arseny.k...@gmail.com on 27 Jan 2014 at 4:07