Skip to content

Commit

Permalink
fix: remove uses of insecure "reinterpret_cast"
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored and dudantas committed Sep 25, 2024
1 parent 4be4078 commit 3cb75d8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/server/network/message/networkmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ std::string NetworkMessage::getString(uint16_t stringLen /* = 0*/) {
}

if (!canRead(stringLen)) {
g_logger().error("Not enough data to read string of length: {}", stringLen);
g_logger().error("[{}] not enough data to read string of length: {}", __METHOD_NAME__, stringLen);

Check warning on line 27 in src/server/network/message/networkmessage.cpp

View workflow job for this annotation

GitHub Actions / Qodana for C/C++

misra-cpp2008-5-2-12

MISRA 5-2-12: An identifier with array type passed as a function argument shall not decay to a pointer
return {};
}

if (stringLen > NETWORKMESSAGE_MAXSIZE) {
g_logger().error("[NetworkMessage::addString] - Exceded NetworkMessage max size: {}, actually size: {}", NETWORKMESSAGE_MAXSIZE, stringLen);
g_logger().error("[{}] exceded NetworkMessage max size: {}, actually size: {}", __METHOD_NAME__, NETWORKMESSAGE_MAXSIZE, stringLen);

Check warning on line 32 in src/server/network/message/networkmessage.cpp

View workflow job for this annotation

GitHub Actions / Qodana for C/C++

misra-cpp2008-5-2-12

MISRA 5-2-12: An identifier with array type passed as a function argument shall not decay to a pointer
return {};
}

const char* v = reinterpret_cast<const char*>(buffer.data() + info.position);
// Copy the string from the buffer
std::string result(buffer.begin() + info.position, buffer.begin() + info.position + stringLen);
info.position += stringLen;
return std::string(v, stringLen);
return result;
}

Position NetworkMessage::getPosition() {
Expand Down
3 changes: 2 additions & 1 deletion src/server/network/message/networkmessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class NetworkMessage {
// 2 bytes for encrypted message size
static constexpr MsgSize_t INITIAL_BUFFER_POSITION = 8;

NetworkMessage() : buffer(NETWORKMESSAGE_MAXSIZE, 0) {}
NetworkMessage() :
buffer(NETWORKMESSAGE_MAXSIZE, 0) { }

void reset() {
info = {};
Expand Down

0 comments on commit 3cb75d8

Please sign in to comment.