Skip to content

Commit 3c9c334

Browse files
committed
Cleaner/safer code
1 parent c2166c5 commit 3c9c334

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

src/io/CompressedInputStream.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,8 @@ T DecodingTask<T>::run()
845845
return T(*_data, blockId, 0, 0, 0, "Success");
846846
}
847847

848-
istreambuf<char> buf(reinterpret_cast<char*>(&_data->_array[0]), streamsize(r));
849-
iostream ios(&buf);
848+
ifixedbuf buf(reinterpret_cast<char*>(&_data->_array[0]), streamsize(r));
849+
istream ios(&buf);
850850
ibs = (streamPerTask == true) ? new DefaultInputBitStream(ios) : _ibs;
851851

852852
// Extract block header from bitstream

src/io/CompressedOutputStream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ T EncodingTask<T>::run()
705705
}
706706

707707
_data->_index = 0;
708-
ostreambuf<char> buf(reinterpret_cast<char*>(&_data->_array[_data->_index]), streamsize(_data->_length));
708+
ofixedbuf buf(reinterpret_cast<char*>(&_data->_array[_data->_index]), streamsize(_data->_length));
709709
ostream os(&buf);
710710
DefaultOutputBitStream obs(os);
711711

src/transform/ROLZCodec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ bool ROLZCodec1::inverse(SliceArray<byte>& input, SliceArray<byte>& output, int
471471
try
472472
{
473473
// Decode literal, length and match index buffers
474-
istreambuf<char> buffer(reinterpret_cast<char*>(&src[srcIdx]), max(min(count - srcIdx, sizeChunk + 16), 65536));
474+
ifixedbuf buffer(reinterpret_cast<char*>(&src[srcIdx]), max(min(count - srcIdx, sizeChunk + 16), 65536));
475475
istream is(&buffer);
476476
DefaultInputBitStream ibs(is, 65536);
477477
const int litLen = int(ibs.readBits(32));

src/util.hpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,36 @@ limitations under the License.
2323
#include "types.hpp"
2424

2525

26-
2726
// Ahem ... Visual Studio
28-
// This ostreambuf class is required because Microsoft cannot bother to implement
29-
// streambuf::pubsetbuf().
30-
template <typename T>
31-
struct ostreambuf : public std::basic_streambuf<T, std::char_traits<T> >
32-
{
33-
ostreambuf(T* buffer, std::streamsize length) {
34-
this->setp(buffer, &buffer[length]);
27+
// This code is required because Microsoft cannot bother to implement streambuf::pubsetbuf().
28+
// Also On libstdc++, pubsetbuf() silently ignores the supplied buffer and leaves internal pointers null.
29+
30+
class ifixedbuf : public std::streambuf {
31+
public:
32+
ifixedbuf(char* data, std::size_t size) {
33+
// Always manually set the read pointers.
34+
// pubsetbuf() is unreliable on libstdc++, and MSVC doesn't implement it.
35+
this->setg(data, data, data + size);
36+
}
37+
};
38+
39+
class ofixedbuf : public std::streambuf {
40+
public:
41+
ofixedbuf(char* data, std::size_t size) {
42+
// Always set buffer manually — pubsetbuf is useless on libstdc++
43+
this->setp(data, data + size);
44+
}
45+
46+
std::size_t written() const {
47+
return this->pptr() - this->pbase();
3548
}
3649
};
3750

38-
template <typename T>
39-
struct istreambuf : public std::basic_streambuf<T, std::char_traits<T> >
40-
{
41-
istreambuf(T* buffer, std::streamsize length) {
42-
this->setg(buffer, buffer, &buffer[length]);
51+
class iofixedbuf : public std::streambuf {
52+
public:
53+
iofixedbuf(char* data, std::size_t size) {
54+
this->setg(data, data, data + size);
55+
this->setp(data, data + size);
4356
}
4457
};
4558

0 commit comments

Comments
 (0)