Use size_t for lengths in stream objects

Provides safety against them accidentally becoming negative because
of bugs in the calculations.

Also does the same to CharArray and friends as they were strongly
connection to the stream objects.
This commit is contained in:
Pierre Ossman
2019-09-23 11:00:17 +02:00
committed by Lauri Kasanen
parent 346fccb96c
commit 259f1055cb
31 changed files with 180 additions and 178 deletions

View File

@@ -44,7 +44,7 @@ namespace rdr {
// itemSize bytes. Returns the number of items which fit (up to a maximum
// of nItems).
inline int check(int itemSize, int nItems=1)
inline size_t check(size_t itemSize, size_t nItems=1)
{
if (ptr + itemSize * nItems > end) {
if (ptr + itemSize > end)
@@ -76,13 +76,13 @@ namespace rdr {
writeBytes(str, len);
}
inline void pad(int bytes) {
inline void pad(size_t bytes) {
while (bytes-- > 0) writeU8(0);
}
inline void skip(int bytes) {
inline void skip(size_t bytes) {
while (bytes > 0) {
int n = check(1, bytes);
size_t n = check(1, bytes);
ptr += n;
bytes -= n;
}
@@ -90,11 +90,11 @@ namespace rdr {
// writeBytes() writes an exact number of bytes.
void writeBytes(const void* data, int length) {
void writeBytes(const void* data, size_t length) {
const U8* dataPtr = (const U8*)data;
const U8* dataEnd = dataPtr + length;
while (dataPtr < dataEnd) {
int n = check(1, dataEnd - dataPtr);
size_t n = check(1, dataEnd - dataPtr);
memcpy(ptr, dataPtr, n);
ptr += n;
dataPtr += n;
@@ -103,9 +103,9 @@ namespace rdr {
// copyBytes() efficiently transfers data between streams
void copyBytes(InStream* is, int length) {
void copyBytes(InStream* is, size_t length) {
while (length > 0) {
int n = check(1, length);
size_t n = check(1, length);
is->readBytes(ptr, n);
ptr += n;
length -= n;
@@ -124,7 +124,7 @@ namespace rdr {
// length() returns the length of the stream.
virtual int length() = 0;
virtual size_t length() = 0;
// flush() requests that the stream be flushed.
@@ -145,7 +145,7 @@ namespace rdr {
// the number of items which fit (up to a maximum of nItems). itemSize is
// supposed to be "small" (a few bytes).
virtual int overrun(int itemSize, int nItems) = 0;
virtual size_t overrun(size_t itemSize, size_t nItems) = 0;
protected: