Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions ixwebsocket/IXBase64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#ifndef _MACARON_BASE64_H_
#define _MACARON_BASE64_H_

/**
* The MIT License (MIT)
* Copyright (c) 2016 tomykaira
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <cstdint>
#include <string>

namespace macaron {

class Base64 {
public:

static std::string Encode(const std::string data) {
static constexpr char sEncodingTable[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};

size_t in_len = data.size();
size_t out_len = 4 * ((in_len + 2) / 3);
std::string ret(out_len, '\0');
size_t i;
char *p = const_cast<char*>(ret.c_str());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Undefined behavior: modifying string through c_str().

const_cast<char*>(ret.c_str()) and then writing through p is undefined behavior. The c_str() method returns a pointer to a read-only buffer, and modifying it violates the C++ standard.

🐛 Proposed fix using legal mutable access
-    char *p = const_cast<char*>(ret.c_str());
+    char *p = &ret[0];  // Legal mutable access in C++11 and later

Alternatively in C++17+: char *p = ret.data();

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
char *p = const_cast<char*>(ret.c_str());
char *p = &ret[0]; // Legal mutable access in C++11 and later
🤖 Prompt for AI Agents
In `@ixwebsocket/IXBase64.h` at line 52, The code currently obtains a writable
pointer via const_cast<char*>(ret.c_str()) which is undefined behavior; change
the pointer acquisition to use a legal mutable buffer (e.g., use ret.data() in
C++17+ or ensure a mutable std::string copy) so that the pointer `p` points to a
modifiable buffer; update the reference where `p` is used accordingly (look for
`char *p = const_cast<char*>(ret.c_str())`, variable `p`, and the string `ret`
in IXBase64.h and replace with a safe approach such as `char *p = ret.data()` or
create a mutable copy before writing).


for (i = 0; i < in_len - 2; i += 3) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int) (data[i + 2] & 0xC0) >> 6)];
*p++ = sEncodingTable[data[i + 2] & 0x3F];
}
Comment on lines +54 to +59
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Integer underflow causes out-of-bounds access on small inputs.

The loop condition i < in_len - 2 uses unsigned size_t arithmetic. When in_len is 0, 1, or 2, the expression in_len - 2 underflows to a massive value (~2^64), causing the loop to execute and access data[i] out of bounds.

🐛 Proposed fix to prevent underflow
-    for (i = 0; i < in_len - 2; i += 3) {
+    for (i = 0; in_len >= 3 && i < in_len - 2; i += 3) {

Or restructure to check in_len before entering the loop.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for (i = 0; i < in_len - 2; i += 3) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int) (data[i + 2] & 0xC0) >> 6)];
*p++ = sEncodingTable[data[i + 2] & 0x3F];
}
for (i = 0; in_len >= 3 && i < in_len - 2; i += 3) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int) (data[i + 2] & 0xC0) >> 6)];
*p++ = sEncodingTable[data[i + 2] & 0x3F];
}
🤖 Prompt for AI Agents
In `@ixwebsocket/IXBase64.h` around lines 54 - 59, The for-loop in the Base64
encoder (the block iterating with i and using data, in_len, and sEncodingTable)
can underflow because it uses the unsigned expression "in_len - 2"; change the
loop guard to a safe check such as testing in_len before the loop or using an
index comparison that cannot underflow (e.g., use "i + 2 < in_len" or
early-return/skip when in_len < 3) so the body that reads data[i], data[i+1],
data[i+2] cannot run on inputs of length 0, 1, or 2.

if (i < in_len) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
if (i == (in_len - 1)) {
*p++ = sEncodingTable[((data[i] & 0x3) << 4)];
*p++ = '=';
}
else {
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
}
*p++ = '=';
}

return ret;
}

static std::string Decode(const std::string& input, std::string& out) {
static constexpr unsigned char kDecodingTable[] = {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
};

size_t in_len = input.size();
if (in_len % 4 != 0) return "Input data size is not a multiple of 4";

size_t out_len = in_len / 4 * 3;
if (input[in_len - 1] == '=') out_len--;
if (input[in_len - 2] == '=') out_len--;
Comment on lines +96 to +101
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Out-of-bounds read on empty input.

When in_len == 0, the check in_len % 4 != 0 passes (0 % 4 == 0), but lines 100-101 access input[in_len - 1] and input[in_len - 2], which resolve to input[-1] and input[-2] — undefined behavior and memory corruption.

🐛 Proposed fix to handle empty input
     size_t in_len = input.size();
+    if (in_len == 0) {
+      out.clear();
+      return "";
+    }
     if (in_len % 4 != 0) return "Input data size is not a multiple of 4";
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
size_t in_len = input.size();
if (in_len % 4 != 0) return "Input data size is not a multiple of 4";
size_t out_len = in_len / 4 * 3;
if (input[in_len - 1] == '=') out_len--;
if (input[in_len - 2] == '=') out_len--;
size_t in_len = input.size();
if (in_len == 0) {
out.clear();
return "";
}
if (in_len % 4 != 0) return "Input data size is not a multiple of 4";
size_t out_len = in_len / 4 * 3;
if (input[in_len - 1] == '=') out_len--;
if (input[in_len - 2] == '=') out_len--;
🤖 Prompt for AI Agents
In `@ixwebsocket/IXBase64.h` around lines 96 - 101, The code reads input[in_len -
1] and input[in_len - 2] without checking for empty input, causing OOB when
in_len == 0; update the start of the block in IXBase64.h (variables input,
in_len, out_len) to handle empty input early—e.g., if in_len == 0 return an
empty output (or otherwise handle as a special case) before computing out_len or
accessing input[in_len - 1/ -2]; alternatively ensure you only access those
indices when in_len >= 2 and adjust out_len logic accordingly.


out.resize(out_len);

for (size_t i = 0, j = 0; i < in_len;) {
uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
Comment on lines +106 to +109
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Signed char causes negative array index — out-of-bounds access.

static_cast<int>(input[i++]) preserves the sign of char on platforms where char is signed. If input contains bytes with the high bit set (values 128-255), the cast produces negative integers, causing kDecodingTable[negative_index] to read before the array — undefined behavior.

🐛 Proposed fix to ensure unsigned indexing
-      uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
-      uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
-      uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
-      uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
+      uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<unsigned char>(input[i++])];
+      uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<unsigned char>(input[i++])];
+      uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<unsigned char>(input[i++])];
+      uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<unsigned char>(input[i++])];
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<unsigned char>(input[i++])];
uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<unsigned char>(input[i++])];
uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<unsigned char>(input[i++])];
uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<unsigned char>(input[i++])];
🤖 Prompt for AI Agents
In `@ixwebsocket/IXBase64.h` around lines 106 - 109, The code in IXBase64.h reads
kDecodingTable using static_cast<int>(input[i++]) which on platforms with signed
char can produce negative indices and OOB access; update the indexing to first
convert the input byte to an unsigned type before casting to int (e.g. use
static_cast<int>(static_cast<unsigned char>(input[i++])) or cast to uint8_t then
to int) for the four reads that assign a, b, c, d so bytes with high bit set
index kDecodingTable safely while preserving the existing '=' handling logic.


uint32_t triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6);

if (j < out_len) out[j++] = (triple >> 2 * 8) & 0xFF;
if (j < out_len) out[j++] = (triple >> 1 * 8) & 0xFF;
if (j < out_len) out[j++] = (triple >> 0 * 8) & 0xFF;
}

return "";
}

};

}

#endif /* _MACARON_BASE64_H_ */
61 changes: 61 additions & 0 deletions ixwebsocket/IXBench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* IXBench.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2017-2020 Machine Zone, Inc. All rights reserved.
*/

#include "IXBench.h"

#include <iostream>

namespace ix
{
Bench::Bench(const std::string& description)
: _description(description)
{
reset();
}

Bench::~Bench()
{
if (!_reported)
{
report();
}
}

void Bench::reset()
{
_start = std::chrono::high_resolution_clock::now();
_reported = false;
}

void Bench::report()
{
auto now = std::chrono::high_resolution_clock::now();
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - _start);

_duration = microseconds.count();
std::cerr << _description << " completed in " << _duration << " us" << std::endl;

setReported();
}

void Bench::record()
{
auto now = std::chrono::high_resolution_clock::now();
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - _start);

_duration = microseconds.count();
}

void Bench::setReported()
{
_reported = true;
}

uint64_t Bench::getDuration() const
{
return _duration;
}
} // namespace ix
32 changes: 32 additions & 0 deletions ixwebsocket/IXBench.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* IXBench.h
* Author: Benjamin Sergeant
* Copyright (c) 2017-2020 Machine Zone, Inc. All rights reserved.
*/
#pragma once

#include <chrono>
#include <cstdint>
#include <string>

namespace ix
{
class Bench
{
public:
Bench(const std::string& description);
~Bench();

void reset();
void record();
void report();
void setReported();
uint64_t getDuration() const;

private:
std::string _description;
std::chrono::time_point<std::chrono::high_resolution_clock> _start;
uint64_t _duration;
bool _reported;
};
} // namespace ix
35 changes: 35 additions & 0 deletions ixwebsocket/IXCancellationRequest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* IXCancellationRequest.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/

#include "IXCancellationRequest.h"

#include <cassert>
#include <chrono>

namespace ix
{
CancellationRequest makeCancellationRequestWithTimeout(
int secs, std::atomic<bool>& requestInitCancellation)
{
assert(secs > 0);

auto start = std::chrono::system_clock::now();
auto timeout = std::chrono::seconds(secs);

auto isCancellationRequested = [&requestInitCancellation, start, timeout]() -> bool {
// Was an explicit cancellation requested ?
if (requestInitCancellation) return true;

auto now = std::chrono::system_clock::now();
if ((now - start) > timeout) return true;

// No cancellation request
return false;
};

return isCancellationRequested;
}
} // namespace ix
18 changes: 18 additions & 0 deletions ixwebsocket/IXCancellationRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* IXCancellationRequest.h
* Author: Benjamin Sergeant
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
*/

#pragma once

#include <atomic>
#include <functional>

namespace ix
{
using CancellationRequest = std::function<bool()>;

CancellationRequest makeCancellationRequestWithTimeout(
int seconds, std::atomic<bool>& requestInitCancellation);
} // namespace ix
73 changes: 73 additions & 0 deletions ixwebsocket/IXConnectionState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* IXConnectionState.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/

#include "IXConnectionState.h"

namespace ix
{
std::atomic<uint64_t> ConnectionState::_globalId(0);

ConnectionState::ConnectionState()
: _terminated(false)
{
computeId();
}

void ConnectionState::computeId()
{
_id = std::to_string(_globalId++);
}

const std::string& ConnectionState::getId() const
{
return _id;
}

std::shared_ptr<ConnectionState> ConnectionState::createConnectionState()
{
return std::make_shared<ConnectionState>();
}

void ConnectionState::setOnSetTerminatedCallback(const OnSetTerminatedCallback& callback)
{
_onSetTerminatedCallback = callback;
}

bool ConnectionState::isTerminated() const
{
return _terminated;
}

void ConnectionState::setTerminated()
{
_terminated = true;

if (_onSetTerminatedCallback)
{
_onSetTerminatedCallback();
}
}

const std::string& ConnectionState::getRemoteIp()
{
return _remoteIp;
}

int ConnectionState::getRemotePort()
{
return _remotePort;
}

void ConnectionState::setRemoteIp(const std::string& remoteIp)
{
_remoteIp = remoteIp;
}

void ConnectionState::setRemotePort(int remotePort)
{
_remotePort = remotePort;
}
} // namespace ix
Loading