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

@@ -32,7 +32,7 @@
using namespace rdr;
const int DEFAULT_BUF_LEN = 256;
const size_t DEFAULT_BUF_LEN = 256;
unsigned int RandomStream::seed;
@@ -83,11 +83,11 @@ RandomStream::~RandomStream() {
#endif
}
int RandomStream::pos() {
size_t RandomStream::pos() {
return offset + ptr - start;
}
int RandomStream::overrun(int itemSize, int nItems, bool wait) {
size_t RandomStream::overrun(size_t itemSize, size_t nItems, bool wait) {
if (itemSize > DEFAULT_BUF_LEN)
throw Exception("RandomStream overrun: max itemSize exceeded");
@@ -98,7 +98,7 @@ int RandomStream::overrun(int itemSize, int nItems, bool wait) {
offset += ptr - start;
ptr = start;
int length = start + DEFAULT_BUF_LEN - end;
size_t length = start + DEFAULT_BUF_LEN - end;
#ifdef RFB_HAVE_WINCRYPT
if (provider) {
@@ -109,7 +109,7 @@ int RandomStream::overrun(int itemSize, int nItems, bool wait) {
#else
#ifndef WIN32
if (fp) {
int n = fread((U8*)end, length, 1, fp);
size_t n = fread((U8*)end, length, 1, fp);
if (n != 1)
throw rdr::SystemException("reading /dev/urandom or /dev/random failed",
errno);
@@ -119,11 +119,11 @@ int RandomStream::overrun(int itemSize, int nItems, bool wait) {
{
#endif
#endif
for (int i=0; i<length; i++)
for (size_t i=0; i<length; i++)
*(U8*)end++ = (int) (256.0*rand()/(RAND_MAX+1.0));
}
if (itemSize * nItems > end - ptr)
if (itemSize * nItems > (size_t)(end - ptr))
nItems = (end - ptr) / itemSize;
return nItems;