Skip to content

Commit

Permalink
Use size_t for lengths in stream objects
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CendioOssman committed Nov 15, 2019
1 parent 4ff58f0 commit 0943c00
Show file tree
Hide file tree
Showing 32 changed files with 184 additions and 182 deletions.
20 changes: 10 additions & 10 deletions common/rdr/FdInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ using namespace rdr;
enum { DEFAULT_BUF_SIZE = 8192,
MIN_BULK_SIZE = 1024 };

FdInStream::FdInStream(int fd_, int timeoutms_, int bufSize_,
FdInStream::FdInStream(int fd_, int timeoutms_, size_t bufSize_,
bool closeWhenDone_)
: fd(fd_), closeWhenDone(closeWhenDone_),
timeoutms(timeoutms_), blockCallback(0),
Expand All @@ -67,7 +67,7 @@ FdInStream::FdInStream(int fd_, int timeoutms_, int bufSize_,
}

FdInStream::FdInStream(int fd_, FdInStreamBlockCallback* blockCallback_,
int bufSize_)
size_t bufSize_)
: fd(fd_), timeoutms(0), blockCallback(blockCallback_),
timing(false), timeWaitedIn100us(5), timedKbits(0),
bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
Expand All @@ -92,12 +92,12 @@ void FdInStream::setBlockCallback(FdInStreamBlockCallback* blockCallback_)
timeoutms = 0;
}

int FdInStream::pos()
size_t FdInStream::pos()
{
return offset + ptr - start;
}

void FdInStream::readBytes(void* data, int length)
void FdInStream::readBytes(void* data, size_t length)
{
if (length < MIN_BULK_SIZE) {
InStream::readBytes(data, length);
Expand All @@ -106,7 +106,7 @@ void FdInStream::readBytes(void* data, int length)

U8* dataPtr = (U8*)data;

int n = end - ptr;
size_t n = end - ptr;
if (n > length) n = length;

memcpy(dataPtr, ptr, n);
Expand All @@ -123,7 +123,7 @@ void FdInStream::readBytes(void* data, int length)
}


int FdInStream::overrun(int itemSize, int nItems, bool wait)
size_t FdInStream::overrun(size_t itemSize, size_t nItems, bool wait)
{
if (itemSize > bufSize)
throw Exception("FdInStream overrun: max itemSize exceeded");
Expand All @@ -135,7 +135,7 @@ int FdInStream::overrun(int itemSize, int nItems, bool wait)
end -= ptr - start;
ptr = start;

int bytes_to_read;
size_t bytes_to_read;
while (end < start + itemSize) {
bytes_to_read = start + bufSize - end;
if (!timing) {
Expand All @@ -147,12 +147,12 @@ int FdInStream::overrun(int itemSize, int nItems, bool wait)
// bytes is ineffecient.
bytes_to_read = vncmin(bytes_to_read, vncmax(itemSize*nItems, 8));
}
int n = readWithTimeoutOrCallback((U8*)end, bytes_to_read, wait);
size_t n = readWithTimeoutOrCallback((U8*)end, bytes_to_read, wait);
if (n == 0) return 0;
end += n;
}

if (itemSize * nItems > end - ptr)
if (itemSize * nItems > (size_t)(end - ptr))
nItems = (end - ptr) / itemSize;

return nItems;
Expand All @@ -171,7 +171,7 @@ int FdInStream::overrun(int itemSize, int nItems, bool wait)
// returning EINTR.
//

int FdInStream::readWithTimeoutOrCallback(void* buf, int len, bool wait)
size_t FdInStream::readWithTimeoutOrCallback(void* buf, size_t len, bool wait)
{
struct timeval before, after;
if (timing)
Expand Down
17 changes: 9 additions & 8 deletions common/rdr/FdInStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,28 @@ namespace rdr {

public:

FdInStream(int fd, int timeoutms=-1, int bufSize=0,
FdInStream(int fd, int timeoutms=-1, size_t bufSize=0,
bool closeWhenDone_=false);
FdInStream(int fd, FdInStreamBlockCallback* blockCallback, int bufSize=0);
FdInStream(int fd, FdInStreamBlockCallback* blockCallback,
size_t bufSize=0);
virtual ~FdInStream();

void setTimeout(int timeoutms);
void setBlockCallback(FdInStreamBlockCallback* blockCallback);
int getFd() { return fd; }
int pos();
void readBytes(void* data, int length);
size_t pos();
void readBytes(void* data, size_t length);

void startTiming();
void stopTiming();
unsigned int kbitsPerSecond();
unsigned int timeWaited() { return timeWaitedIn100us; }

protected:
int overrun(int itemSize, int nItems, bool wait);
size_t overrun(size_t itemSize, size_t nItems, bool wait);

private:
int readWithTimeoutOrCallback(void* buf, int len, bool wait=true);
size_t readWithTimeoutOrCallback(void* buf, size_t len, bool wait=true);

int fd;
bool closeWhenDone;
Expand All @@ -68,8 +69,8 @@ namespace rdr {
unsigned int timeWaitedIn100us;
unsigned int timedKbits;

int bufSize;
int offset;
size_t bufSize;
size_t offset;
U8* start;
};

Expand Down
20 changes: 10 additions & 10 deletions common/rdr/FdOutStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ using namespace rdr;

enum { DEFAULT_BUF_SIZE = 16384 };

FdOutStream::FdOutStream(int fd_, bool blocking_, int timeoutms_, int bufSize_)
FdOutStream::FdOutStream(int fd_, bool blocking_, int timeoutms_, size_t bufSize_)
: fd(fd_), blocking(blocking_), timeoutms(timeoutms_),
bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
{
Expand Down Expand Up @@ -79,7 +79,7 @@ void FdOutStream::setBlocking(bool blocking_) {
blocking = blocking_;
}

int FdOutStream::length()
size_t FdOutStream::length()
{
return offset + ptr - sentUpTo;
}
Expand All @@ -97,9 +97,9 @@ unsigned FdOutStream::getIdleTime()
void FdOutStream::flush()
{
while (sentUpTo < ptr) {
int n = writeWithTimeout((const void*) sentUpTo,
ptr - sentUpTo,
blocking? timeoutms : 0);
size_t n = writeWithTimeout((const void*) sentUpTo,
ptr - sentUpTo,
blocking? timeoutms : 0);

// Timeout?
if (n == 0) {
Expand All @@ -120,7 +120,7 @@ void FdOutStream::flush()
}


int FdOutStream::overrun(int itemSize, int nItems)
size_t FdOutStream::overrun(size_t itemSize, size_t nItems)
{
if (itemSize > bufSize)
throw Exception("FdOutStream overrun: max itemSize exceeded");
Expand All @@ -129,10 +129,10 @@ int FdOutStream::overrun(int itemSize, int nItems)
flush();

// Still not enough space?
if (itemSize > end - ptr) {
if (itemSize > (size_t)(end - ptr)) {
// Can we shuffle things around?
// (don't do this if it gains us less than 25%)
if ((sentUpTo - start > bufSize / 4) &&
if (((size_t)(sentUpTo - start) > bufSize / 4) &&
(itemSize < bufSize - (ptr - sentUpTo))) {
memmove(start, sentUpTo, ptr - sentUpTo);
ptr = start + (ptr - sentUpTo);
Expand All @@ -150,7 +150,7 @@ int FdOutStream::overrun(int itemSize, int nItems)
}

// Can we fit all the items asked for?
if (itemSize * nItems > end - ptr)
if (itemSize * nItems > (size_t)(end - ptr))
nItems = (end - ptr) / itemSize;

return nItems;
Expand All @@ -166,7 +166,7 @@ int FdOutStream::overrun(int itemSize, int nItems)
// select() and send() returning EINTR.
//

int FdOutStream::writeWithTimeout(const void* data, int length, int timeoutms)
size_t FdOutStream::writeWithTimeout(const void* data, size_t length, int timeoutms)
{
int n;

Expand Down
12 changes: 6 additions & 6 deletions common/rdr/FdOutStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,28 @@ namespace rdr {

public:

FdOutStream(int fd, bool blocking=true, int timeoutms=-1, int bufSize=0);
FdOutStream(int fd, bool blocking=true, int timeoutms=-1, size_t bufSize=0);
virtual ~FdOutStream();

void setTimeout(int timeoutms);
void setBlocking(bool blocking);
int getFd() { return fd; }

void flush();
int length();
size_t length();

int bufferUsage();

unsigned getIdleTime();

private:
int overrun(int itemSize, int nItems);
int writeWithTimeout(const void* data, int length, int timeoutms);
size_t overrun(size_t itemSize, size_t nItems);
size_t writeWithTimeout(const void* data, size_t length, int timeoutms);
int fd;
bool blocking;
int timeoutms;
int bufSize;
int offset;
size_t bufSize;
size_t offset;
U8* start;
U8* sentUpTo;
struct timeval lastWrite;
Expand Down
8 changes: 4 additions & 4 deletions common/rdr/FileInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ void FileInStream::reset(void) {
ptr = end = b;
}

int FileInStream::pos()
size_t FileInStream::pos()
{
if (!file)
throw Exception("File is not open");

return ftell(file) + ptr - b;
}

int FileInStream::overrun(int itemSize, int nItems, bool wait)
size_t FileInStream::overrun(size_t itemSize, size_t nItems, bool wait)
{
if (itemSize > (int)sizeof(b))
if (itemSize > sizeof(b))
throw Exception("FileInStream overrun: max itemSize exceeded");

if (end - ptr != 0)
Expand All @@ -80,7 +80,7 @@ int FileInStream::overrun(int itemSize, int nItems, bool wait)
end += b + sizeof(b) - end;
}

if (itemSize * nItems > end - ptr)
if (itemSize * nItems > (size_t)(end - ptr))
nItems = (end - ptr) / itemSize;

return nItems;
Expand Down
4 changes: 2 additions & 2 deletions common/rdr/FileInStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ namespace rdr {

void reset(void);

int pos();
size_t pos();

protected:
int overrun(int itemSize, int nItems, bool wait = true);
size_t overrun(size_t itemSize, size_t nItems, bool wait = true);

private:
U8 b[131072];
Expand Down
20 changes: 10 additions & 10 deletions common/rdr/HexInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const int DEFAULT_BUF_LEN = 16384;

static inline int min(int a, int b) {return a<b ? a : b;}

HexInStream::HexInStream(InStream& is, int bufSize_)
HexInStream::HexInStream(InStream& is, size_t bufSize_)
: bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_LEN), offset(0), in_stream(is)
{
ptr = end = start = new U8[bufSize];
Expand All @@ -50,16 +50,16 @@ bool HexInStream::readHexAndShift(char c, int* v) {
return true;
}

bool HexInStream::hexStrToBin(const char* s, char** data, int* length) {
int l=strlen(s);
bool HexInStream::hexStrToBin(const char* s, char** data, size_t* length) {
size_t l=strlen(s);
if ((l % 2) == 0) {
delete [] *data;
*data = 0; *length = 0;
if (l == 0)
return true;
*data = new char[l/2];
*length = l/2;
for(int i=0;i<l;i+=2) {
for(size_t i=0;i<l;i+=2) {
int byte = 0;
if (!readHexAndShift(s[i], &byte) ||
!readHexAndShift(s[i+1], &byte))
Expand All @@ -76,11 +76,11 @@ bool HexInStream::hexStrToBin(const char* s, char** data, int* length) {
}


int HexInStream::pos() {
size_t HexInStream::pos() {
return offset + ptr - start;
}

int HexInStream::overrun(int itemSize, int nItems, bool wait) {
size_t HexInStream::overrun(size_t itemSize, size_t nItems, bool wait) {
if (itemSize > bufSize)
throw Exception("HexInStream overrun: max itemSize exceeded");

Expand All @@ -92,14 +92,14 @@ int HexInStream::overrun(int itemSize, int nItems, bool wait) {
ptr = start;

while (end < ptr + itemSize) {
int n = in_stream.check(2, 1, wait);
size_t n = in_stream.check(2, 1, wait);
if (n == 0) return 0;
const U8* iptr = in_stream.getptr();
const U8* eptr = in_stream.getend();
int length = min((eptr - iptr)/2, start + bufSize - end);
size_t length = min((eptr - iptr)/2, start + bufSize - end);

U8* optr = (U8*) end;
for (int i=0; i<length; i++) {
for (size_t i=0; i<length; i++) {
int v = 0;
readHexAndShift(iptr[i*2], &v);
readHexAndShift(iptr[i*2+1], &v);
Expand All @@ -110,7 +110,7 @@ int HexInStream::overrun(int itemSize, int nItems, bool wait) {
end += length;
}

if (itemSize * nItems > end - ptr)
if (itemSize * nItems > (size_t)(end - ptr))
nItems = (end - ptr) / itemSize;

return nItems;
Expand Down
12 changes: 6 additions & 6 deletions common/rdr/HexInStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ namespace rdr {
class HexInStream : public InStream {
public:

HexInStream(InStream& is, int bufSize=0);
HexInStream(InStream& is, size_t bufSize=0);
virtual ~HexInStream();

int pos();
size_t pos();

static bool readHexAndShift(char c, int* v);
static bool hexStrToBin(const char* s, char** data, int* length);
static bool hexStrToBin(const char* s, char** data, size_t* length);

protected:
int overrun(int itemSize, int nItems, bool wait);
size_t overrun(size_t itemSize, size_t nItems, bool wait);

private:
int bufSize;
size_t bufSize;
U8* start;
int offset;
size_t offset;

InStream& in_stream;
};
Expand Down
Loading

0 comments on commit 0943c00

Please sign in to comment.