Skip to content

Commit a0b95fd

Browse files
committed
Do not send any data if message is too big
CURA-11103
1 parent d288e43 commit a0b95fd

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/Socket_p.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,37 +338,41 @@ void Socket::Private::run()
338338
// Send a message to the connected socket.
339339
void Socket::Private::sendMessage(const MessagePtr& message)
340340
{
341-
uint32_t header = (ARCUS_SIGNATURE << 16) | (VERSION_MAJOR << 8) | (VERSION_MINOR);
341+
const uint32_t message_size = message->ByteSizeLong();
342+
if (message_size > message_size_maximum)
343+
{
344+
error(ErrorCode::SendFailedError, "Message is too big to be sent");
345+
return;
346+
}
347+
348+
const uint32_t header = (ARCUS_SIGNATURE << 16) | (VERSION_MAJOR << 8) | (VERSION_MINOR);
342349
if (platform_socket.writeUInt32(header) == -1)
343350
{
344351
error(ErrorCode::SendFailedError, "Could not send message header");
345352
return;
346353
}
347354

348-
uint32_t message_size = message->ByteSizeLong();
349355
if (platform_socket.writeUInt32(message_size) == -1)
350356
{
351357
error(ErrorCode::SendFailedError, "Could not send message size");
352358
return;
353359
}
354360

355-
uint32_t type_id = message_types.getMessageTypeId(message);
361+
const uint32_t type_id = message_types.getMessageTypeId(message);
356362
if (platform_socket.writeUInt32(type_id) == -1)
357363
{
358364
error(ErrorCode::SendFailedError, "Could not send message type");
359365
return;
360366
}
361367

362-
std::string data = message->SerializeAsString();
363-
if (data.size() > message_size_maximum)
364-
{
365-
error(ErrorCode::SendFailedError, "Message is too big to be sent");
366-
}
367-
else if (platform_socket.writeBytes(data.size(), data.data()) == -1)
368+
const std::string data = message->SerializeAsString();
369+
if (platform_socket.writeBytes(data.size(), data.data()) == -1)
368370
{
369371
error(ErrorCode::SendFailedError, "Could not send message data");
372+
return;
370373
}
371-
DEBUG(std::string("Sending message of type ") + std::to_string(type_id) + " and size " + std::to_string(message_size));
374+
375+
DEBUG(std::string("Sent message of type ") + std::to_string(type_id) + " and size " + std::to_string(message_size));
372376
}
373377

374378
// Handle receiving data until we have a proper message.

0 commit comments

Comments
 (0)