From a2345d1f45870eb00a2a3d5d6fd21a386efcade3 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 27 Jan 2019 17:01:35 +0500 Subject: Replaced "new/delete" with safer analogues --- src/Network.cpp | 20 ++++++-------------- src/Network.hpp | 5 +++-- src/Stream.cpp | 44 ++++++++++++++++---------------------------- src/Stream.hpp | 4 +--- 4 files changed, 26 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/Network.cpp b/src/Network.cpp index c8be740..431a5b1 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -7,24 +7,16 @@ Network::Network(std::string address, unsigned short port) { try { - socket = new Socket(address, port); + socket = std::make_unique(address, port); + stream = std::make_unique(socket.get()); } catch (std::exception &e) { LOG(WARNING) << "Connection failed: " << e.what(); throw; - } - - try { - stream = new StreamSocket(socket); - } catch (std::exception &e) { - LOG(WARNING) << "Stream creation failed: " << e.what(); - } - - + } } Network::~Network() { - delete stream; - delete socket; + } std::shared_ptr Network::ReceivePacket(ConnectionState state, bool useCompression) { @@ -94,7 +86,7 @@ void Network::SendPacket(Packet &packet, int compressionThreshold) { stream->WriteVarInt(packetSize.GetCountedSize()); stream->WriteVarInt(0); stream->WriteVarInt(packet.GetPacketId()); - packet.ToStream(stream); + packet.ToStream(stream.get()); } else { throw std::runtime_error("Compressing send data not supported"); } @@ -105,7 +97,7 @@ void Network::SendPacket(Packet &packet, int compressionThreshold) { packet.ToStream(&packetSize); stream->WriteVarInt(packetSize.GetCountedSize()); stream->WriteVarInt(packet.GetPacketId()); - packet.ToStream(stream); + packet.ToStream(stream.get()); } } diff --git a/src/Network.hpp b/src/Network.hpp index 5d7fc38..29a090f 100644 --- a/src/Network.hpp +++ b/src/Network.hpp @@ -11,12 +11,13 @@ enum ConnectionState : unsigned char { }; class Network { - Socket *socket = nullptr; - StreamSocket *stream = nullptr; + std::unique_ptr socket; + std::unique_ptr stream; std::shared_ptr ReceivePacketByPacketId(int packetId, ConnectionState state, StreamInput &stream); public: Network(std::string address, unsigned short port); + ~Network(); std::shared_ptr ReceivePacket(ConnectionState state = Play, bool useCompression = false); diff --git a/src/Stream.cpp b/src/Stream.cpp index d66335e..efe37d2 100644 --- a/src/Stream.cpp +++ b/src/Stream.cpp @@ -71,11 +71,10 @@ double StreamInput::ReadDouble() { std::string StreamInput::ReadString() { int strLength = ReadVarInt(); - unsigned char *buff = new unsigned char[strLength + 1]; - ReadData(buff, strLength); + std::vector buff(strLength + 1); + ReadData(buff.data(), strLength); buff[strLength] = 0; - std::string str((char *) buff); - delete[] buff; + std::string str((char *) buff.data()); return str; } @@ -178,11 +177,9 @@ Uuid StreamInput::ReadUuid() { } std::vector StreamInput::ReadByteArray(size_t arrLength) { - unsigned char *buffer = new unsigned char[arrLength]; - ReadData(buffer, arrLength); - std::vector ret(buffer, buffer + arrLength); - delete[] buffer; - return ret; + std::vector buffer(arrLength); + ReadData(buffer.data(), arrLength); + return buffer; } @@ -307,7 +304,7 @@ void StreamOutput::WriteByteArray(const std::vector &value) { } void StreamBuffer::ReadData(unsigned char *buffPtr, size_t buffLen) { - size_t bufferLengthLeft = buffer + bufferLength - bufferPtr; + size_t bufferLengthLeft = buffer.data() + buffer.size() - bufferPtr; if (bufferLengthLeft < buffLen) throw std::runtime_error("Internal error: StreamBuffer reader out of data"); @@ -316,38 +313,29 @@ void StreamBuffer::ReadData(unsigned char *buffPtr, size_t buffLen) { } void StreamBuffer::WriteData(unsigned char *buffPtr, size_t buffLen) { - size_t bufferLengthLeft = buffer + bufferLength - bufferPtr; + size_t bufferLengthLeft = buffer.data() + buffer.size() - bufferPtr; if (bufferLengthLeft < buffLen) throw std::runtime_error("Internal error: StreamBuffer writer out of data"); std::memcpy(bufferPtr, buffPtr, buffLen); bufferPtr += buffLen; } -StreamBuffer::StreamBuffer(unsigned char *data, size_t dataLen) { - buffer = new unsigned char[dataLen]; - bufferPtr = buffer; - bufferLength = dataLen; - std::memcpy(buffer, data, dataLen); +StreamBuffer::StreamBuffer(unsigned char *data, size_t dataLen) : buffer(data,data+dataLen) { + bufferPtr = buffer.data(); } -StreamBuffer::StreamBuffer(size_t bufferLen) { - buffer = new unsigned char[bufferLen]; - bufferPtr = buffer; - bufferLength = bufferLen; - for (unsigned char *p = buffer; p != buffer + bufferLength; ++p) - *p = 0; -} - -StreamBuffer::~StreamBuffer() { - delete[] buffer; +StreamBuffer::StreamBuffer(size_t bufferLen) : buffer(bufferLen) { + bufferPtr = buffer.data(); + for (auto &it : buffer) + it = 0; } std::vector StreamBuffer::GetBuffer() { - return std::vector(buffer, buffer + bufferLength); + return buffer; } size_t StreamBuffer::GetReadedLength() { - return bufferPtr - buffer; + return bufferPtr - buffer.data(); } void StreamCounter::WriteData(unsigned char *buffPtr, size_t buffLen) { diff --git a/src/Stream.hpp b/src/Stream.hpp index 8269300..a7c161b 100644 --- a/src/Stream.hpp +++ b/src/Stream.hpp @@ -73,9 +73,8 @@ public: }; class StreamBuffer : public StreamInput, public StreamOutput { - unsigned char *buffer; + std::vector buffer; unsigned char *bufferPtr; - size_t bufferLength; void ReadData(unsigned char *buffPtr, size_t buffLen) override; void WriteData(unsigned char *buffPtr, size_t buffLen) override; @@ -83,7 +82,6 @@ class StreamBuffer : public StreamInput, public StreamOutput { public: StreamBuffer(unsigned char *data, size_t dataLen); StreamBuffer(size_t bufferLen); - ~StreamBuffer(); std::vector GetBuffer(); size_t GetReadedLength(); -- cgit v1.2.3