From 807dbdfede2e9f5c0caf62168685a5e9ff4b98fe Mon Sep 17 00:00:00 2001 From: taynpg Date: Wed, 9 Apr 2025 15:05:56 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E5=A4=84=E7=90=86=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=86=85=E5=AD=98=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- client/client.cpp | 6 +++--- util/util.cpp | 18 ++++++++++++------ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 51b1082..bf47ebd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.16) -project(transm VERSION 1.5.0 LANGUAGES CXX) +project(transm VERSION 1.5.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/client/client.cpp b/client/client.cpp index 3b6001d..9cd4f96 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -793,8 +793,8 @@ bool CClient::cmd_ls(const std::string& param) buf->type_ = TYPE_GET_DIRFILES; CMessageInfo msg_info(own_id_); msg_info.str = path; - serialize(msg_info, &buf->data_, buf->len_); buf->tid_ = sr->id; + serialize(msg_info, &buf->data_, buf->len_); if (!send_frame(buf.get())) { TLOGE("Send Failed {}", __LINE__); @@ -1094,13 +1094,13 @@ void CClient::handle_frame(CFrameBuffer* buf) if (!get_dir_files(msg_info.str, out, err)) { TLOGE("Get Dir Files Failed. {}", err); buf->type_ = TYPE_GET_DIRFILES_FAILED; - delete[] buf->data_; msg_info.str = err; } else { buf->type_ = TYPE_GET_DIRFILES_DONE; - delete[] buf->data_; msg_info.str = out; } + delete[] buf->data_; + buf->data_ = nullptr; serialize(msg_info, &buf->data_, buf->len_); std::swap(buf->tid_, buf->fid_); if (!send_frame(buf)) { diff --git a/util/util.cpp b/util/util.cpp index 00b43c5..2750850 100644 --- a/util/util.cpp +++ b/util/util.cpp @@ -9,7 +9,7 @@ CTransProtocal::CTransProtocal() = default; constexpr uint8_t kz = 16; -static bool use_encrypt = true; +static bool use_encrypt = false; CTransProtocal::~CTransProtocal() = default; /* @@ -144,12 +144,13 @@ void serialize(CMessageInfo& msg_info, char** out_buf, int& len) // 计算总长度 len = sizeof(int) * 4 + info.id.size() + info.uuid.size() + info.str.size() + info.data.size() + kz + 1; - // 《这里为了效率》,认为如果 *out_buf 不为空,则直接使用,且长度符合要求 - // 调用方负责确保内存够用性(len小于最大长度)和可用性。 - // 即,如果调用方及高频率调用 serialize, 且每次 len 固定就复用内存,完了再释放。 + // 《这里为了效率》, + // 认为如果 *out_buf 不为空,则直接使用,且长度符合要求 + // 调用方负责确保内存够用性(len <= 可用最大空间长度)和内存可用性。 + // 即,如果调用方及高频率调用 serialize, 且每次 len <= 已分配空间就复用内存,完了再释放。 // 低频率或者 len 不固定时,每次都释放内存,并置 nullptr。 if (*out_buf == nullptr) { - *out_buf = new char[len]{}; // 分配内存(调用方负责释放) + *out_buf = new char[len]; // 分配内存(调用方负责释放) } char* ptr = *out_buf + kz + 1; @@ -312,9 +313,14 @@ void hash(const char* data, uint8_t k[32]) void rdm(uint8_t* o, size_t size) { + /* + 需要加密安全:坚持用random_device(慢) + 需要性能:用 mt19937 + uniform_int_distribution + */ std::random_device rd; + std::mt19937 gen(rd()); std::uniform_int_distribution dist(0, 255); - std::generate(o, o + size, [&]() { return static_cast(dist(rd)); }); + std::generate(o, o + size, [&]() { return static_cast(dist(gen)); }); } bool encrypt(const uint8_t* k, uint8_t* m, size_t len)