Resolves: ofz#727 don't allow negative sizes or indexes
remove extra size in favour of vector size and don't resize and memcpy data, just use vector::insert Change-Id: I8efb91a8c11fbd862c0458042554cf7e94b813cd Reviewed-on: https://gerrit.libreoffice.org/34891 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Caolán McNamara <caolanm@redhat.com> Tested-by: Caolán McNamara <caolanm@redhat.com>
This commit is contained in:
@@ -22,38 +22,37 @@
|
||||
#include "hstream.hxx"
|
||||
|
||||
HStream::HStream()
|
||||
: size(0)
|
||||
, pos(0)
|
||||
: pos(0)
|
||||
{
|
||||
}
|
||||
|
||||
void HStream::addData(const byte *buf, int aToAdd)
|
||||
void HStream::addData(const byte *buf, size_t aToAdd)
|
||||
{
|
||||
seq.resize(size + aToAdd);
|
||||
memcpy(seq.data() + size, buf, aToAdd);
|
||||
size += aToAdd;
|
||||
seq.insert(seq.end(), buf, buf + aToAdd);
|
||||
}
|
||||
|
||||
int HStream::readBytes(byte * buf, int aToRead)
|
||||
size_t HStream::readBytes(byte * buf, size_t aToRead)
|
||||
{
|
||||
auto size = seq.size();
|
||||
if (aToRead >= (size - pos))
|
||||
aToRead = size - pos;
|
||||
for (int i = 0; i < aToRead; i++)
|
||||
for (size_t i = 0; i < aToRead; ++i)
|
||||
buf[i] = seq[pos++];
|
||||
return aToRead;
|
||||
}
|
||||
|
||||
int HStream::skipBytes(int aToSkip)
|
||||
size_t HStream::skipBytes(size_t aToSkip)
|
||||
{
|
||||
auto size = seq.size();
|
||||
if (aToSkip >= (size - pos))
|
||||
aToSkip = size - pos;
|
||||
pos += aToSkip;
|
||||
return aToSkip;
|
||||
}
|
||||
|
||||
int HStream::available() const
|
||||
size_t HStream::available() const
|
||||
{
|
||||
return size - pos;
|
||||
return seq.size() - pos;
|
||||
}
|
||||
|
||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
||||
|
@@ -34,24 +34,23 @@ class HStream
|
||||
/**
|
||||
*
|
||||
*/
|
||||
void addData( const byte *buf, int aToAdd);
|
||||
void addData( const byte *buf, size_t aToAdd);
|
||||
/**
|
||||
* Read some byte to buf as given size
|
||||
*/
|
||||
int readBytes( byte *buf, int aToRead);
|
||||
size_t readBytes( byte *buf, size_t aToRead);
|
||||
/**
|
||||
* Skip some byte from stream as given size
|
||||
*/
|
||||
int skipBytes( int aToSkip );
|
||||
size_t skipBytes( size_t aToSkip );
|
||||
/**
|
||||
* @returns Size of remained stream
|
||||
*/
|
||||
int available() const;
|
||||
size_t available() const;
|
||||
|
||||
private:
|
||||
int size;
|
||||
std::vector<byte> seq;
|
||||
int pos;
|
||||
size_t pos;
|
||||
};
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user