Skip to content

Commit

Permalink
Allowed wsl_peer to grab the lenth of incoming messages and only acce…
Browse files Browse the repository at this point in the history
…pt them if they fit in in_buffer
  • Loading branch information
RadenTheFolf committed Oct 13, 2024
1 parent 83d6573 commit cb4aaa2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions modules/websocket/packet_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class PacketBuffer {
return _queued;
}

int space_left() const {
return _payload.space_left();
}

void clear() {
_payload.resize(0);
_packets.resize(0);
Expand Down
17 changes: 16 additions & 1 deletion modules/websocket/wsl_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,15 @@ Error WSLPeer::connect_to_url(const String &p_url, Ref<TLSOptions> p_options) {
ssize_t WSLPeer::_wsl_recv_callback(wslay_event_context_ptr ctx, uint8_t *data, size_t len, int flags, void *user_data) {
WSLPeer *peer = (WSLPeer *)user_data;
Ref<StreamPeer> conn = peer->connection;

if (conn.is_null()) {
wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
return -1;
}

if (peer->in_buffer.space_left() < peer->length_needed) {
return WSLAY_ERR_NOMEM;
}
int read = 0;
Error err = conn->get_partial_data(data, len, read);
if (err != OK) {
Expand All @@ -584,6 +589,16 @@ ssize_t WSLPeer::_wsl_recv_callback(wslay_event_context_ptr ctx, uint8_t *data,
return read;
}

void WSLPeer::_wsl_on_frame_start_callback(wslay_event_context_ptr ctx, const wslay_event_on_frame_recv_start_arg *arg, void *user_data) {
WSLPeer *peer = (WSLPeer *)user_data;
Ref<StreamPeer> conn = peer->connection;
if (arg->opcode == WSLAY_TEXT_FRAME || arg->opcode == WSLAY_BINARY_FRAME) {
if (peer->in_buffer.space_left() < arg->payload_length) {

Check failure on line 596 in modules/websocket/wsl_peer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor (target=editor tests=yes)

comparison of integer expressions of different signedness: 'int' and 'const uint64_t' {aka 'const long unsigned int'} [-Werror=sign-compare]

Check failure on line 596 in modules/websocket/wsl_peer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor w/ Mono (target=editor)

comparison of integer expressions of different signedness: 'int' and 'const uint64_t' {aka 'const long unsigned int'} [-Werror=sign-compare]

Check failure on line 596 in modules/websocket/wsl_peer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with doubles and GCC sanitizers (target=editor, tests=yes, dev_build=yes, scu_build=yes, precision=double, use_asan=yes, use_ubsan=yes, linker=gold)

comparison of integer expressions of different signedness: 'int' and 'const uint64_t' {aka 'const long unsigned int'} [-Werror=sign-compare]

Check failure on line 596 in modules/websocket/wsl_peer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

comparison of integers of different signs: 'int' and 'const uint64_t' (aka 'const unsigned long') [-Werror,-Wsign-compare]

Check failure on line 596 in modules/websocket/wsl_peer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

comparison of integers of different signs: 'int' and 'const uint64_t' (aka 'const unsigned long') [-Werror,-Wsign-compare]

Check failure on line 596 in modules/websocket/wsl_peer.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Template w/ Mono (target=template_release, tests=yes)

comparison of integer expressions of different signedness: 'int' and 'const uint64_t' {aka 'const long unsigned int'} [-Werror=sign-compare]
peer->length_needed = arg->payload_length;
}
}
}

ssize_t WSLPeer::_wsl_send_callback(wslay_event_context_ptr ctx, const uint8_t *data, size_t len, int flags, void *user_data) {
WSLPeer *peer = (WSLPeer *)user_data;
Ref<StreamPeer> conn = peer->connection;
Expand Down Expand Up @@ -645,7 +660,7 @@ wslay_event_callbacks WSLPeer::_wsl_callbacks = {
_wsl_recv_callback,
_wsl_send_callback,
_wsl_genmask_callback,
nullptr, /* on_frame_recv_start_callback */
_wsl_on_frame_start_callback, /* on_frame_recv_start_callback */
nullptr, /* on_frame_recv_callback */
nullptr, /* on_frame_recv_end_callback */
_wsl_msg_recv_callback
Expand Down
3 changes: 3 additions & 0 deletions modules/websocket/wsl_peer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class WSLPeer : public WebSocketPeer {

// Callbacks.
static ssize_t _wsl_recv_callback(wslay_event_context_ptr ctx, uint8_t *data, size_t len, int flags, void *user_data);
static void _wsl_on_frame_start_callback(wslay_event_context_ptr ctx, const struct wslay_event_on_frame_recv_start_arg *arg, void *user_data);
static ssize_t _wsl_send_callback(wslay_event_context_ptr ctx, const uint8_t *data, size_t len, int flags, void *user_data);
static int _wsl_genmask_callback(wslay_event_context_ptr ctx, uint8_t *buf, size_t len, void *user_data);
static void _wsl_msg_recv_callback(wslay_event_context_ptr ctx, const struct wslay_event_on_msg_recv_arg *arg, void *user_data);
Expand Down Expand Up @@ -122,6 +123,8 @@ class WSLPeer : public WebSocketPeer {
void _clear();

public:
int length_needed = 0;

static void initialize();
static void deinitialize();

Expand Down

0 comments on commit cb4aaa2

Please sign in to comment.