Skip to content

Commit 0943c00

Browse files
committed
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.
1 parent 4ff58f0 commit 0943c00

32 files changed

+184
-182
lines changed

Diff for: common/rdr/FdInStream.cxx

+10-10
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ using namespace rdr;
5656
enum { DEFAULT_BUF_SIZE = 8192,
5757
MIN_BULK_SIZE = 1024 };
5858

59-
FdInStream::FdInStream(int fd_, int timeoutms_, int bufSize_,
59+
FdInStream::FdInStream(int fd_, int timeoutms_, size_t bufSize_,
6060
bool closeWhenDone_)
6161
: fd(fd_), closeWhenDone(closeWhenDone_),
6262
timeoutms(timeoutms_), blockCallback(0),
@@ -67,7 +67,7 @@ FdInStream::FdInStream(int fd_, int timeoutms_, int bufSize_,
6767
}
6868

6969
FdInStream::FdInStream(int fd_, FdInStreamBlockCallback* blockCallback_,
70-
int bufSize_)
70+
size_t bufSize_)
7171
: fd(fd_), timeoutms(0), blockCallback(blockCallback_),
7272
timing(false), timeWaitedIn100us(5), timedKbits(0),
7373
bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
@@ -92,12 +92,12 @@ void FdInStream::setBlockCallback(FdInStreamBlockCallback* blockCallback_)
9292
timeoutms = 0;
9393
}
9494

95-
int FdInStream::pos()
95+
size_t FdInStream::pos()
9696
{
9797
return offset + ptr - start;
9898
}
9999

100-
void FdInStream::readBytes(void* data, int length)
100+
void FdInStream::readBytes(void* data, size_t length)
101101
{
102102
if (length < MIN_BULK_SIZE) {
103103
InStream::readBytes(data, length);
@@ -106,7 +106,7 @@ void FdInStream::readBytes(void* data, int length)
106106

107107
U8* dataPtr = (U8*)data;
108108

109-
int n = end - ptr;
109+
size_t n = end - ptr;
110110
if (n > length) n = length;
111111

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

125125

126-
int FdInStream::overrun(int itemSize, int nItems, bool wait)
126+
size_t FdInStream::overrun(size_t itemSize, size_t nItems, bool wait)
127127
{
128128
if (itemSize > bufSize)
129129
throw Exception("FdInStream overrun: max itemSize exceeded");
@@ -135,7 +135,7 @@ int FdInStream::overrun(int itemSize, int nItems, bool wait)
135135
end -= ptr - start;
136136
ptr = start;
137137

138-
int bytes_to_read;
138+
size_t bytes_to_read;
139139
while (end < start + itemSize) {
140140
bytes_to_read = start + bufSize - end;
141141
if (!timing) {
@@ -147,12 +147,12 @@ int FdInStream::overrun(int itemSize, int nItems, bool wait)
147147
// bytes is ineffecient.
148148
bytes_to_read = vncmin(bytes_to_read, vncmax(itemSize*nItems, 8));
149149
}
150-
int n = readWithTimeoutOrCallback((U8*)end, bytes_to_read, wait);
150+
size_t n = readWithTimeoutOrCallback((U8*)end, bytes_to_read, wait);
151151
if (n == 0) return 0;
152152
end += n;
153153
}
154154

155-
if (itemSize * nItems > end - ptr)
155+
if (itemSize * nItems > (size_t)(end - ptr))
156156
nItems = (end - ptr) / itemSize;
157157

158158
return nItems;
@@ -171,7 +171,7 @@ int FdInStream::overrun(int itemSize, int nItems, bool wait)
171171
// returning EINTR.
172172
//
173173

174-
int FdInStream::readWithTimeoutOrCallback(void* buf, int len, bool wait)
174+
size_t FdInStream::readWithTimeoutOrCallback(void* buf, size_t len, bool wait)
175175
{
176176
struct timeval before, after;
177177
if (timing)

Diff for: common/rdr/FdInStream.h

+9-8
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,28 @@ namespace rdr {
3737

3838
public:
3939

40-
FdInStream(int fd, int timeoutms=-1, int bufSize=0,
40+
FdInStream(int fd, int timeoutms=-1, size_t bufSize=0,
4141
bool closeWhenDone_=false);
42-
FdInStream(int fd, FdInStreamBlockCallback* blockCallback, int bufSize=0);
42+
FdInStream(int fd, FdInStreamBlockCallback* blockCallback,
43+
size_t bufSize=0);
4344
virtual ~FdInStream();
4445

4546
void setTimeout(int timeoutms);
4647
void setBlockCallback(FdInStreamBlockCallback* blockCallback);
4748
int getFd() { return fd; }
48-
int pos();
49-
void readBytes(void* data, int length);
49+
size_t pos();
50+
void readBytes(void* data, size_t length);
5051

5152
void startTiming();
5253
void stopTiming();
5354
unsigned int kbitsPerSecond();
5455
unsigned int timeWaited() { return timeWaitedIn100us; }
5556

5657
protected:
57-
int overrun(int itemSize, int nItems, bool wait);
58+
size_t overrun(size_t itemSize, size_t nItems, bool wait);
5859

5960
private:
60-
int readWithTimeoutOrCallback(void* buf, int len, bool wait=true);
61+
size_t readWithTimeoutOrCallback(void* buf, size_t len, bool wait=true);
6162

6263
int fd;
6364
bool closeWhenDone;
@@ -68,8 +69,8 @@ namespace rdr {
6869
unsigned int timeWaitedIn100us;
6970
unsigned int timedKbits;
7071

71-
int bufSize;
72-
int offset;
72+
size_t bufSize;
73+
size_t offset;
7374
U8* start;
7475
};
7576

Diff for: common/rdr/FdOutStream.cxx

+10-10
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ using namespace rdr;
5151

5252
enum { DEFAULT_BUF_SIZE = 16384 };
5353

54-
FdOutStream::FdOutStream(int fd_, bool blocking_, int timeoutms_, int bufSize_)
54+
FdOutStream::FdOutStream(int fd_, bool blocking_, int timeoutms_, size_t bufSize_)
5555
: fd(fd_), blocking(blocking_), timeoutms(timeoutms_),
5656
bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
5757
{
@@ -79,7 +79,7 @@ void FdOutStream::setBlocking(bool blocking_) {
7979
blocking = blocking_;
8080
}
8181

82-
int FdOutStream::length()
82+
size_t FdOutStream::length()
8383
{
8484
return offset + ptr - sentUpTo;
8585
}
@@ -97,9 +97,9 @@ unsigned FdOutStream::getIdleTime()
9797
void FdOutStream::flush()
9898
{
9999
while (sentUpTo < ptr) {
100-
int n = writeWithTimeout((const void*) sentUpTo,
101-
ptr - sentUpTo,
102-
blocking? timeoutms : 0);
100+
size_t n = writeWithTimeout((const void*) sentUpTo,
101+
ptr - sentUpTo,
102+
blocking? timeoutms : 0);
103103

104104
// Timeout?
105105
if (n == 0) {
@@ -120,7 +120,7 @@ void FdOutStream::flush()
120120
}
121121

122122

123-
int FdOutStream::overrun(int itemSize, int nItems)
123+
size_t FdOutStream::overrun(size_t itemSize, size_t nItems)
124124
{
125125
if (itemSize > bufSize)
126126
throw Exception("FdOutStream overrun: max itemSize exceeded");
@@ -129,10 +129,10 @@ int FdOutStream::overrun(int itemSize, int nItems)
129129
flush();
130130

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

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

156156
return nItems;
@@ -166,7 +166,7 @@ int FdOutStream::overrun(int itemSize, int nItems)
166166
// select() and send() returning EINTR.
167167
//
168168

169-
int FdOutStream::writeWithTimeout(const void* data, int length, int timeoutms)
169+
size_t FdOutStream::writeWithTimeout(const void* data, size_t length, int timeoutms)
170170
{
171171
int n;
172172

Diff for: common/rdr/FdOutStream.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,28 @@ namespace rdr {
3434

3535
public:
3636

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

4040
void setTimeout(int timeoutms);
4141
void setBlocking(bool blocking);
4242
int getFd() { return fd; }
4343

4444
void flush();
45-
int length();
45+
size_t length();
4646

4747
int bufferUsage();
4848

4949
unsigned getIdleTime();
5050

5151
private:
52-
int overrun(int itemSize, int nItems);
53-
int writeWithTimeout(const void* data, int length, int timeoutms);
52+
size_t overrun(size_t itemSize, size_t nItems);
53+
size_t writeWithTimeout(const void* data, size_t length, int timeoutms);
5454
int fd;
5555
bool blocking;
5656
int timeoutms;
57-
int bufSize;
58-
int offset;
57+
size_t bufSize;
58+
size_t offset;
5959
U8* start;
6060
U8* sentUpTo;
6161
struct timeval lastWrite;

Diff for: common/rdr/FileInStream.cxx

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ void FileInStream::reset(void) {
4848
ptr = end = b;
4949
}
5050

51-
int FileInStream::pos()
51+
size_t FileInStream::pos()
5252
{
5353
if (!file)
5454
throw Exception("File is not open");
5555

5656
return ftell(file) + ptr - b;
5757
}
5858

59-
int FileInStream::overrun(int itemSize, int nItems, bool wait)
59+
size_t FileInStream::overrun(size_t itemSize, size_t nItems, bool wait)
6060
{
61-
if (itemSize > (int)sizeof(b))
61+
if (itemSize > sizeof(b))
6262
throw Exception("FileInStream overrun: max itemSize exceeded");
6363

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

83-
if (itemSize * nItems > end - ptr)
83+
if (itemSize * nItems > (size_t)(end - ptr))
8484
nItems = (end - ptr) / itemSize;
8585

8686
return nItems;

Diff for: common/rdr/FileInStream.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ namespace rdr {
3535

3636
void reset(void);
3737

38-
int pos();
38+
size_t pos();
3939

4040
protected:
41-
int overrun(int itemSize, int nItems, bool wait = true);
41+
size_t overrun(size_t itemSize, size_t nItems, bool wait = true);
4242

4343
private:
4444
U8 b[131072];

Diff for: common/rdr/HexInStream.cxx

+10-10
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const int DEFAULT_BUF_LEN = 16384;
2828

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

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

53-
bool HexInStream::hexStrToBin(const char* s, char** data, int* length) {
54-
int l=strlen(s);
53+
bool HexInStream::hexStrToBin(const char* s, char** data, size_t* length) {
54+
size_t l=strlen(s);
5555
if ((l % 2) == 0) {
5656
delete [] *data;
5757
*data = 0; *length = 0;
5858
if (l == 0)
5959
return true;
6060
*data = new char[l/2];
6161
*length = l/2;
62-
for(int i=0;i<l;i+=2) {
62+
for(size_t i=0;i<l;i+=2) {
6363
int byte = 0;
6464
if (!readHexAndShift(s[i], &byte) ||
6565
!readHexAndShift(s[i+1], &byte))
@@ -76,11 +76,11 @@ bool HexInStream::hexStrToBin(const char* s, char** data, int* length) {
7676
}
7777

7878

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

83-
int HexInStream::overrun(int itemSize, int nItems, bool wait) {
83+
size_t HexInStream::overrun(size_t itemSize, size_t nItems, bool wait) {
8484
if (itemSize > bufSize)
8585
throw Exception("HexInStream overrun: max itemSize exceeded");
8686

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

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

101101
U8* optr = (U8*) end;
102-
for (int i=0; i<length; i++) {
102+
for (size_t i=0; i<length; i++) {
103103
int v = 0;
104104
readHexAndShift(iptr[i*2], &v);
105105
readHexAndShift(iptr[i*2+1], &v);
@@ -110,7 +110,7 @@ int HexInStream::overrun(int itemSize, int nItems, bool wait) {
110110
end += length;
111111
}
112112

113-
if (itemSize * nItems > end - ptr)
113+
if (itemSize * nItems > (size_t)(end - ptr))
114114
nItems = (end - ptr) / itemSize;
115115

116116
return nItems;

Diff for: common/rdr/HexInStream.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ namespace rdr {
2626
class HexInStream : public InStream {
2727
public:
2828

29-
HexInStream(InStream& is, int bufSize=0);
29+
HexInStream(InStream& is, size_t bufSize=0);
3030
virtual ~HexInStream();
3131

32-
int pos();
32+
size_t pos();
3333

3434
static bool readHexAndShift(char c, int* v);
35-
static bool hexStrToBin(const char* s, char** data, int* length);
35+
static bool hexStrToBin(const char* s, char** data, size_t* length);
3636

3737
protected:
38-
int overrun(int itemSize, int nItems, bool wait);
38+
size_t overrun(size_t itemSize, size_t nItems, bool wait);
3939

4040
private:
41-
int bufSize;
41+
size_t bufSize;
4242
U8* start;
43-
int offset;
43+
size_t offset;
4444

4545
InStream& in_stream;
4646
};

0 commit comments

Comments
 (0)