Skip to content

Commit

Permalink
Fix some size truncation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerkaraszewski committed Jul 25, 2024
1 parent 7d326b8 commit 0a5f357
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
24 changes: 11 additions & 13 deletions libstuff/libstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ string SUnescape(const char* lhs, char escaper) {
else if (utfValue <= 0x07ff) {
// UTF-8 2 byte header is 110.
// 1100.0000 | Top 5 bits of utfValue
char byte = 0xc0 | (utfValue >> 6);
char byte = (char)(0xc0 | (utfValue >> 6));
working += byte;

// Cancel out the bits we just used.
Expand All @@ -526,7 +526,7 @@ string SUnescape(const char* lhs, char escaper) {
else if (utfValue <= 0xffff) {
// UTF-8 3 byte header is 1110.
// 1110.0000 | Top 4 bits of utfValue.
char byte = 0xe0 | (utfValue >> 12);
char byte = (char)(0xe0 | (utfValue >> 12));
working += byte;

// Cancel out the bits we just used.
Expand Down Expand Up @@ -1705,7 +1705,7 @@ string SGUnzip (const string& content) {
return "";
}

strm.avail_in = content.size();
strm.avail_in = (decltype(strm.avail_in))content.size();
strm.next_in = (unsigned char*)content.c_str();

do {
Expand Down Expand Up @@ -2006,14 +2006,12 @@ bool S_recvappend(int s, SFastBuffer& recvBuffer) {

// Keep trying to receive as long as we can
char buffer[4096];
int totalRecv = 0;
ssize_t numRecv = 0;
sockaddr_in fromAddr;
socklen_t fromAddrLen = sizeof(fromAddr);
while ((numRecv = recvfrom(s, buffer, sizeof(buffer), 0, (sockaddr*)&fromAddr, &fromAddrLen)) > 0) {
// Got some more data
recvBuffer.append(buffer, numRecv);
totalRecv += numRecv;

// If this is a blocking socket, don't try again, once is enough
if (blocking) {
Expand Down Expand Up @@ -2110,7 +2108,7 @@ int S_poll(fd_map& fdm, uint64_t timeout) {

// Timeout is specified in microseconds, but poll uses milliseconds, so we divide by 1000.
int timeoutVal = int(timeout / 1000);
int returnValue = poll(&pollvec[0], fdm.size(), timeoutVal);
int returnValue = poll(&pollvec[0], (nfds_t)fdm.size(), timeoutVal);

// And write our returned events back to our original structure.
for (pollfd pfd : pollvec) {
Expand Down Expand Up @@ -2285,7 +2283,7 @@ bool SFileSave(const string& path, const string& buffer) {
// --------------------------------------------------------------------------
bool SFileCopy(const string& fromPath, const string& toPath) {
// Figure out the size of the file we're copying.
uint64_t fromSize = SFileSize(fromPath);
size_t fromSize = SFileSize(fromPath);
if (!fromSize) {
SWARN("File " << fromPath << " is empty! Copying anyway.");
}
Expand All @@ -2309,8 +2307,8 @@ bool SFileCopy(const string& fromPath, const string& toPath) {
// Read and write
char buf[1024 * 64];
size_t numRead = 0;
uint64_t completeBytes = 0;
int completePercent = 0;
size_t completeBytes = 0;
size_t completePercent = 0;
bool readAny = false;
bool writtenAny = false;
while ((numRead = fread(buf, 1, sizeof(buf), from)) > 0) {
Expand All @@ -2327,7 +2325,7 @@ bool SFileCopy(const string& fromPath, const string& toPath) {
SINFO("Wrote first " << numRead << " bytes to " << toPath << ".");
}
completeBytes += numRead;
int percent = fromSize ? ((completeBytes * 100) / fromSize) : 0;
size_t percent = fromSize ? ((completeBytes * 100) / fromSize) : 0;
if (percent > completePercent) {
SINFO("Copying " << fromPath << " to " << toPath << " is " << percent << "% complete.");
completePercent = percent;
Expand Down Expand Up @@ -2400,7 +2398,7 @@ string SHashSHA256(const string& buffer) {

// --------------------------------------------------------------------------

string SEncodeBase64(const unsigned char* buffer, int size) {
string SEncodeBase64(const unsigned char* buffer, size_t size) {
// First, get the required buffer size
size_t olen = 0;
mbedtls_base64_encode(0, 0, &olen, buffer, size);
Expand All @@ -2417,7 +2415,7 @@ string SEncodeBase64(const string& bufferString) {
}

// --------------------------------------------------------------------------
string SDecodeBase64(const unsigned char* buffer, int size) {
string SDecodeBase64(const unsigned char* buffer, size_t size) {
// First, get the required buffer size
size_t olen = 0;
mbedtls_base64_decode(0, 0, &olen, buffer, size);
Expand Down Expand Up @@ -2578,7 +2576,7 @@ int SQuery(sqlite3* db, const char* e, const string& sql, SQResult& result, int6
// Calling strlen() or any function that iterates across the whole string here is a giant performance problem, and all the operations chosen here have
// been picked specifically to avoid that.
size_t maxLength = sql.size() - (statementRemainder - sql.c_str()) + 1;
error = sqlite3_prepare_v2(db, statementRemainder, maxLength, &preparedStatement, &statementRemainder);
error = sqlite3_prepare_v2(db, statementRemainder, (int)maxLength, &preparedStatement, &statementRemainder);
if (isSyncThread) {
prepareTimeUS += STimeNow() - beforePrepare;
}
Expand Down
10 changes: 5 additions & 5 deletions libstuff/libstuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,19 @@ namespace std {
bool is_lock_free() const {
return false;
}
void store(string desired, std::memory_order order = std::memory_order_seq_cst) {
void store(string desired, std::memory_order) {
lock_guard<decltype(m)> l(m);
_string = desired;
};
string load(std::memory_order order = std::memory_order_seq_cst) const {
string load(std::memory_order) const {
lock_guard<decltype(m)> l(m);
return _string;
}
operator string() const {
lock_guard<decltype(m)> l(m);
return _string;
}
string exchange(string desired, std::memory_order order = std::memory_order_seq_cst) {
string exchange(string desired, std::memory_order) {
lock_guard<decltype(m)> l(m);
string existing = _string;
_string = desired;
Expand Down Expand Up @@ -549,9 +549,9 @@ string SHashSHA1(const string& buffer);
string SHashSHA256(const string& buffer);

// Various encoding/decoding functions
string SEncodeBase64(const unsigned char* buffer, const int size);
string SEncodeBase64(const unsigned char* buffer, const size_t size);
string SEncodeBase64(const string& buffer);
string SDecodeBase64(const unsigned char* buffer, const int size);
string SDecodeBase64(const unsigned char* buffer, const size_t size);
string SDecodeBase64(const string& buffer);

// HMAC (for use with Amazon S3)
Expand Down

0 comments on commit 0a5f357

Please sign in to comment.